fyre / entity
An entity library.
Requires
- fyre/inflector: ^3.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.59
- fyre/datetime: ^3.0
- fyre/php-cs-fixer-config: ^1.0
- phpunit/phpunit: ^11
README
FyreEntity is a free, open-source entity library for PHP.
Table Of Contents
Installation
Using Composer
composer require fyre/entity
In PHP:
use Fyre\Entity\Entity;
Basic Usage
$data
is an array containing the data for populating the entity.$options
is an array containing the options for creating the entity.source
is a string representing the entity source, and will default to null.new
is a boolean indicating whether the entity is new, and will default to true.clean
is a boolean indicating whether to clean the entity after init, and will default to true.
$entity = new Entity($data, $options);
Methods
Clean
Clean the entity.
$entity->clean();
Clear
Clear values from the entity.
$fields
is an array containing the fields to clear.
$entity->clear($fields);
Extract
Extract values from the entity.
$fields
is an array containing the fields to extract.
$values = $entity->extract($fields);
Extract Dirty
Extract dirty values from the entity.
$fields
is an array containing the fields to extract.
$values = $entity->extractDirty($fields);
If the $field
argument is omitted, this method will return all dirty values.
$values = $entity->extractDirty();
Extract Original
Extract original values from the entity.
$fields
is an array containing the fields to extract.
$values = $entity->extractOriginal($fields);
Fill
Fill the entity with values.
$fields
is an array containing the data to fill.$options
is an array containing options for filling the entity.guard
is a boolean indicating whether to check whether the field is accessible, and will default to true.mutate
is a boolean indicating whether to mutate the value, and will default to true.
$entity->fill($data, $options);
If the mutate
option is set to true, and a _setFieldName
method exists in the entity (where the field name is field_name), then that method will be called for each value being set. The argument will be the value being populated, and the return value of that method will be stored in the entity instead.
Fill Invalid
Fill the entity with invalid values.
$fields
is an array containing the data to fill.$overwrite
is a boolean indicating whether to overwrite existing values, and will default to false.
$entity->fillInvalid($data, $overwrite);
Get
Get a value from the entity.
$field
is a string representing the field name.
$value = $entity->get($field);
Alternatively, you can get a value using the magic __get
method or array syntax.
$value = $entity->$field; $value = $entity[$field];
If a _getFieldName
method exists in the entity (where the field name is field_name), then that method will be called for the value being retrieved. The argument of that method will be the value stored in the entity, and the return value of that method will be returned instead.
Get Accessible
Get the accessible fields from the entity.
$accessible = $entity->getAccessible();
Get Dirty
Get the dirty fields from the entity.
$dirty = $entity->getDirty();
Get Error
Get the errors for an entity field.
$field
is a string representing the field name.
$errors = $entity->getError($field);
Get Errors
Get all errors for the entity.
$errors = $entity->getErrors();
Get Hidden
Get the hidden fields from the entity.
$hidden = $entity->getHidden();
Get Invalid
Get invalid value(s) from the entity.
$field
is a string representing the field name.
$value = $entity->getInvalid($field);
If the $field
argument is omitted, this method will return all invalid values.
$invalid = $entity->getInvalid();
Get Original
Get an original value from the entity.
$field
is a string representing the field name.
$value = $entity->getOriginal($field);
If the $field
argument is omitted, this method will return all original values.
$original = $entity->getOriginal();
Get Source
Get the entity source.
$source = $entity->getSource();
Get Virtual
Get the virtual fields from the entity.
$virtual = $entity->getVirtual();
Get Visible
Get the visible fields from the entity.
$visible = $entity->getVisible();
Has
Determine if an entity value is set.
$field
is a string representing the field name.
$has = $entity->has($field);
Alternatively, you can determine if a value is set using the magic __isset
method or array syntax.
$isset = isset($entity->$field); $isset = isset($entity[$field]);
Has Value
Determine if an entity value is not empty.
$field
is a string representing the field name.
$hasValue = $entity->hasValue($field);
Has Errors
Determine if the entity has errors.
$hasErrors = $entity->hasErrors();
Is Accessible
Determine if an entity field is accessible.
$field
is a string representing the field name.
$isAccessible = $entity->isAccessible($field);
Is Dirty
Determine if an entity field is dirty.
$field
is a string representing the field name.
$isDirty = $entity->isDirty($field);
If the $field
argument is omitted, this method will determine whether the entity has any dirty fields.
$isDirty = $entity->isDirty();
Is Empty
Determine if an entity value is empty.
$field
is a string representing the field name.
$isEmpty = $entity->isEmpty($field);
If the $field
argument is omitted, this method will determine whether all entity fields are empty.
$isEmpty = $entity->isEmpty();
Is New
Determine if the entity is new.
$isNew = $entity->isNew();
Restore State
Restore the saved entity state.
$entity->restoreState();
Save State
Save the current entity state.
$entity->saveState();
Set
Set an entity value.
$field
is a string representing the field name.$value
is the value to set.$options
is an array containing options for filling the entity.guard
is a boolean indicating whether to check whether the field is accessible, and will default to true.mutate
is a boolean indicating whether to mutate the value, and will default to true.
$entity->set($field, $value, $options);
Alternatively, you can set a value using the magic __set
method or array syntax.
$entity->$field = $value; $entity[$field] = $value;
If the mutate
option is set to true, and a _setFieldName
method exists in the entity (where the field name is field_name), then that method will be called for the value being set. The argument will be the value being populated, and the return value of that method will be stored in the entity instead.
Set Access
Set whether a field is accessible.
$field
is a string representing the field name.$accessible
is a boolean indicating whether the field is accessible.
$entity->setAccess($field, $accessible);
Set Dirty
Set whether a field is dirty.
$field
is a string representing the field name.$dirty
is a boolean indicating whether the field is dirty, and will default to true.
$entity->setDirty($field, $dirty);
Set Error
Set errors for an entity field.
$field
is a string representing the field name.$error
is a string or array containing the errors.$overwrite
is a boolean indicating whether to overwrite existing errors, and will default to false.
$entity->setError($field, $error, $ovewrite);
Set Errors
Set all errors for the entity.
$errors
is an array containing the errors.$overwrite
is a boolean indicating whether to overwrite existing errors, and will default to false.
$entity->setErrors($errors, $overwrite);
Set Hidden
Set hidden fields.
$field
is an array containing the field names.$merge
is a boolean indicating whether to merge with existing fields.
$entity->setHidden($fields, $merge);
Set Invalid
Set an invalid value.
$field
is a string representing the field name.$value
is the value to set.$overwrite
is a boolean indicating whether to overwrite existing errors, and will default to true.
$entity->setInvalid($field, $value, $overwrite);
Set New
Set whether the entity is new.
$new
is a boolean whether the entity is new, and will default to true.
$entity->setNew($new);
Set Source
Set the entity source.
$source
is a string representing the source.
$entity->setSource($source);
Set Virtual
Set virtual fields.
$field
is an array containing the field names.$merge
is a boolean indicating whether to merge with existing fields.
$entity->setVirtual($fields, $merge);
To Array
Convert the entity to an array.
$array = $entity->toArray();
To JSON
Convert the entity to a JSON string.
$json = $entity->toJson();
Alternatively, you can cast the value to a string using the magic __toString
method.
$json = (string) $entity;
Unset
Unset an entity value.
$field
is a string representing the field name.
$entity->unset($field);
Alternatively, you can unset a value using the magic __unset
method or array syntax.
unset($entity->$field); unset($entity[$field]);
Entity Locators
use Fyre\Entity\EntityLocator;
$inflector
is an Inflector.$namespaces
is an array containing the namespaces.
$entityLocator = new EntityLocator($inflector, $namespaces);
Add Namespace
Add a namespace for locating entities.
$namespace
is a string representing the namespace.
$entityLocator->addNamespace($namespace);
Clear
Clear all namespaces and entities.
$entityLocator->clear();
Find
Find the entity class name for an alias.
$alias
is a string representing the alias.
$className = $entityLocator->find($alias);
Find Alias
Find the alias for an entity class name.
$className
is a string representing the entity class name.
$alias = $entityLocator->findAlias($className);
Get Default Entity Class
Get the default entity class name.
$defaultEntityClass = $entityLocator->getDefaultEntityClass();
Get Namespaces
Get the namespaces.
$namespaces = $entityLocator->getNamespaces();
Has Namespace
Check if a namespace exists.
$namespace
is a string representing the namespace.
$hasNamespace = $entityLocator->hasNamespace($namespace);
Map
Map an alias to an entity class name.
$alias
is a string representing the alias.$className
is a string representing the entity class name.
$entityLocator->map($alias, $className);
Remove Namespace
Remove a namespace.
$namespace
is a string representing the namespace.
$entityLocator->removeNamespace($namespace);
Set Default Entity Class
Set the default entity class name.
$defaultEntityClass
is a string representing the default entity class name.
$entityLocator->setDefaultEntityClass($defaultEntityClass);