Class DocumentContext
Provides a context provider for Elasticsearch documents.
Property Summary
-
$_context protected
array
The context data
-
$_isCollection protected
bool
Boolean to track whether or not the entity is a collection.
-
$_request protected
Cake\Http\ServerRequest
The request object.
-
$_rootName protected
string
The name of the top level entity/index object.
Method Summary
-
__construct() public
Constructor.
-
_prepare() protected
Prepare some additional data from the context.
-
attributes() public
Get an associative array of other attributes for a field name.
-
entity() protected
Get the entity that is closest to $path.
-
error() public
Get the errors for a given field
-
fieldNames() public
Get the fieldnames of the top level object in this context.
-
getMaxLength() public @method
Get maximum length of a field from model validation
-
getProp() protected
Read property values or traverse arrays/iterators.
-
getRequiredMessage() public @method
Gets the default "required" error message for a field
-
getValidator() protected
Get the validator for the current index.
-
hasError() public
Check whether or not a field has an error attached to it
-
isCreate() public
Returns whether or not this form is for a create operation.
-
isPrimaryKey() public
Returns true if the passed field name is part of the primary key for this context
-
isRequired() public
Check if a given field is 'required'.
-
primaryKey() public
Get the fields used in the context as a primary key.
-
type() public
Get the abstract field type for a given field name.
-
val() public
Get the current value for a given field.
Method Detail
__construct() ¶ public
__construct(Cake\Http\ServerRequest $request, array $context)
Constructor.
Parameters
-
Cake\Http\ServerRequest
$request The request object.
-
array
$context Context info.
_prepare() ¶ protected
_prepare(): void
Prepare some additional data from the context.
If the table option was provided to the constructor and it was a string, IndexRegistry will be used to get the correct table instance.
If an object is provided as the index option, it will be used as is.
If no index option is provided, the index name will be derived based on naming conventions. This inference will work with a number of common objects like arrays, Collection objects and ResultSets.
Returns
void
Throws
RuntimeException
When a table object cannot be located/inferred.
attributes() ¶ public
attributes(string $field): array
Get an associative array of other attributes for a field name.
Parameters
-
string
$field
Returns
array
entity() ¶ protected
entity(array $path): Cake\Datasource\EntityInterface|false
Get the entity that is closest to $path.
Parameters
-
array
$path The to get an entity for.
Returns
Cake\Datasource\EntityInterface|false
Throws
RuntimeException
when no entity can be found.
error() ¶ public
error(string $field): array
Get the errors for a given field
Parameters
-
string
$field
Returns
array
fieldNames() ¶ public
fieldNames(): string[]
Get the fieldnames of the top level object in this context.
Returns
string[]
getMaxLength() ¶ public @method
getMaxLength(mixed $field): int|null
Get maximum length of a field from model validation
Parameters
-
$field
Returns
int|null
getProp() ¶ protected
getProp(mixed $target, string $field): mixed
Read property values or traverse arrays/iterators.
Parameters
-
mixed
$target The entity/array/collection to fetch $field from.
-
string
$field The next field to fetch.
Returns
mixed
getRequiredMessage() ¶ public @method
getRequiredMessage(mixed $field): string|null
Gets the default "required" error message for a field
Parameters
-
$field
Returns
string|null
getValidator() ¶ protected
getValidator(): Cake\Validation\Validator
Get the validator for the current index.
Returns
Cake\Validation\Validator
hasError() ¶ public
hasError(string $field): bool
Check whether or not a field has an error attached to it
Parameters
-
string
$field
Returns
bool
isCreate() ¶ public
isCreate(): bool
Returns whether or not this form is for a create operation.
Returns
bool
isPrimaryKey() ¶ public
isPrimaryKey(string $field): bool
Returns true if the passed field name is part of the primary key for this context
Parameters
-
string
$field
Returns
bool
isRequired() ¶ public
isRequired(string $field): bool
Check if a given field is 'required'.
In this context class, this is simply defined by the 'required' array.
Parameters
-
string
$field
Returns
bool
primaryKey() ¶ public
primaryKey(): array
Get the fields used in the context as a primary key.
Returns
array
type() ¶ public
type(string $field): string|null
Get the abstract field type for a given field name.
Parameters
-
string
$field
Returns
string|null
val() ¶ public
val(string $field): mixed
Get the current value for a given field.
Classes implementing this method can optionally have a second argument
$options
. Valid key for $options
array are:
default
: Default value to return if no value found in request data or context record.schemaDefault
: Boolean indicating whether default value from context's schema should be used if it's not explicitly provided.
Parameters
-
string
$field
Returns
mixed