EntityContext
in package
implements
ContextInterface
uses
LocatorAwareTrait
Provides a form context around a single entity and its relations.
It also can be used as context around an array or iterator of entities.
This class lets FormHelper interface with entities or collections of entities.
Important Keys:
-
entity
The entity this context is operating on. -
table
Either the ORM\Table instance to fetch schema/validators from, an array of table instances in the case of a form spanning multiple entities, or the name(s) of the table. If this is null the table name(s) will be determined using naming conventions. -
validator
Either the Validation\Validator to use, or the name of the validation method to call on the table object. For example 'default'. Defaults to 'default'. Can be an array of table alias=>validators when dealing with associated forms.
Table of Contents
Interfaces
- ContextInterface
- Interface for FormHelper context implementations.
Properties
- $_context : array<string, mixed>
- Context data for this object.
- $_isCollection : bool
- Boolean to track whether the entity is a collection.
- $_rootName : string
- The name of the top level entity/table object.
- $_tableLocator : LocatorInterface|null
- Table locator instance
- $_tables : array<string|int, Table>
- A dictionary of tables
- $_validator : array<string|int, Validator>
- Dictionary of validators.
- $defaultTable : string|null
- This object's default table alias.
Methods
- __construct() : mixed
- Constructor.
- attributes() : array<string|int, mixed>
- Get an associative array of other attributes for a field name.
- entity() : EntityInterface|iterable<string|int, mixed>|null
- Fetch the entity or data value for a given path
- error() : array<string|int, mixed>
- Get the errors for a given field
- fetchTable() : Table
- Convenience method to get a table instance.
- fieldNames() : array<string|int, string>
- Get the field names from the top level entity.
- getMaxLength() : int|null
- Get field length from validation
- getPrimaryKey() : array<string|int, string>
- Get the primary key data for the context.
- getRequiredMessage() : string|null
- Gets the default "required" error message for a field
- getTableLocator() : LocatorInterface
- Gets the table locator.
- hasError() : bool
- Check whether a field has an error attached to it
- isCreate() : bool
- Check whether this form is a create or update.
- isPrimaryKey() : bool
- Returns true if the passed field name is part of the primary key for this context
- isRequired() : bool|null
- Check if a field should be marked as required.
- primaryKey() : array<string|int, string>
- Get the primary key data for the context.
- setTableLocator() : $this
- Sets the table locator.
- type() : string|null
- Get the abstract field type for a given field name.
- val() : mixed
- Get the value for a given path.
- _extractMultiple() : array<string|int, mixed>|null
- Helper method used to extract all the primary key values out of an array, The primary key column is guessed out of the provided $path array
- _getProp() : mixed
- Read property values or traverse arrays/iterators.
- _getTable() : Table|null
- Get the table instance from a property path
- _getValidator() : Validator
- Get the validator associated to an entity based on naming conventions.
- _prepare() : void
- Prepare some additional data from the context.
- _schemaDefault() : mixed
- Get default value from table schema for given entity field.
- leafEntity() : array<string|int, mixed>
- Fetch the terminal or leaf entity for the given path.
Properties
$_context
Context data for this object.
protected
array<string, mixed>
$_context
$_isCollection
Boolean to track whether the entity is a collection.
protected
bool
$_isCollection
= false
$_rootName
The name of the top level entity/table object.
protected
string
$_rootName
$_tableLocator
Table locator instance
protected
LocatorInterface|null
$_tableLocator
$_tables
A dictionary of tables
protected
array<string|int, Table>
$_tables
= []
$_validator
Dictionary of validators.
protected
array<string|int, Validator>
$_validator
= []
$defaultTable
This object's default table alias.
protected
string|null
$defaultTable
= null
Methods
__construct()
Constructor.
public
__construct(array<string, mixed> $context) : mixed
Parameters
- $context : array<string, mixed>
-
Context info.
attributes()
Get an associative array of other attributes for a field name.
public
attributes(string $field) : array<string|int, mixed>
Parameters
- $field : string
-
A dot separated path to get additional data on.
Return values
array<string|int, mixed> —An array of data describing the additional attributes on a field.
entity()
Fetch the entity or data value for a given path
public
entity([array<string|int, mixed>|null $path = null ]) : EntityInterface|iterable<string|int, mixed>|null
This method will traverse the given path and find the entity or array value for a given path.
If you only want the terminal Entity for a path use leafEntity
instead.
Parameters
- $path : array<string|int, mixed>|null = null
-
Each one of the parts in a path for a field name or null to get the entity passed in constructor context.
Tags
Return values
EntityInterface|iterable<string|int, mixed>|nullerror()
Get the errors for a given field
public
error(string $field) : array<string|int, mixed>
Parameters
- $field : string
-
A dot separated path to check errors on.
Return values
array<string|int, mixed> —An array of errors.
fetchTable()
Convenience method to get a table instance.
public
fetchTable([string|null $alias = null ][, array<string, mixed> $options = [] ]) : Table
Parameters
- $alias : string|null = null
-
The alias name you want to get. Should be in CamelCase format. If
null
then the value of $defaultTable property is used. - $options : array<string, mixed> = []
-
The options you want to build the table with. If a table has already been loaded the registry options will be ignored.
Tags
Return values
TablefieldNames()
Get the field names from the top level entity.
public
fieldNames() : array<string|int, string>
If the context is for an array of entities, the 0th index will be used.
Return values
array<string|int, string> —Array of field names in the table/entity.
getMaxLength()
Get field length from validation
public
getMaxLength(string $field) : int|null
Parameters
- $field : string
-
The dot separated path to the field you want to check.
Return values
int|nullgetPrimaryKey()
Get the primary key data for the context.
public
getPrimaryKey() : array<string|int, string>
Gets the primary key columns from the root entity's schema.
Return values
array<string|int, string>getRequiredMessage()
Gets the default "required" error message for a field
public
getRequiredMessage(string $field) : string|null
Parameters
- $field : string
-
A dot separated path to the field name
Tags
Return values
string|nullgetTableLocator()
Gets the table locator.
public
getTableLocator() : LocatorInterface
Return values
LocatorInterfacehasError()
Check whether a field has an error attached to it
public
hasError(string $field) : bool
Parameters
- $field : string
-
A dot separated path to check errors on.
Return values
bool —Returns true if the errors for the field are not empty.
isCreate()
Check whether this form is a create or update.
public
isCreate() : bool
If the context is for a single entity, the entity's isNew() method will be used. If isNew() returns null, a create operation will be assumed.
If the context is for a collection or array the first object in the collection will be used.
Return values
boolisPrimaryKey()
Returns true if the passed field name is part of the primary key for this context
public
isPrimaryKey(string $field) : bool
Parameters
- $field : string
-
A dot separated path to the field a value is needed for.
Tags
Return values
boolisRequired()
Check if a field should be marked as required.
public
isRequired(string $field) : bool|null
Parameters
- $field : string
-
The dot separated path to the field you want to check.
Return values
bool|nullprimaryKey()
Get the primary key data for the context.
public
primaryKey() : array<string|int, string>
Gets the primary key columns from the root entity's schema.
Return values
array<string|int, string>setTableLocator()
Sets the table locator.
public
setTableLocator(LocatorInterface $tableLocator) : $this
Parameters
- $tableLocator : LocatorInterface
-
LocatorInterface instance.
Return values
$thistype()
Get the abstract field type for a given field name.
public
type(string $field) : string|null
Parameters
- $field : string
-
A dot separated path to get a schema type for.
Tags
Return values
string|null —An abstract data type or null.
val()
Get the value for a given path.
public
val(string $field[, array<string, mixed> $options = [] ]) : mixed
Traverses the entity data and finds the value for $path.
Parameters
- $field : string
-
The dot separated path to the value.
- $options : array<string, mixed> = []
-
Options:
-
default
: Default value to return if no value found in data or entity. -
schemaDefault
: Boolean indicating whether default value from table schema should be used if it's not explicitly provided.
-
Return values
mixed —The value of the field or null on a miss.
_extractMultiple()
Helper method used to extract all the primary key values out of an array, The primary key column is guessed out of the provided $path array
protected
_extractMultiple(mixed $values, array<string|int, string> $path) : array<string|int, mixed>|null
Parameters
- $values : mixed
-
The list from which to extract primary keys from
- $path : array<string|int, string>
-
Each one of the parts in a path for a field name
Return values
array<string|int, mixed>|null_getProp()
Read property values or traverse arrays/iterators.
protected
_getProp(mixed $target, string $field) : mixed
Parameters
- $target : mixed
-
The entity/array/collection to fetch $field from.
- $field : string
-
The next field to fetch.
_getTable()
Get the table instance from a property path
protected
_getTable(EntityInterface|array<string|int, string>|string $parts[, bool $fallback = true ]) : Table|null
Parameters
- $parts : EntityInterface|array<string|int, string>|string
-
Each one of the parts in a path for a field name
- $fallback : bool = true
-
Whether to fallback to the last found table when a nonexistent field/property is being encountered.
Return values
Table|null —Table instance or null
_getValidator()
Get the validator associated to an entity based on naming conventions.
protected
_getValidator(array<string|int, mixed> $parts) : Validator
Parameters
- $parts : array<string|int, mixed>
-
Each one of the parts in a path for a field name
Tags
Return values
Validator_prepare()
Prepare some additional data from the context.
protected
_prepare() : void
If the table option was provided to the constructor and it was a string, TableLocator will be used to get the correct table instance.
If an object is provided as the table option, it will be used as is.
If no table option is provided, the table name will be derived based on naming conventions. This inference will work with a number of common objects like arrays, Collection objects and ResultSets.
Tags
_schemaDefault()
Get default value from table schema for given entity field.
protected
_schemaDefault(array<string|int, string> $parts) : mixed
Parameters
- $parts : array<string|int, string>
-
Each one of the parts in a path for a field name
leafEntity()
Fetch the terminal or leaf entity for the given path.
protected
leafEntity([array<string|int, mixed>|null $path = null ]) : array<string|int, mixed>
Traverse the path until an entity cannot be found. Lists containing entities will be traversed if the first element contains an entity. Otherwise, the containing Entity will be assumed to be the terminal one.
Parameters
- $path : array<string|int, mixed>|null = null
-
Each one of the parts in a path for a field name or null to get the entity passed in constructor context.
Tags
Return values
array<string|int, mixed> —Containing the found entity, and remaining un-matched path.