PHP Class Webiny\Component\Entity\Attribute\AbstractAttribute

Inheritance: implements JsonSerializabl\JsonSerializable, use trait Webiny\Component\StdLib\StdLibTrait, use trait Webiny\Component\StdLib\FactoryLoaderTrait
Mostra file Open project: Webiny/Framework Class Usage Examples

Protected Properties

Property Type Description
$attribute null | string Attribute name
$defaultValue null Default value
$onFromDbCallback null The value returned from the callback will be set as attribute value
$onGetCallback null The value returned from the callback will be returned as attribute value.
$onSetCallback null The value returned from the callback will be set as a new attribute value.
$onSetValue null Value to trigger $onSetCallback
$onToArrayCallback null The value returned from the callback will be returned as attribute value.
$onToDbCallback null The value returned from the callback will be returned as attribute value for database.
$once boolean If true - updating will be disabled
$parent Webiny\Component\Entity\AbstractEntity Entity this attribute belongs to
$required boolean Is this attribute required
$setAfterPopulate boolean If true - will wait until mass populate is finished and assign the value afterwards
$skipOnPopulate boolean If true - mass populate will skip this attribute
$storeToDb boolean If true - will store this attribute to database
$toArrayDefault null If true - this attribute will be included in the output of entity's toArray method
$validationMessages array Validation messages
$validatorInterface string
$validators array Attribute validators
$value null Actual value

Public Methods

Method Description
__construct ( string $name = null, AbstractEntity $parent = null )
__toString ( ) : string Get attribute value as string
attr ( null | string $attribute = null ) : EntityAttributeBuilder | string Create new attribute or get name of current attribute
getAfterPopulate ( ) : boolean Should this attribute's value be assigned after populate() ?
getDbValue ( ) : string Get value that will be stored to database
getDefaultValue ( ) : mixed Get default value
getName ( ) : string Get attribute name
getOnce ( ) Get 'once' flag
This flag tells you whether this attribute should only be populated when it's a new AbstractEntity instance.
By default, attributes are populated each time.
getParent ( ) : AbstractEntity Get entity instance this attribute belongs to
getSkipOnPopulate ( ) : boolean Get skipOnPopulate flag
getStoreToDb ( ) : boolean Should this attribute value be stored to DB
getToArrayDefault ( ) : boolean Get toArrayDefault flag
getValidationMessages ( string | null $validator = null ) : mixed Get validation messages
getValidators ( ) : array Get validators
getValue ( array $params = [], boolean $processCallbacks = true ) Get attribute value
hasToArrayCallback ( )
hasValue ( )
isRequired ( ) Get required flag
jsonSerialize ( ) : mixed (PHP 5 >= 5.4.0)
Specify data which should be serialized to JSON
onFromDb ( $callback ) Set callback that will be used to process value when data is loaded from DB
onGet ( $callable = null )
onSet ( $value, $callable = null )
onToArray ( $callback ) Set callback that will be used to process getToArrayValue() call
onToDb ( $callback ) Set callback that will be used to process getDbValue() call
setAfterPopulate ( boolean $flag = true ) Should this attribute's value be assigned after populate() ? This is useful when onSet() callback is triggering business logic that depends on other attributes that are not yet set because populate() cycle has not yet finished and onSet() callback of current attribute is executed
setDefaultValue ( mixed $defaultValue = null ) Set default value
setName ( string $attribute ) Set attribute name
setOnce ( boolean $flag = true ) Set 'once' flag
If true, it tells AbstractEntity to only populate this attribute if it's a new entity
This is useful when you want to protect values from being populate on later updates.
setParent ( AbstractEntity $entity )
setRequired ( boolean $flag = true ) Set required flag
setSkipOnPopulate ( boolean $flag = true ) Skip attribute on mass populate
setToArrayDefault ( boolean $flag = true ) Set if this attribute will be by default included in the output of entity's toArray method
setValidationMessages ( array $messages ) Set validation messages
setValidators ( array | string $validators = [] ) Set attribute validators
setValue ( null $value = null, boolean $fromDb = false ) Set attribute value
toArray ( array $params = [] ) : string Get value that will be used to represent this attribute when converting AbstractEntity to array

Protected Methods

