CodeIgniter v4.7 API

Entity
in package
implements JsonSerializable

Entity encapsulation, for use with CodeIgniter\Model

Tags
see
EntityTest

Table of Contents

Interfaces

JsonSerializable

Properties

$attributes  : array<string, mixed>
Holds the current values of all class vars.
$castHandlers  : array<string, string>
Custom convert handlers.
$casts  : array<string, string>
Array of field names and the type of value to cast them as when they are accessed.
$dataCaster  : DataCaster|null
The data caster.
$datamap  : array<string, string>
Maps names used in sets and gets against unique names within the class, allowing independence from database column names.
$dates  : array<int, string>
The date fields.
$original  : array<string, 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.
$_onlyScalars  : bool
Indicates whether all attributes are scalars (for optimization).
$defaultCastHandlers  : array<string, string>
Default convert handlers.

Methods

__construct()  : mixed
Allows filling in Entity parameters during construction.
__get()  : array<int|string, mixed>|bool|float|int|object|string|null
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()  : void
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.
injectRawData()  : $this
Set raw data array without any mutations.
jsonSerialize()  : array<string, mixed>
Support for json_encode().
syncOriginal()  : $this
Ensures our "original" values match the current values.
toArray()  : array<string, 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, mixed>
Returns the raw values of the current attributes.
castAs()  : array<int|string, mixed>|bool|float|int|object|string|null
Provides the ability to cast an item as a specific data type.
dataCaster()  : DataCaster|null
Returns a DataCaster instance when casts are defined.
hasMappedProperty()  : bool
Whether this key has mapped property?
isMappedDbColumn()  : bool
Whether this key is mapped db column name?
mapProperty()  : string
Checks the datamap to see if this property name is being mapped, and returns the DB column name, if any, or the original property name.
mutateDate()  : Time
Converts the given string|timestamp|DateTimeInterface instance into the "CodeIgniter\I18n\Time" object.
containsOnlyEntities()  : bool
Checks if an array contains only Entity instances.
normalizeValue()  : mixed
Recursively normalize a value for comparison.

Properties

$attributes

Holds the current values of all class vars.

protected array<string, 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 array<string, string> $casts = []

$datamap

Maps names used in sets and gets against unique names within the class, allowing independence from database column names.

protected array<string, string> $datamap = []

Example: $datamap = [ 'class_property_name' => 'db_column_name' ];

$dates

The date fields.

protected array<int, string> $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, mixed> $original = []

$_cast

Holds info whenever properties have to be casted.

private bool $_cast = true

$_onlyScalars

Indicates whether all attributes are scalars (for optimization).

private bool $_onlyScalars = true

$defaultCastHandlers

Default convert handlers.

private array<string, string> $defaultCastHandlers = ['array' => \CodeIgniter\Entity\Cast\ArrayCast::class, 'bool' => \CodeIgniter\Entity\Cast\BooleanCast::class, 'boolean' => \CodeIgniter\Entity\Cast\BooleanCast::class, 'csv' => \CodeIgniter\Entity\Cast\CSVCast::class, 'datetime' => \CodeIgniter\Entity\Cast\DatetimeCast::class, 'double' => \CodeIgniter\Entity\Cast\FloatCast::class, 'enum' => \CodeIgniter\Entity\Cast\EnumCast::class, 'float' => \CodeIgniter\Entity\Cast\FloatCast::class, 'int' => \CodeIgniter\Entity\Cast\IntegerCast::class, 'integer' => \CodeIgniter\Entity\Cast\IntegerCast::class, 'int-bool' => \CodeIgniter\Entity\Cast\IntBoolCast::class, 'json' => \CodeIgniter\Entity\Cast\JsonCast::class, 'object' => \CodeIgniter\Entity\Cast\ObjectCast::class, 'string' => \CodeIgniter\Entity\Cast\StringCast::class, 'timestamp' => \CodeIgniter\Entity\Cast\TimestampCast::class, 'uri' => \CodeIgniter\Entity\Cast\URICast::class]

Methods

__construct()

