CodeIgniter v4.5 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|int, 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
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|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

Methods

__construct()  : mixed
Allows filling in Entity parameters during construction.
__get()  : array<string|int, 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|int, 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()  : array<string|int, mixed>|bool|float|int|object|string|null
Provides the ability to cast an item as a specific data type.
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|DateTime|Time instance into the "CodeIgniter\I18n\Time" object.

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

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

Parameters
$key : string
Tags
throws
Exception
params

string $key class property

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

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

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

class property

Return values
bool

injectRawData()

Set raw data array without any mutations

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

jsonSerialize()

Support for json_encode()

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

setAttributes()

Set raw data array without any mutations

public setAttributes(array<string|int, mixed> $data) : $this
Parameters
$data : array<string|int, mixed>
Tags
deprecated

Use injectRawData() instead.

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 cast.

$recursive : bool = false

If true, inner entities will be cast 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 cast 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(bool|float|int|string|null $value, string $attribute[, string $method = 'get' ]) : array<string|int, 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<string|int, mixed>|bool|float|int|object|string|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

db column name

mutateDate()

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

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

        
On this page

Search results