Method Description
applyValidator ( string $validator, string $key, mixed $value, array $messages = [] ) Apply validator to given value
canAssign ( ) : boolean Check if value can be assigned to current attribute
expected ( string $expecting, string $got, boolean $return = false ) : ValidationException Throw or return attribute validation exception
processFromDbValue ( $value ) : mixed Triggered when calling 'setValue()' with $fromDb=true on attribute instance
processGetValue ( mixed $value, array $params = [], boolean $processCallbacks = true ) : mixed Triggered when calling 'getValue()' on attribute instance
processSetValue ( $value ) : mixed Triggered when calling 'setValue()' on attribute instance
processToArrayValue ( $value ) : mixed Triggered when calling 'toArray()' on the entity instance
processToDbValue ( $value ) : mixed Triggered when calling 'getDbValue()' on attribute instance
validate ( &$value ) Perform validation against given value

Private Methods

Method Description
processCallback ( $callback, $value, array $params = [] ) : mixed Execute given callback
Take $value and check if a valid callback is given
If yes, return the processed value.

Method Details

__construct() public method

public __construct ( string $name = null, AbstractEntity $parent = null )
$name string
$parent Webiny\Component\Entity\AbstractEntity

__toString() public method

Get attribute value as string
public __toString ( ) : string
return string

applyValidator() protected method

Apply validator to given value
protected applyValidator ( string $validator, string $key, mixed $value, array $messages = [] )
$validator string
$key string
$value mixed
$messages array

attr() public method

Create new attribute or get name of current attribute
public attr ( null | string $attribute = null ) : EntityAttributeBuilder | string
$attribute null | string
return Webiny\Component\Entity\EntityAttributeBuilder | string

canAssign() protected method

If Entity has an ID and attribute 'once' flag is set and attribute has a value assigned to it then a new value should not be assigned.
protected canAssign ( ) : boolean
return boolean

expected() protected method

Throw or return attribute validation exception
protected expected ( string $expecting, string $got, boolean $return = false ) : ValidationException
$expecting string
$got string
$return boolean
return Webiny\Component\Entity\Attribute\Validation\ValidationException

getAfterPopulate() public method

Should this attribute's value be assigned after populate() ?
public getAfterPopulate ( ) : boolean
return boolean

getDbValue() public method

If no value is set, default value will be used, and that value will also be assigned as a new attribute value.
public getDbValue ( ) : string
return string

getDefaultValue() public method

Get default value
public getDefaultValue ( ) : mixed
return mixed

getName() public method

Get attribute name
public getName ( ) : string
return string

getOnce() public method

Get 'once' flag
This flag tells you whether this attribute should only be populated when it's a new AbstractEntity instance.
By default, attributes are populated each time.
public getOnce ( )

getParent() public method

Get entity instance this attribute belongs to
public getParent ( ) : AbstractEntity
return Webiny\Component\Entity\AbstractEntity

getSkipOnPopulate() public method

Get skipOnPopulate flag
public getSkipOnPopulate ( ) : boolean
return boolean

getStoreToDb() public method

Should this attribute value be stored to DB
public getStoreToDb ( ) : boolean
return boolean

getToArrayDefault() public method

Get toArrayDefault flag
public getToArrayDefault ( ) : boolean
return boolean

getValidationMessages() public method

Get validation messages
public getValidationMessages ( string | null $validator = null ) : mixed
$validator string | null If given, returns validation message for given validator
return mixed

getValidators() public method

Get validators
public getValidators ( ) : array
return array

getValue() public method

Get attribute value
public getValue ( array $params = [], boolean $processCallbacks = true )
$params array
$processCallbacks boolean Process `onGet` callbacks

hasToArrayCallback() public method

public hasToArrayCallback ( )

hasValue() public method

public hasValue ( )

isRequired() public method

Get required flag
public isRequired ( )

jsonSerialize() public method

(PHP 5 >= 5.4.0)
Specify data which should be serialized to JSON
public jsonSerialize ( ) : mixed
return mixed data which can be serialized by json_encode, which is a value of any type other than a resource.

onFromDb() public method

Set callback that will be used to process value when data is loaded from DB
public onFromDb ( $callback )
$callback

onGet() public method

public onGet ( $callable = null )

onSet() public method

public onSet ( $value, $callable = null )

onToArray() public method

Set callback that will be used to process getToArrayValue() call
public onToArray ( $callback )
$callback

onToDb() public method

Set callback that will be used to process getDbValue() call
public onToDb ( $callback )
$callback

processFromDbValue() protected method

Triggered when calling 'setValue()' with $fromDb=true on attribute instance
protected processFromDbValue ( $value ) : mixed
$value
return mixed

processGetValue() protected method