Allows filling in Entity parameters during construction.

public __construct([array<string, mixed> $data = null ]) : mixed
Parameters
$data : array<string, mixed> = null

__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) : array<int|string, mixed>|bool|float|int|object|string|null

Examples: $p = $this->my_property $p = $this->getMyProperty()

Parameters
$key : string
Tags
throws
Exception
Return values
array<int|string, mixed>|bool|float|int|object|string|null

__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[, array<int|string, mixed>|bool|float|int|object|string|null $value = null ]) : void

Examples: $this->my_property = $p; $this->setMyProperty() = $p;

Parameters
$key : string
$value : array<int|string, mixed>|bool|float|int|object|string|null = null
Tags
throws
Exception

__unset()

Unsets an attribute property.

public __unset(string $key) : void
Parameters
$key : string

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, array<int|string, mixed>|bool|float|int|object|string|null> $data = null ]) : $this
Parameters
$data : array<string, array<int|string, mixed>|bool|float|int|object|string|null> = 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|null $key = null ]) : bool
Parameters
$key : string|null = null
Return values
bool

injectRawData()

Set raw data array without any mutations.

public injectRawData(array<string, mixed> $data) : $this
Parameters
$data : array<string, mixed>
Return values
$this

jsonSerialize()

Support for json_encode().

public jsonSerialize() : array<string, mixed>
Return values
array<string, mixed>

syncOriginal()

Ensures our "original" values match the current values.

public syncOriginal() : $this

Objects and arrays are normalized and JSON-encoded for reliable change detection, while scalars are stored as-is for performance.

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, mixed>
Parameters
$onlyChanged : bool = false

If true, only return values that have changed since object creation.

$cast : bool = true

If true, properties will be cast.

$recursive : bool = false

If true, inner entities will be cast as array as well.

Return values
array<string, mixed>

toRawArray()

Returns the raw values of the current attributes.

public toRawArray([bool $onlyChanged = false ][, bool $recursive = false ]) : array<string, 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 cast as array as well.

Return values
array<string, mixed>

castAs()

Provides the ability to cast an item as a specific data type.

protected castAs(bool|float|int|string|null $value, string $attribute[, string $method = 'get' ]) : array<int|string, mixed>|bool|float|int|object|string|null

Add ? at the beginning of the type (i.e. ?string) to get null instead of casting $value when $value is null.

Parameters
$value : bool|float|int|string|null

Attribute value

$attribute : string

Attribute name

$method : string = 'get'

Allowed to "get" and "set"

Tags
throws
CastException
Return values
array<int|string, mixed>|bool|float|int|object|string|null

dataCaster()

Returns a DataCaster instance when casts are defined.

protected dataCaster() : DataCaster|null

If no casts are configured, no DataCaster is created and null is returned.

Return values
DataCaster|null

hasMappedProperty()

Whether this key has mapped property?

protected hasMappedProperty(string $key) : bool
Parameters
$key : string
Return values
bool

isMappedDbColumn()

Whether this key is mapped db column name?

protected isMappedDbColumn(string $key) : bool
Parameters
$key : string
Return values
bool

mapProperty()

Checks the datamap to see if this property name is being mapped, and returns the DB column name, if any, or the original property name.

protected mapProperty(string $key) : string
Parameters
$key : string
Return values
string

Database column name.

mutateDate()

Converts the given string|timestamp|DateTimeInterface instance into the "CodeIgniter\I18n\Time" object.

protected mutateDate(DateTimeInterface|float|int|string $value) : Time
Parameters
$value : DateTimeInterface|float|int|string
Tags
throws
Exception
Return values
Time

containsOnlyEntities()

Checks if an array contains only Entity instances.

private containsOnlyEntities(array<int|string, mixed> $data) : bool

This allows optimization for per-entity change tracking.

Parameters
$data : array<int|string, mixed>
Return values
bool

normalizeValue()

Recursively normalize a value for comparison.

private normalizeValue(mixed $data) : mixed

Converts objects and arrays to a JSON-encodable format.

Parameters
$data : mixed

        
On this page

Search results