Entity
extends Entity
in package
Entity encapsulation, for use with CodeIgniter\Model
Tags
Table of Contents
- $attributes : array<string|int, mixed>
- Holds the current values of all class vars.
- $castHandlers : array<string, string>
- Custom convert handlers
- $casts : mixed
- Array of field names and the type of value to cast them as when they are accessed.
- $datamap : mixed
- Maps names used in sets and gets against unique names within the class, allowing independence from database column names.
- $dates : mixed
- $original : array<string|int, mixed>
- Holds original copies of all class vars so we can determine what's actually been changed and not accidentally write nulls where we shouldn't.
- $_cast : bool
- Holds info whenever properties have to be casted
- $defaultCastHandlers : array<string, string>
- Default convert handlers
- __construct() : mixed
- Allows filling in Entity parameters during construction.
- __get() : mixed
- Magic method to allow retrieval of protected and private class properties either by their name, or through a `getCamelCasedProperty()` method.
- __isset() : bool
- Returns true if a property exists names $key, or a getter method exists named like for __get().
- __set() : $this
- Magic method to all protected/private class properties to be easily set, either through a direct access or a `setCamelCasedProperty()` method.
- __unset() : void
- Unsets an attribute property.
- cast() : bool|Entity
- Change the value of the private $_cast property
- fill() : $this
- Takes an array of key/value pairs and sets them as class properties, using any `setCamelCasedProperty()` methods that may or may not exist.
- hasChanged() : bool
- Checks a property to see if it has changed since the entity was created. Or, without a parameter, checks if any properties have changed.
- jsonSerialize() : array<string|int, mixed>|mixed
- Support for json_encode()
- setAttributes() : $this
- Set raw data array without any mutations
- syncOriginal() : $this
- Ensures our "original" values match the current values.
- toArray() : array<string|int, mixed>
- General method that will return all public and protected values of this entity as an array. All values are accessed through the __get() magic method so will have any casts, etc applied to them.
- toRawArray() : array<string|int, mixed>
- Returns the raw values of the current attributes.
- castAs() : mixed
- Provides the ability to cast an item as a specific data type.
- mapProperty() : mixed|string
- Checks the datamap to see if this column name is being mapped, and returns the mapped name, if any, or the original name.
- mutateDate() : Time|mixed
- Converts the given string|timestamp|DateTime|Time instance into the "CodeIgniter\I18n\Time" object.
- castAsJson() : mixed
- Cast as JSON
Properties
$attributes
Holds the current values of all class vars.
protected
array<string|int, mixed>
$attributes
= []
$castHandlers
Custom convert handlers
protected
array<string, string>
$castHandlers
= []
$casts
Array of field names and the type of value to cast them as when they are accessed.
protected
mixed
$casts
= []
$datamap
Maps names used in sets and gets against unique names within the class, allowing independence from database column names.
protected
mixed
$datamap
= []
Example: $datamap = [ 'db_name' => 'class_name' ];
$dates
protected
mixed
$dates
= ['created_at', 'updated_at', 'deleted_at']
$original
Holds original copies of all class vars so we can determine what's actually been changed and not accidentally write nulls where we shouldn't.
protected
array<string|int, mixed>
$original
= []
$_cast
Holds info whenever properties have to be casted
private
bool
$_cast
= true
$defaultCastHandlers
Default convert handlers
private
array<string, string>
$defaultCastHandlers
= ['array' => CodeIgniterEntityCastArrayCast::class, 'bool' => CodeIgniterEntityCastBooleanCast::class, 'boolean' => CodeIgniterEntityCastBooleanCast::class, 'csv' => CodeIgniterEntityCastCSVCast::class, 'datetime' => CodeIgniterEntityCastDatetimeCast::class, 'double' => CodeIgniterEntityCastFloatCast::class, 'float' => CodeIgniterEntityCastFloatCast::class, 'int' => CodeIgniterEntityCastIntegerCast::class, 'integer' => CodeIgniterEntityCastIntegerCast::class, 'json' => CodeIgniterEntityCastJsonCast::class, 'object' => CodeIgniterEntityCastObjectCast::class, 'string' => CodeIgniterEntityCastStringCast::class, 'timestamp' => CodeIgniterEntityCastTimestampCast::class, 'uri' => CodeIgniterEntityCastURICast::class]
Methods
__construct()
Allows filling in Entity parameters during construction.
public
__construct([array<string|int, mixed>|null $data = null ]) : mixed
Parameters
- $data : array<string|int, mixed>|null = null
Return values
mixed —__get()
Magic method to allow retrieval of protected and private class properties either by their name, or through a `getCamelCasedProperty()` method.
public
__get(string $key) : mixed
Examples: $p = $this->my_property $p = $this->getMyProperty()
Parameters
- $key : string
Tags
Return values
mixed —__isset()
Returns true if a property exists names $key, or a getter method exists named like for __get().
public
__isset(string $key) : bool
Parameters
- $key : string
Return values
bool —__set()
Magic method to all protected/private class properties to be easily set, either through a direct access or a `setCamelCasedProperty()` method.
public
__set(string $key[, mixed|null $value = null ]) : $this
Examples: $this->my_property = $p; $this->setMyProperty() = $p;
Parameters
- $key : string
- $value : mixed|null = null
Tags
Return values
$this —__unset()
Unsets an attribute property.
public
__unset(string $key) : void
Parameters
- $key : string
Return values
void —cast()
Change the value of the private $_cast property
public
cast([bool|null $cast = null ]) : bool|Entity
Parameters
- $cast : bool|null = null
Return values
bool|Entity —fill()
Takes an array of key/value pairs and sets them as class properties, using any `setCamelCasedProperty()` methods that may or may not exist.
public
fill([array<string|int, mixed> $data = null ]) : $this
Parameters
- $data : array<string|int, mixed> = null
Return values
$this —hasChanged()
Checks a property to see if it has changed since the entity was created. Or, without a parameter, checks if any properties have changed.
public
hasChanged([string $key = null ]) : bool
Parameters
- $key : string = null
Return values
bool —jsonSerialize()
Support for json_encode()
public
jsonSerialize() : array<string|int, mixed>|mixed
Return values
array<string|int, mixed>|mixed —setAttributes()
Set raw data array without any mutations
public
setAttributes(array<string|int, mixed> $data) : $this
Parameters
- $data : array<string|int, mixed>
Return values
$this —syncOriginal()
Ensures our "original" values match the current values.
public
syncOriginal() : $this
Return values
$this —toArray()
General method that will return all public and protected values of this entity as an array. All values are accessed through the __get() magic method so will have any casts, etc applied to them.
public
toArray([bool $onlyChanged = false ][, bool $cast = true ][, bool $recursive = false ]) : array<string|int, mixed>
Parameters
- $onlyChanged : bool = false
-
If true, only return values that have changed since object creation
- $cast : bool = true
-
If true, properties will be casted.
- $recursive : bool = false
-
If true, inner entities will be casted as array as well.
Return values
array<string|int, mixed> —toRawArray()
Returns the raw values of the current attributes.
public
toRawArray([bool $onlyChanged = false ][, bool $recursive = false ]) : array<string|int, mixed>
Parameters
- $onlyChanged : bool = false
-
If true, only return values that have changed since object creation
- $recursive : bool = false
-
If true, inner entities will be casted as array as well.
Return values
array<string|int, mixed> —castAs()
Provides the ability to cast an item as a specific data type.
protected
castAs(mixed $value, string $attribute[, string $method = 'get' ]) : mixed
Add ? at the beginning of $type (i.e. ?string) to get NULL instead of casting $value if $value === null
Parameters
- $value : mixed
-
Attribute value
- $attribute : string
-
Attribute name
- $method : string = 'get'
-
Allowed to "get" and "set"
Tags
Return values
mixed —mapProperty()
Checks the datamap to see if this column name is being mapped, and returns the mapped name, if any, or the original name.
protected
mapProperty(string $key) : mixed|string
Parameters
- $key : string
Return values
mixed|string —mutateDate()
Converts the given string|timestamp|DateTime|Time instance into the "CodeIgniter\I18n\Time" object.
protected
mutateDate(mixed $value) : Time|mixed
Parameters
- $value : mixed
Tags
Return values
Time|mixed —castAsJson()
Cast as JSON
private
castAsJson(mixed $value[, bool $asArray = false ]) : mixed
Parameters
- $value : mixed
- $asArray : bool = false