PHP Class CRUDlex\AbstractData

Mostrar archivo Open project: philiplb/crudlex Class Usage Examples

Protected Properties

Property Type Description
$definition Holds the {@see EntityDefinition} entity definition.
$events Holds the events.
$fileProcessor Holds the {@see FileProcessorInterface} file processor.

Public Methods

Method Description
countBy ( string $table, array $params, array $paramsOperators, boolean $excludeDeleted ) : integer Retrieves the amount of entities in the datasource fulfilling the given parameters.
create ( Entity $entity ) : boolean Persists the given entity as new entry in the datasource.
createEmpty ( ) : Entity Creates a new, empty entity instance having all fields prefilled with null or the defined value in case of fixed fields.
createFiles ( Request $request, Entity $entity, string $entityName ) : boolean Creates the uploaded files of a newly created entity.
delete ( Entity $entity ) : integer Deletes an entry from the datasource.
deleteFile ( Entity $entity, string $entityName, string $field ) : boolean Deletes a specific file from an existing entity.
deleteFiles ( Entity $entity, string $entityName ) : boolean Deletes all files of an existing entity.
fetchReferences ( array &$entities = null ) : void Adds the id and name of referenced entities to the given entities. Each reference field is before the raw id of the referenced entity and after the fetch, it's an array with the keys id and name.
get ( string $id ) : void Gets the entity with the given id.
getDefinition ( ) : EntityDefinition Gets the {@see EntityDefinition} instance.
getIdToNameMap ( string $entity, string $nameField ) : array Gets ids and names of a table. Used for building up the dropdown box of reference type fields for example.
hasManySet ( string $field, array $thatIds, string | null $excludeId = null ) : boolean Checks whether a given set of ids is assigned to any entity exactly like it is given (no subset, no superset).
listEntries ( array $filter = [], array $filterOperators = [], integer | null $skip = null, integer | null $amount = null, string | null $sortField = null, boolean | null $sortAscending = null ) : Entity[] Gets a list of entities fullfilling the given filter or all if no selection was given.
popEvent ( string $moment, string $action ) : Closure | null Removes and returns the latest event for the given parameters.
pushEvent ( string $moment, string $action, Closure $function ) Adds an event to fire for the given parameters. The event function must have this signature: function (Entity $entity) and has to return true or false.
renderFile ( Entity $entity, string $entityName, string $field ) : Response Renders (outputs) a file of an entity. This includes setting headers like the file size, mimetype and name, too.
update ( Entity $entity ) : void Updates an existing entry in the datasource having the same id.
updateFiles ( Request $request, Entity $entity, string $entityName ) : boolean Updates the uploaded files of an updated entity.

Protected Methods

Method Description
doDelete ( Entity $entity, boolean $deleteCascade ) : integer Performs the actual deletion.
enrichEntityWithMetaData ( mixed $id, Entity $entity ) Enriches an entity with metadata: id, version, created_at, updated_at
hydrate ( array $row ) : Entity Creates an {@see Entity} from the raw data array with the field name as keys and field values as values.
performOnFiles ( Entity $entity, string $entityName, Closure $function ) Executes a function for each file field of this entity.
shouldExecuteEvents ( Entity $entity, string $moment, string $action ) : boolean Executes the event chain of an entity.

Method Details

countBy() abstract public method

Retrieves the amount of entities in the datasource fulfilling the given parameters.
abstract public countBy ( string $table, array $params, array $paramsOperators, boolean $excludeDeleted ) : integer
$table string the table to count in
$params array an array with the field names as keys and field values as values
$paramsOperators array the operators of the parameters like "=" defining the full condition of the field
$excludeDeleted boolean false, if soft deleted entries in the datasource should be counted, too
return integer the count fulfilling the given parameters

create() abstract public method

Persists the given entity as new entry in the datasource.
abstract public create ( Entity $entity ) : boolean
$entity Entity the entity to persist
return boolean true on successful creation

createEmpty() public method

Creates a new, empty entity instance having all fields prefilled with null or the defined value in case of fixed fields.
public createEmpty ( ) : Entity
return Entity the newly created entity

createFiles() public method

Creates the uploaded files of a newly created entity.
public createFiles ( Request $request, Entity $entity, string $entityName ) : boolean
$request Symfony\Component\HttpFoundation\Request the HTTP request containing the file data
$entity Entity the just created entity
$entityName string the name of the entity as this class here is not aware of it
return boolean true if all before events passed

delete() public method

Deletes an entry from the datasource.
public delete ( Entity $entity ) : integer
$entity Entity the entity to delete
return integer returns one of: - AbstractData::DELETION_SUCCESS -> successful deletion - AbstractData::DELETION_FAILED_STILL_REFERENCED -> failed deletion due to existing references - AbstractData::DELETION_FAILED_EVENT -> failed deletion due to a failed before delete event

deleteFile() public method

Deletes a specific file from an existing entity.
public deleteFile ( Entity $entity, string $entityName, string $field ) : boolean
$entity Entity the entity to delete the file from
$entityName string the name of the entity as this class here is not aware of it
$field string the field of the entity containing the file to be deleted
return boolean true on successful deletion

deleteFiles() public method

Deletes all files of an existing entity.
public deleteFiles ( Entity $entity, string $entityName ) : boolean
$entity Entity the entity to delete the files from
$entityName string the name of the entity as this class here is not aware of it
return boolean true on successful deletion

