Skip to content

Commit

Permalink
Extracted entity behaviours to separate class
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrian Miu committed Feb 26, 2020
1 parent e0a427c commit e393923
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 38 deletions.
44 changes: 44 additions & 0 deletions src/Entity/Behaviours.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
declare(strict_types=1);

namespace Sirius\Orm\Entity;

use Sirius\Orm\Behaviour\BehaviourInterface;
use Sirius\Orm\Helpers\Str;

class Behaviours
{

protected $list = [];

public function add(BehaviourInterface $behaviour)
{
$this->list[$behaviour->getName()] = $behaviour;
}

public function remove($name)
{
unset($this->list[$name]);
}

public function without(...$names)
{
$clone = clone $this;
foreach ($names as $name) {
$clone->remove($name);
}
return $clone;
}

public function apply($mapper, $target, $result, ...$args)
{
foreach ($this->list as $behaviour) {
$method = 'on' . Str::className($target);
if (method_exists($behaviour, $method)) {
$result = $behaviour->{$method}($mapper, $result, ...$args);
}
}

return $result;
}
}
49 changes: 11 additions & 38 deletions src/Mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Sirius\Orm\Behaviour\BehaviourInterface;
use Sirius\Orm\Collection\Collection;
use Sirius\Orm\Collection\PaginatedCollection;
use Sirius\Orm\Entity\Behaviours;
use Sirius\Orm\Entity\EntityInterface;
use Sirius\Orm\Entity\GenericEntity;
use Sirius\Orm\Entity\GenericEntityHydrator;
Expand Down Expand Up @@ -89,10 +90,9 @@ class Mapper
protected $entityDefaultAttributes = [];

/**
* List of behaviours to be attached to the mapper
* @var BehaviourInterface[]
* @var Behaviours
*/
protected $behaviours = [];
protected $behaviours;

/**
* @var array
Expand Down Expand Up @@ -166,6 +166,7 @@ public function __construct(Orm $orm, HydratorInterface $entityHydrator = null,
$queryBuilder = QueryBuilder::getInstance();
}
$this->queryBuilder = $queryBuilder;
$this->behaviours = new Behaviours();
}

public function __call(string $method, array $params)
Expand All @@ -191,29 +192,15 @@ public function __call(string $method, array $params)
*/
public function use(...$behaviours)
{
if (empty($behaviours)) {
return;
}
foreach ($behaviours as $behaviour) {
/** @var $behaviour BehaviourInterface */
if (isset($this->behaviours[$behaviour->getName()])) {
throw new \BadMethodCallException(
sprintf('Behaviour "%s" is already registered', $behaviour->getName())
);
}
$this->behaviours[$behaviour->getName()] = $behaviour;
$this->behaviours->add($behaviour);
}
}

public function without(...$behaviours)
{
if (empty($behaviours)) {
return $this;
}
$mapper = clone $this;
foreach ($behaviours as $behaviour) {
unset($mapper->behaviours[$behaviour]);
}
$mapper->behaviours = $this->behaviours->without(...$behaviours);

return $mapper;
}
Expand Down Expand Up @@ -331,14 +318,14 @@ public function newEntity(array $data): EntityInterface
{
$entity = $this->entityHydrator->hydrate(array_merge($this->getEntityDefaults(), $data));

return $this->applyBehaviours(__FUNCTION__, $entity);
return $this->behaviours->apply($this, __FUNCTION__, $entity);
}

public function extractFromEntity(EntityInterface $entity): array
{
$data = $this->entityHydrator->extract($entity);

return $this->applyBehaviours(__FUNCTION__, $data);
return $this->behaviours->apply($this, __FUNCTION__, $data);
}

public function newEntityFromRow(array $data = null, array $load = [], Tracker $tracker = null)
Expand Down Expand Up @@ -408,8 +395,6 @@ protected function injectRelations(EntityInterface $entity, Tracker $tracker, ar
}

if (array_key_exists($name, $eagerLoad) || in_array($name, $eagerLoad) || $relation->isEagerLoad()) {
#$relation->attachLazyRelationToEntity($entity, $tracker);
#$this->getEntityAttribute($entity, $name);
$relation->attachMatchesToEntity($entity, $tracker->getResultsForRelation($name));
} elseif ($relation->isLazyLoad()) {
$relation->attachLazyRelationToEntity($entity, $tracker);
Expand Down Expand Up @@ -493,7 +478,7 @@ public function newQuery(): Query
{
$query = $this->queryBuilder->newQuery($this);

return $this->applyBehaviours(__FUNCTION__, $query);
return $this->behaviours->apply($this, __FUNCTION__, $query);
}

public function find($pk, array $load = [])
Expand Down Expand Up @@ -537,7 +522,7 @@ public function newSaveAction(EntityInterface $entity, $options): Update
$action = new Update($this, $entity, $options);
}

return $this->applyBehaviours('save', $action);
return $this->behaviours->apply($this, 'save', $action);
}

public function delete(EntityInterface $entity, $withRelations = true)
Expand All @@ -563,7 +548,7 @@ public function newDeleteAction(EntityInterface $entity, $options)
{
$action = new Delete($this, $entity, $options);

return $this->applyBehaviours('delete', $action);
return $this->behaviours->apply($this, 'delete', $action);
}

protected function assertCanPersistEntity($entity)
Expand All @@ -578,18 +563,6 @@ protected function assertCanPersistEntity($entity)
}
}

protected function applyBehaviours($target, $result, ...$args)
{
foreach ($this->behaviours as $behaviour) {
$method = 'on' . Helpers\Str::className($target);
if (method_exists($behaviour, $method)) {
$result = $behaviour->{$method}($this, $result, ...$args);
}
}

return $result;
}

public function getReadConnection()
{
return $this->orm->getConnectionLocator()->getRead();
Expand Down

0 comments on commit e393923

Please sign in to comment.