Triggered when calling 'getValue()' on attribute instance
protected processGetValue ( mixed $value, array $params = [], boolean $processCallbacks = true ) : mixed
$value mixed
$params array
$processCallbacks boolean
return mixed

processSetValue() protected method

Triggered when calling 'setValue()' on attribute instance
protected processSetValue ( $value ) : mixed
$value
return mixed

processToArrayValue() protected method

Triggered when calling 'toArray()' on the entity instance
protected processToArrayValue ( $value ) : mixed
$value
return mixed

processToDbValue() protected method

Triggered when calling 'getDbValue()' on attribute instance
protected processToDbValue ( $value ) : mixed
$value
return mixed

setAfterPopulate() public method

Should this attribute's value be assigned after populate() ? This is useful when onSet() callback is triggering business logic that depends on other attributes that are not yet set because populate() cycle has not yet finished and onSet() callback of current attribute is executed
public setAfterPopulate ( boolean $flag = true )
$flag boolean

setDefaultValue() public method

Set default value
public setDefaultValue ( mixed $defaultValue = null )
$defaultValue mixed

setName() public method

Set attribute name
public setName ( string $attribute )
$attribute string

setOnce() public method

Set 'once' flag
If true, it tells AbstractEntity to only populate this attribute if it's a new entity
This is useful when you want to protect values from being populate on later updates.
public setOnce ( boolean $flag = true )
$flag boolean

setParent() public method

public setParent ( AbstractEntity $entity )
$entity Webiny\Component\Entity\AbstractEntity

setRequired() public method

Set required flag
public setRequired ( boolean $flag = true )
$flag boolean

setSkipOnPopulate() public method

Skip attribute on mass populate
public setSkipOnPopulate ( boolean $flag = true )
$flag boolean

setToArrayDefault() public method

Set if this attribute will be by default included in the output of entity's toArray method
public setToArrayDefault ( boolean $flag = true )
$flag boolean

setValidationMessages() public method

Set validation messages
public setValidationMessages ( array $messages )
$messages array

setValidators() public method

Set attribute validators
public setValidators ( array | string $validators = [] )
$validators array | string

setValue() public method

Set attribute value
public setValue ( null $value = null, boolean $fromDb = false )
$value null Attribute value
$fromDb boolean Is value coming from DB?

toArray() public method

Get value that will be used to represent this attribute when converting AbstractEntity to array
public toArray ( array $params = [] ) : string
$params array
return string

validate() protected method

Perform validation against given value
protected validate ( &$value )
$value

Property Details

$attribute protected_oe property

Attribute name
protected null|string $attribute
return null | string

$defaultValue protected_oe property

Default value
protected null $defaultValue
return null

$onFromDbCallback protected_oe property

The value returned from the callback will be set as attribute value
protected null $onFromDbCallback
return null

$onGetCallback protected_oe property

The value returned from the callback will be returned as attribute value.
protected null $onGetCallback
return null

$onSetCallback protected_oe property

The value returned from the callback will be set as a new attribute value.
protected null $onSetCallback
return null

$onSetValue protected_oe property

Value to trigger $onSetCallback
protected null $onSetValue
return null

$onToArrayCallback protected_oe property

The value returned from the callback will be returned as attribute value.
protected null $onToArrayCallback
return null

$onToDbCallback protected_oe property

The value returned from the callback will be returned as attribute value for database.
protected null $onToDbCallback
return null

$once protected_oe property

If true - updating will be disabled
protected bool $once
return boolean

$parent protected_oe property

Entity this attribute belongs to
protected AbstractEntity,Webiny\Component\Entity $parent
return Webiny\Component\Entity\AbstractEntity

$required protected_oe property

Is this attribute required
protected bool $required
return boolean

$setAfterPopulate protected_oe property

If true - will wait until mass populate is finished and assign the value afterwards
protected bool $setAfterPopulate
return boolean

$skipOnPopulate protected_oe property

If true - mass populate will skip this attribute
protected bool $skipOnPopulate
return boolean

$storeToDb protected_oe property

If true - will store this attribute to database
protected bool $storeToDb
return boolean

$toArrayDefault protected_oe property

If true - this attribute will be included in the output of entity's toArray method
protected null $toArrayDefault
return null

$validationMessages protected_oe property

Validation messages
protected array $validationMessages
return array

$validatorInterface protected_oe property

protected string $validatorInterface
return string

$validators protected_oe property

Attribute validators
protected array $validators
return array

$value protected_oe property

Actual value
protected null $value
return null