doDelete() abstract protected method

Performs the actual deletion.
abstract protected doDelete ( Entity $entity, boolean $deleteCascade ) : integer
$entity Entity the id of the entry to delete
$deleteCascade boolean whether to delete children and subchildren
return integer true on successful deletion

enrichEntityWithMetaData() protected method

Enriches an entity with metadata: id, version, created_at, updated_at
protected enrichEntityWithMetaData ( mixed $id, Entity $entity )
$id mixed the id of the entity to enrich
$entity Entity the entity to enrich

fetchReferences() abstract public method

Adds the id and name of referenced entities to the given entities. Each reference field is before the raw id of the referenced entity and after the fetch, it's an array with the keys id and name.
abstract public fetchReferences ( array &$entities = null ) : void
$entities array
return void

get() abstract public method

Gets the entity with the given id.
abstract public get ( string $id ) : void
$id string the id
return void the entity belonging to the id or null if not existant

getDefinition() public method

Gets the {@see EntityDefinition} instance.
public getDefinition ( ) : EntityDefinition
return EntityDefinition the definition instance

getIdToNameMap() abstract public method

Gets ids and names of a table. Used for building up the dropdown box of reference type fields for example.
abstract public getIdToNameMap ( string $entity, string $nameField ) : array
$entity string the entity
$nameField string the field defining the name of the rows
return array an array with the ids as key and the names as values

hasManySet() abstract public method

Checks whether a given set of ids is assigned to any entity exactly like it is given (no subset, no superset).
abstract public hasManySet ( string $field, array $thatIds, string | null $excludeId = null ) : boolean
$field string the many field
$thatIds array the id set to check
$excludeId string | null one optional own id to exclude from the check
return boolean true if the set of ids exists for an entity

hydrate() protected method

Creates an {@see Entity} from the raw data array with the field name as keys and field values as values.
protected hydrate ( array $row ) : Entity
$row array the array with the raw data
return Entity the entity containing the array data then

listEntries() abstract public method

Gets a list of entities fullfilling the given filter or all if no selection was given.
abstract public listEntries ( array $filter = [], array $filterOperators = [], integer | null $skip = null, integer | null $amount = null, string | null $sortField = null, boolean | null $sortAscending = null ) : Entity[]
$filter array the filter all resulting entities must fulfill, the keys as field names
$filterOperators array the operators of the filter like "=" defining the full condition of the field
$skip integer | null if given and not null, it specifies the amount of rows to skip
$amount integer | null if given and not null, it specifies the maximum amount of rows to retrieve
$sortField string | null if given and not null, it specifies the field to sort the entries
$sortAscending boolean | null if given and not null, it specifies that the sort order is ascending, descending else
return Entity[] the entities fulfilling the filter or all if no filter was given

performOnFiles() protected method

Executes a function for each file field of this entity.
protected performOnFiles ( Entity $entity, string $entityName, Closure $function )
$entity Entity the just created entity
$entityName string the name of the entity as this class here is not aware of it
$function Closure the function to perform, takes $entity, $entityName and $field as parameter

popEvent() public method

Removes and returns the latest event for the given parameters.
public popEvent ( string $moment, string $action ) : Closure | null
$moment string the "moment" of the event, can be either "before" or "after"
$action string the "action" of the event, can be either "create", "update" or "delete"
return Closure | null the popped event or null if no event was available.

pushEvent() public method

The events are executed one after another in the added order as long as they return "true". The first event returning "false" will stop the process.
public pushEvent ( string $moment, string $action, Closure $function )
$moment string the "moment" of the event, can be either "before" or "after"
$action string the "action" of the event, can be either "create", "update" or "delete"
$function Closure the event function to be called if set

renderFile() public method

Renders (outputs) a file of an entity. This includes setting headers like the file size, mimetype and name, too.
public renderFile ( Entity $entity, string $entityName, string $field ) : Response
$entity Entity the entity to render the file from
$entityName string the name of the entity as this class here is not aware of it
$field string the field of the entity containing the file to be rendered
return Symfony\Component\HttpFoundation\Response the HTTP response, likely to be a streamed one

shouldExecuteEvents() protected method

Executes the event chain of an entity.
protected shouldExecuteEvents ( Entity $entity, string $moment, string $action ) : boolean
$entity Entity the entity having the event chain to execute
$moment string the "moment" of the event, can be either "before" or "after"
$action string the "action" of the event, can be either "create", "update" or "delete"
return boolean true on successful execution of the full chain or false if it broke at any point (and stopped the execution)

update() abstract public method

Updates an existing entry in the datasource having the same id.
abstract public update ( Entity $entity ) : void
$entity Entity the entity with the new data
return void

updateFiles() public method

Updates the uploaded files of an updated entity.
public updateFiles ( Request $request, Entity $entity, string $entityName ) : boolean
$request Symfony\Component\HttpFoundation\Request the HTTP request containing the file data
$entity Entity the updated entity
$entityName string the name of the entity as this class here is not aware of it
return boolean true on successful update

Property Details

$definition protected_oe property

Holds the {@see EntityDefinition} entity definition.
protected $definition

$events protected_oe property

Holds the events.
protected $events

$fileProcessor protected_oe property

Holds the {@see FileProcessorInterface} file processor.
protected $fileProcessor