@@ -2,7 +2,7 @@ discard block |
||
| 2 | 2 | |
| 3 | 3 | use Analogue\ORM\System\Manager; |
| 4 | 4 | |
| 5 | -if (! function_exists('analogue')) { |
|
| 5 | +if (!function_exists('analogue')) { |
|
| 6 | 6 | |
| 7 | 7 | /** |
| 8 | 8 | * Return analogue's manager instance |
@@ -17,7 +17,7 @@ discard block |
||
| 17 | 17 | } |
| 18 | 18 | |
| 19 | 19 | |
| 20 | -if (! function_exists('mapper')) { |
|
| 20 | +if (!function_exists('mapper')) { |
|
| 21 | 21 | |
| 22 | 22 | /** |
| 23 | 23 | * Create a mapper for a given entity (static alias) |
@@ -4,29 +4,29 @@ |
||
| 4 | 4 | |
| 5 | 5 | if (! function_exists('analogue')) { |
| 6 | 6 | |
| 7 | - /** |
|
| 8 | - * Return analogue's manager instance |
|
| 9 | - * |
|
| 10 | - * @return \Analogue\ORM\System\Manager |
|
| 11 | - */ |
|
| 12 | - function analogue() |
|
| 13 | - { |
|
| 14 | - return Manager::getInstance(); |
|
| 15 | - } |
|
| 7 | + /** |
|
| 8 | + * Return analogue's manager instance |
|
| 9 | + * |
|
| 10 | + * @return \Analogue\ORM\System\Manager |
|
| 11 | + */ |
|
| 12 | + function analogue() |
|
| 13 | + { |
|
| 14 | + return Manager::getInstance(); |
|
| 15 | + } |
|
| 16 | 16 | } |
| 17 | 17 | |
| 18 | 18 | |
| 19 | 19 | if (! function_exists('mapper')) { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * Create a mapper for a given entity (static alias) |
|
| 23 | - * |
|
| 24 | - * @param \Analogue\ORM\Mappable|string $entity |
|
| 25 | - * @param mixed $entityMap |
|
| 26 | - * @return Mapper |
|
| 27 | - */ |
|
| 28 | - function mapper($entity, $entityMap = null) |
|
| 29 | - { |
|
| 30 | - return Manager::getMapper($entity, $entityMap); |
|
| 31 | - } |
|
| 21 | + /** |
|
| 22 | + * Create a mapper for a given entity (static alias) |
|
| 23 | + * |
|
| 24 | + * @param \Analogue\ORM\Mappable|string $entity |
|
| 25 | + * @param mixed $entityMap |
|
| 26 | + * @return Mapper |
|
| 27 | + */ |
|
| 28 | + function mapper($entity, $entityMap = null) |
|
| 29 | + { |
|
| 30 | + return Manager::getMapper($entity, $entityMap); |
|
| 31 | + } |
|
| 32 | 32 | } |
@@ -11,117 +11,117 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | abstract class Wrapper implements InternallyMappable |
| 13 | 13 | { |
| 14 | - /** |
|
| 15 | - * Original Entity Object |
|
| 16 | - * |
|
| 17 | - * @var mixed |
|
| 18 | - */ |
|
| 19 | - protected $entity; |
|
| 20 | - |
|
| 21 | - /** |
|
| 22 | - * Corresponding EntityMap |
|
| 23 | - * |
|
| 24 | - * @var \Analogue\ORM\EntityMap |
|
| 25 | - */ |
|
| 26 | - protected $entityMap; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * Wrapper constructor. |
|
| 30 | - * @param $entity |
|
| 31 | - * @param $entityMap |
|
| 32 | - */ |
|
| 33 | - public function __construct($entity, $entityMap) |
|
| 34 | - { |
|
| 35 | - $this->entity = $entity; |
|
| 36 | - $this->entityMap = $entityMap; |
|
| 37 | - } |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * Return the wrapped entity class |
|
| 41 | - * |
|
| 42 | - * @return mixed |
|
| 43 | - */ |
|
| 44 | - public function getEntityClass() |
|
| 45 | - { |
|
| 46 | - return get_class($this->entity); |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * Returns the wrapped entity |
|
| 51 | - * |
|
| 52 | - * @return mixed |
|
| 53 | - */ |
|
| 54 | - public function getObject() |
|
| 55 | - { |
|
| 56 | - return $this->entity; |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * Returns the wrapped entity's map |
|
| 61 | - * |
|
| 62 | - * @return mixed |
|
| 63 | - */ |
|
| 64 | - public function getMap() |
|
| 65 | - { |
|
| 66 | - return $this->entityMap; |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * Set the lazyloading proxies on the wrapped entity objet |
|
| 71 | - * |
|
| 72 | - * @return void |
|
| 73 | - */ |
|
| 74 | - public function setProxies() |
|
| 75 | - { |
|
| 76 | - $attributes = $this->getEntityAttributes(); |
|
| 77 | - $singleRelations = $this->entityMap->getSingleRelationships(); |
|
| 78 | - $manyRelations = $this->entityMap->getManyRelationships(); |
|
| 79 | - |
|
| 80 | - $proxies = []; |
|
| 81 | - |
|
| 82 | - foreach ($this->entityMap->getRelationships() as $relation) { |
|
| 83 | - if (!array_key_exists($relation, $attributes) || is_null($attributes[$relation])) { |
|
| 84 | - if (in_array($relation, $singleRelations)) { |
|
| 85 | - $proxies[$relation] = new EntityProxy($this->getObject(), $relation); |
|
| 86 | - } |
|
| 87 | - if (in_array($relation, $manyRelations)) { |
|
| 88 | - $proxies[$relation] = new CollectionProxy($this->getObject(), $relation); |
|
| 89 | - } |
|
| 90 | - } |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - foreach ($proxies as $key => $value) { |
|
| 94 | - $this->setEntityAttribute($key, $value); |
|
| 95 | - } |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * @param string $key |
|
| 100 | - * @param string $value |
|
| 101 | - * @return mixed |
|
| 102 | - */ |
|
| 103 | - abstract public function setEntityAttribute($key, $value); |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * @param string $key |
|
| 107 | - * @return mixed |
|
| 108 | - */ |
|
| 109 | - abstract public function getEntityAttribute($key); |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * @param array $attributes |
|
| 113 | - * @return mixed |
|
| 114 | - */ |
|
| 115 | - abstract public function setEntityAttributes(array $attributes); |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * @return mixed |
|
| 119 | - */ |
|
| 120 | - abstract public function getEntityAttributes(); |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * @param string $key |
|
| 124 | - * @return mixed |
|
| 125 | - */ |
|
| 126 | - abstract public function hasAttribute($key); |
|
| 14 | + /** |
|
| 15 | + * Original Entity Object |
|
| 16 | + * |
|
| 17 | + * @var mixed |
|
| 18 | + */ |
|
| 19 | + protected $entity; |
|
| 20 | + |
|
| 21 | + /** |
|
| 22 | + * Corresponding EntityMap |
|
| 23 | + * |
|
| 24 | + * @var \Analogue\ORM\EntityMap |
|
| 25 | + */ |
|
| 26 | + protected $entityMap; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * Wrapper constructor. |
|
| 30 | + * @param $entity |
|
| 31 | + * @param $entityMap |
|
| 32 | + */ |
|
| 33 | + public function __construct($entity, $entityMap) |
|
| 34 | + { |
|
| 35 | + $this->entity = $entity; |
|
| 36 | + $this->entityMap = $entityMap; |
|
| 37 | + } |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * Return the wrapped entity class |
|
| 41 | + * |
|
| 42 | + * @return mixed |
|
| 43 | + */ |
|
| 44 | + public function getEntityClass() |
|
| 45 | + { |
|
| 46 | + return get_class($this->entity); |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * Returns the wrapped entity |
|
| 51 | + * |
|
| 52 | + * @return mixed |
|
| 53 | + */ |
|
| 54 | + public function getObject() |
|
| 55 | + { |
|
| 56 | + return $this->entity; |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * Returns the wrapped entity's map |
|
| 61 | + * |
|
| 62 | + * @return mixed |
|
| 63 | + */ |
|
| 64 | + public function getMap() |
|
| 65 | + { |
|
| 66 | + return $this->entityMap; |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * Set the lazyloading proxies on the wrapped entity objet |
|
| 71 | + * |
|
| 72 | + * @return void |
|
| 73 | + */ |
|
| 74 | + public function setProxies() |
|
| 75 | + { |
|
| 76 | + $attributes = $this->getEntityAttributes(); |
|
| 77 | + $singleRelations = $this->entityMap->getSingleRelationships(); |
|
| 78 | + $manyRelations = $this->entityMap->getManyRelationships(); |
|
| 79 | + |
|
| 80 | + $proxies = []; |
|
| 81 | + |
|
| 82 | + foreach ($this->entityMap->getRelationships() as $relation) { |
|
| 83 | + if (!array_key_exists($relation, $attributes) || is_null($attributes[$relation])) { |
|
| 84 | + if (in_array($relation, $singleRelations)) { |
|
| 85 | + $proxies[$relation] = new EntityProxy($this->getObject(), $relation); |
|
| 86 | + } |
|
| 87 | + if (in_array($relation, $manyRelations)) { |
|
| 88 | + $proxies[$relation] = new CollectionProxy($this->getObject(), $relation); |
|
| 89 | + } |
|
| 90 | + } |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + foreach ($proxies as $key => $value) { |
|
| 94 | + $this->setEntityAttribute($key, $value); |
|
| 95 | + } |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * @param string $key |
|
| 100 | + * @param string $value |
|
| 101 | + * @return mixed |
|
| 102 | + */ |
|
| 103 | + abstract public function setEntityAttribute($key, $value); |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * @param string $key |
|
| 107 | + * @return mixed |
|
| 108 | + */ |
|
| 109 | + abstract public function getEntityAttribute($key); |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * @param array $attributes |
|
| 113 | + * @return mixed |
|
| 114 | + */ |
|
| 115 | + abstract public function setEntityAttributes(array $attributes); |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * @return mixed |
|
| 119 | + */ |
|
| 120 | + abstract public function getEntityAttributes(); |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * @param string $key |
|
| 124 | + * @return mixed |
|
| 125 | + */ |
|
| 126 | + abstract public function hasAttribute($key); |
|
| 127 | 127 | } |
@@ -27,7 +27,7 @@ |
||
| 27 | 27 | /** |
| 28 | 28 | * Return a new Query instance for this driver |
| 29 | 29 | * |
| 30 | - * @return QueryAdapter |
|
| 30 | + * @return IlluminateQueryBuilder |
|
| 31 | 31 | */ |
| 32 | 32 | public function getQuery() |
| 33 | 33 | { |
@@ -10,68 +10,68 @@ |
||
| 10 | 10 | */ |
| 11 | 11 | class IlluminateDBAdapter implements DBAdapter |
| 12 | 12 | { |
| 13 | - /** |
|
| 14 | - * @var Connection |
|
| 15 | - */ |
|
| 16 | - protected $connection; |
|
| 13 | + /** |
|
| 14 | + * @var Connection |
|
| 15 | + */ |
|
| 16 | + protected $connection; |
|
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * IlluminateDBAdapter constructor. |
|
| 20 | - * @param Connection $connection |
|
| 21 | - */ |
|
| 22 | - public function __construct(Connection $connection) |
|
| 23 | - { |
|
| 24 | - $this->connection = $connection; |
|
| 25 | - } |
|
| 18 | + /** |
|
| 19 | + * IlluminateDBAdapter constructor. |
|
| 20 | + * @param Connection $connection |
|
| 21 | + */ |
|
| 22 | + public function __construct(Connection $connection) |
|
| 23 | + { |
|
| 24 | + $this->connection = $connection; |
|
| 25 | + } |
|
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * Return a new Query instance for this driver |
|
| 29 | - * |
|
| 30 | - * @return QueryAdapter |
|
| 31 | - */ |
|
| 32 | - public function getQuery() |
|
| 33 | - { |
|
| 34 | - $connection = $this->connection; |
|
| 27 | + /** |
|
| 28 | + * Return a new Query instance for this driver |
|
| 29 | + * |
|
| 30 | + * @return QueryAdapter |
|
| 31 | + */ |
|
| 32 | + public function getQuery() |
|
| 33 | + { |
|
| 34 | + $connection = $this->connection; |
|
| 35 | 35 | |
| 36 | - $grammar = $connection->getQueryGrammar(); |
|
| 36 | + $grammar = $connection->getQueryGrammar(); |
|
| 37 | 37 | |
| 38 | - return new IlluminateQueryBuilder($connection, $grammar, $connection->getPostProcessor()); |
|
| 39 | - } |
|
| 38 | + return new IlluminateQueryBuilder($connection, $grammar, $connection->getPostProcessor()); |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * Get the date format supported by the current connection |
|
| 43 | - * |
|
| 44 | - * @return string |
|
| 45 | - */ |
|
| 46 | - public function getDateFormat() |
|
| 47 | - { |
|
| 48 | - return $this->connection->getQueryGrammar()->getDateFormat(); |
|
| 49 | - } |
|
| 41 | + /** |
|
| 42 | + * Get the date format supported by the current connection |
|
| 43 | + * |
|
| 44 | + * @return string |
|
| 45 | + */ |
|
| 46 | + public function getDateFormat() |
|
| 47 | + { |
|
| 48 | + return $this->connection->getQueryGrammar()->getDateFormat(); |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * Start a DB transaction on driver that supports it. |
|
| 53 | - * @return void |
|
| 54 | - */ |
|
| 55 | - public function beginTransaction() |
|
| 56 | - { |
|
| 57 | - $this->connection->beginTransaction(); |
|
| 58 | - } |
|
| 51 | + /** |
|
| 52 | + * Start a DB transaction on driver that supports it. |
|
| 53 | + * @return void |
|
| 54 | + */ |
|
| 55 | + public function beginTransaction() |
|
| 56 | + { |
|
| 57 | + $this->connection->beginTransaction(); |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | - /** |
|
| 61 | - * Commit a DB transaction on driver that supports it. |
|
| 62 | - * @return void |
|
| 63 | - */ |
|
| 64 | - public function commit() |
|
| 65 | - { |
|
| 66 | - $this->connection->commit(); |
|
| 67 | - } |
|
| 60 | + /** |
|
| 61 | + * Commit a DB transaction on driver that supports it. |
|
| 62 | + * @return void |
|
| 63 | + */ |
|
| 64 | + public function commit() |
|
| 65 | + { |
|
| 66 | + $this->connection->commit(); |
|
| 67 | + } |
|
| 68 | 68 | |
| 69 | - /** |
|
| 70 | - * Rollback a DB transaction |
|
| 71 | - * @return void |
|
| 72 | - */ |
|
| 73 | - public function rollback() |
|
| 74 | - { |
|
| 75 | - $this->connection->rollBack(); |
|
| 76 | - } |
|
| 69 | + /** |
|
| 70 | + * Rollback a DB transaction |
|
| 71 | + * @return void |
|
| 72 | + */ |
|
| 73 | + public function rollback() |
|
| 74 | + { |
|
| 75 | + $this->connection->rollBack(); |
|
| 76 | + } |
|
| 77 | 77 | } |
@@ -177,7 +177,7 @@ |
||
| 177 | 177 | /** |
| 178 | 178 | * Get a the Underlying QueryAdapter. |
| 179 | 179 | * |
| 180 | - * @return \Analogue\ORM\Drivers\QueryAdapter |
|
| 180 | + * @return \Illuminate\Database\Query\Builder |
|
| 181 | 181 | */ |
| 182 | 182 | public function newQueryBuilder() |
| 183 | 183 | { |
@@ -25,555 +25,555 @@ |
||
| 25 | 25 | */ |
| 26 | 26 | class Mapper |
| 27 | 27 | { |
| 28 | - /** |
|
| 29 | - * The Manager instance |
|
| 30 | - * |
|
| 31 | - * @var \Analogue\ORM\System\Manager |
|
| 32 | - */ |
|
| 33 | - protected $manager; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * Instance of EntityMapper Object |
|
| 37 | - * |
|
| 38 | - * @var \Analogue\ORM\EntityMap |
|
| 39 | - */ |
|
| 40 | - protected $entityMap; |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * The instance of db adapter |
|
| 44 | - * |
|
| 45 | - * @var \Analogue\ORM\Drivers\DBAdapter |
|
| 46 | - */ |
|
| 47 | - protected $adapter; |
|
| 48 | - |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * Event dispatcher instance |
|
| 52 | - * |
|
| 53 | - * @var \Illuminate\Contracts\Events\Dispatcher |
|
| 54 | - */ |
|
| 55 | - protected $dispatcher; |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * Entity Cache |
|
| 59 | - * |
|
| 60 | - * @var \Analogue\ORM\System\EntityCache |
|
| 61 | - */ |
|
| 62 | - protected $cache; |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * Global scopes |
|
| 66 | - * |
|
| 67 | - * @var array |
|
| 68 | - */ |
|
| 69 | - protected $globalScopes = []; |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * Custom Commands |
|
| 73 | - * |
|
| 74 | - * @var array |
|
| 75 | - */ |
|
| 76 | - protected $customCommands = []; |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * @param EntityMap $entityMap |
|
| 80 | - * @param DBAdapter $adapter |
|
| 81 | - * @param Dispatcher $dispatcher |
|
| 82 | - * @param Manager $manager |
|
| 83 | - */ |
|
| 84 | - public function __construct(EntityMap $entityMap, DBAdapter $adapter, Dispatcher $dispatcher, Manager $manager) |
|
| 85 | - { |
|
| 86 | - $this->entityMap = $entityMap; |
|
| 87 | - |
|
| 88 | - $this->adapter = $adapter; |
|
| 89 | - |
|
| 90 | - $this->dispatcher = $dispatcher; |
|
| 91 | - |
|
| 92 | - $this->manager = $manager; |
|
| 93 | - |
|
| 94 | - $this->cache = new EntityCache($entityMap); |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - /** |
|
| 98 | - * Persist an entity or an entity collection into the database |
|
| 99 | - * |
|
| 100 | - * @param Mappable|\Traversable|array $entity |
|
| 101 | - * @throws \InvalidArgumentException |
|
| 102 | - * @throws MappingException |
|
| 103 | - * @return Mappable|\Traversable|array |
|
| 104 | - */ |
|
| 105 | - public function store($entity) |
|
| 106 | - { |
|
| 107 | - if ($this->manager->isTraversable($entity)) { |
|
| 108 | - return $this->storeCollection($entity); |
|
| 109 | - } else { |
|
| 110 | - return $this->storeEntity($entity); |
|
| 111 | - } |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * Store an entity collection inside a single DB Transaction |
|
| 116 | - * |
|
| 117 | - * @param \Traversable|array $entities |
|
| 118 | - * @throws \InvalidArgumentException |
|
| 119 | - * @throws MappingException |
|
| 120 | - * @return \Traversable|array |
|
| 121 | - */ |
|
| 122 | - protected function storeCollection($entities) |
|
| 123 | - { |
|
| 124 | - $this->adapter->beginTransaction(); |
|
| 125 | - |
|
| 126 | - foreach ($entities as $entity) { |
|
| 127 | - $this->storeEntity($entity); |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - $this->adapter->commit(); |
|
| 131 | - |
|
| 132 | - return $entities; |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - /** |
|
| 136 | - * Store a single entity into the database |
|
| 137 | - * |
|
| 138 | - * @param Mappable $entity |
|
| 139 | - * @throws \InvalidArgumentException |
|
| 140 | - * @throws MappingException |
|
| 141 | - * @return \Analogue\ORM\Entity |
|
| 142 | - */ |
|
| 143 | - protected function storeEntity($entity) |
|
| 144 | - { |
|
| 145 | - $this->checkEntityType($entity); |
|
| 146 | - |
|
| 147 | - $store = new Store($this->aggregate($entity), $this->newQueryBuilder()); |
|
| 148 | - |
|
| 149 | - return $store->execute(); |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - /** |
|
| 153 | - * Check that the entity correspond to the current mapper. |
|
| 154 | - * |
|
| 155 | - * @param mixed $entity |
|
| 156 | - * @throws InvalidArgumentException |
|
| 157 | - * @return void |
|
| 158 | - */ |
|
| 159 | - protected function checkEntityType($entity) |
|
| 160 | - { |
|
| 161 | - if (get_class($entity) != $this->entityMap->getClass()) { |
|
| 162 | - $expected = $this->entityMap->getClass(); |
|
| 163 | - $actual = get_class($entity); |
|
| 164 | - throw new InvalidArgumentException("Expected : $expected, got $actual."); |
|
| 165 | - } |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - /** |
|
| 169 | - * Convert an entity into an aggregate root |
|
| 170 | - * |
|
| 171 | - * @param mixed $entity |
|
| 172 | - * @throws MappingException |
|
| 173 | - * @return \Analogue\ORM\System\Aggregate |
|
| 174 | - */ |
|
| 175 | - protected function aggregate($entity) |
|
| 176 | - { |
|
| 177 | - return new Aggregate($entity); |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - /** |
|
| 181 | - * Get a the Underlying QueryAdapter. |
|
| 182 | - * |
|
| 183 | - * @return \Analogue\ORM\Drivers\QueryAdapter |
|
| 184 | - */ |
|
| 185 | - public function newQueryBuilder() |
|
| 186 | - { |
|
| 187 | - return $this->adapter->getQuery(); |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - /** |
|
| 191 | - * Delete an entity or an entity collection from the database |
|
| 192 | - * |
|
| 193 | - * @param Mappable|\Traversable|array |
|
| 194 | - * @throws MappingException |
|
| 195 | - * @throws \InvalidArgumentException |
|
| 196 | - * @return \Traversable|array |
|
| 197 | - */ |
|
| 198 | - public function delete($entity) |
|
| 199 | - { |
|
| 200 | - if ($this->manager->isTraversable($entity)) { |
|
| 201 | - return $this->deleteCollection($entity); |
|
| 202 | - } else { |
|
| 203 | - $this->deleteEntity($entity); |
|
| 204 | - } |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - /** |
|
| 208 | - * Delete an Entity Collection inside a single db transaction |
|
| 209 | - * |
|
| 210 | - * @param \Traversable|array $entities |
|
| 211 | - * @throws \InvalidArgumentException |
|
| 212 | - * @throws MappingException |
|
| 213 | - * @return \Traversable|array |
|
| 214 | - */ |
|
| 215 | - protected function deleteCollection($entities) |
|
| 216 | - { |
|
| 217 | - $this->adapter->beginTransaction(); |
|
| 218 | - |
|
| 219 | - foreach ($entities as $entity) { |
|
| 220 | - $this->deleteEntity($entity); |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - $this->adapter->commit(); |
|
| 224 | - |
|
| 225 | - return $entities; |
|
| 226 | - } |
|
| 227 | - |
|
| 228 | - /** |
|
| 229 | - * Delete a single entity from the database. |
|
| 230 | - * |
|
| 231 | - * @param Mappable $entity |
|
| 232 | - * @throws \InvalidArgumentException |
|
| 233 | - * @throws MappingException |
|
| 234 | - * @return void |
|
| 235 | - */ |
|
| 236 | - protected function deleteEntity($entity) |
|
| 237 | - { |
|
| 238 | - $this->checkEntityType($entity); |
|
| 239 | - |
|
| 240 | - $delete = new Delete($this->aggregate($entity), $this->newQueryBuilder()); |
|
| 241 | - |
|
| 242 | - $delete->execute(); |
|
| 243 | - } |
|
| 244 | - |
|
| 245 | - /** |
|
| 246 | - * Return the entity map for this mapper |
|
| 247 | - * |
|
| 248 | - * @return EntityMap |
|
| 249 | - */ |
|
| 250 | - public function getEntityMap() |
|
| 251 | - { |
|
| 252 | - return $this->entityMap; |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - /** |
|
| 256 | - * Get the entity cache for the current mapper |
|
| 257 | - * |
|
| 258 | - * @return EntityCache $entityCache |
|
| 259 | - */ |
|
| 260 | - public function getEntityCache() |
|
| 261 | - { |
|
| 262 | - return $this->cache; |
|
| 263 | - } |
|
| 264 | - |
|
| 265 | - /** |
|
| 266 | - * Fire the given event for the entity |
|
| 267 | - * |
|
| 268 | - * @param string $event |
|
| 269 | - * @param \Analogue\ORM\Entity $entity |
|
| 270 | - * @param bool $halt |
|
| 271 | - * @throws InvalidArgumentException |
|
| 272 | - * @return mixed |
|
| 273 | - */ |
|
| 274 | - public function fireEvent($event, $entity, $halt = true) |
|
| 275 | - { |
|
| 276 | - if ($entity instanceof Wrapper) { |
|
| 277 | - throw new InvalidArgumentException('Fired Event with invalid Entity Object'); |
|
| 278 | - } |
|
| 279 | - |
|
| 280 | - $event = "analogue.{$event}." . $this->entityMap->getClass(); |
|
| 281 | - |
|
| 282 | - $method = $halt ? 'until' : 'fire'; |
|
| 283 | - |
|
| 284 | - return $this->dispatcher->$method($event, $entity); |
|
| 285 | - } |
|
| 286 | - |
|
| 287 | - /** |
|
| 288 | - * Register an entity event with the dispatcher. |
|
| 289 | - * |
|
| 290 | - * @param string $event |
|
| 291 | - * @param \Closure $callback |
|
| 292 | - * @return void |
|
| 293 | - */ |
|
| 294 | - public function registerEvent($event, $callback) |
|
| 295 | - { |
|
| 296 | - $name = $this->entityMap->getClass(); |
|
| 297 | - |
|
| 298 | - $this->dispatcher->listen("analogue.{$event}.{$name}", $callback); |
|
| 299 | - } |
|
| 300 | - |
|
| 301 | - /** |
|
| 302 | - * Add a global scope to this mapper query builder |
|
| 303 | - * |
|
| 304 | - * @param ScopeInterface $scope |
|
| 305 | - * @return void |
|
| 306 | - */ |
|
| 307 | - public function addGlobalScope(ScopeInterface $scope) |
|
| 308 | - { |
|
| 309 | - $this->globalScopes[get_class($scope)] = $scope; |
|
| 310 | - } |
|
| 311 | - |
|
| 312 | - /** |
|
| 313 | - * Determine if the mapper has a global scope. |
|
| 314 | - * |
|
| 315 | - * @param \Analogue\ORM\System\ScopeInterface $scope |
|
| 316 | - * @return bool |
|
| 317 | - */ |
|
| 318 | - public function hasGlobalScope($scope) |
|
| 319 | - { |
|
| 320 | - return !is_null($this->getGlobalScope($scope)); |
|
| 321 | - } |
|
| 322 | - |
|
| 323 | - /** |
|
| 324 | - * Get a global scope registered with the modal. |
|
| 325 | - * |
|
| 326 | - * @param \Analogue\ORM\System\ScopeInterface $scope |
|
| 327 | - * @return \Analogue\ORM\System\ScopeInterface|null |
|
| 328 | - */ |
|
| 329 | - public function getGlobalScope($scope) |
|
| 330 | - { |
|
| 331 | - return array_first($this->globalScopes, function ($key, $value) use ($scope) { |
|
| 332 | - return $scope instanceof $value; |
|
| 333 | - }); |
|
| 334 | - } |
|
| 335 | - |
|
| 336 | - /** |
|
| 337 | - * Get a new query instance without a given scope. |
|
| 338 | - * |
|
| 339 | - * @param \Analogue\ORM\System\ScopeInterface $scope |
|
| 340 | - * @return \Analogue\ORM\System\Query |
|
| 341 | - */ |
|
| 342 | - public function newQueryWithoutScope($scope) |
|
| 343 | - { |
|
| 344 | - $this->getGlobalScope($scope)->remove($query = $this->getQuery(), $this); |
|
| 345 | - |
|
| 346 | - return $query; |
|
| 347 | - } |
|
| 348 | - |
|
| 349 | - /** |
|
| 350 | - * Get the Analogue Query Builder for this instance |
|
| 351 | - * |
|
| 352 | - * @return \Analogue\ORM\System\Query |
|
| 353 | - */ |
|
| 354 | - public function getQuery() |
|
| 355 | - { |
|
| 356 | - $query = new Query($this, $this->adapter); |
|
| 357 | - |
|
| 358 | - return $this->applyGlobalScopes($query); |
|
| 359 | - } |
|
| 360 | - |
|
| 361 | - /** |
|
| 362 | - * Apply all of the global scopes to an Analogue Query builder. |
|
| 363 | - * |
|
| 364 | - * @param Query $query |
|
| 365 | - * @return \Analogue\ORM\System\Query |
|
| 366 | - */ |
|
| 367 | - public function applyGlobalScopes($query) |
|
| 368 | - { |
|
| 369 | - foreach ($this->getGlobalScopes() as $scope) { |
|
| 370 | - $scope->apply($query, $this); |
|
| 371 | - } |
|
| 372 | - |
|
| 373 | - return $query; |
|
| 374 | - } |
|
| 375 | - |
|
| 376 | - /** |
|
| 377 | - * Get the global scopes for this class instance. |
|
| 378 | - * |
|
| 379 | - * @return \Analogue\ORM\System\ScopeInterface |
|
| 380 | - */ |
|
| 381 | - public function getGlobalScopes() |
|
| 382 | - { |
|
| 383 | - return $this->globalScopes; |
|
| 384 | - } |
|
| 385 | - |
|
| 386 | - /** |
|
| 387 | - * Add a dynamic method that extends the mapper/repository |
|
| 388 | - * |
|
| 389 | - * @param string $command |
|
| 390 | - */ |
|
| 391 | - public function addCustomCommand($command) |
|
| 392 | - { |
|
| 393 | - $name = lcfirst(class_basename($command)); |
|
| 394 | - |
|
| 395 | - $this->customCommands[$name] = $command; |
|
| 396 | - } |
|
| 397 | - |
|
| 398 | - /** |
|
| 399 | - * Create a new instance of the mapped entity class |
|
| 400 | - * |
|
| 401 | - * @return mixed |
|
| 402 | - */ |
|
| 403 | - public function newInstance() |
|
| 404 | - { |
|
| 405 | - $class = $this->entityMap->getClass(); |
|
| 406 | - |
|
| 407 | - if($this->entityMap->useDependencyInjection()) { |
|
| 408 | - return $this->newInstanceUsingDependencyInjection($class); |
|
| 409 | - } |
|
| 28 | + /** |
|
| 29 | + * The Manager instance |
|
| 30 | + * |
|
| 31 | + * @var \Analogue\ORM\System\Manager |
|
| 32 | + */ |
|
| 33 | + protected $manager; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * Instance of EntityMapper Object |
|
| 37 | + * |
|
| 38 | + * @var \Analogue\ORM\EntityMap |
|
| 39 | + */ |
|
| 40 | + protected $entityMap; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * The instance of db adapter |
|
| 44 | + * |
|
| 45 | + * @var \Analogue\ORM\Drivers\DBAdapter |
|
| 46 | + */ |
|
| 47 | + protected $adapter; |
|
| 48 | + |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * Event dispatcher instance |
|
| 52 | + * |
|
| 53 | + * @var \Illuminate\Contracts\Events\Dispatcher |
|
| 54 | + */ |
|
| 55 | + protected $dispatcher; |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * Entity Cache |
|
| 59 | + * |
|
| 60 | + * @var \Analogue\ORM\System\EntityCache |
|
| 61 | + */ |
|
| 62 | + protected $cache; |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * Global scopes |
|
| 66 | + * |
|
| 67 | + * @var array |
|
| 68 | + */ |
|
| 69 | + protected $globalScopes = []; |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * Custom Commands |
|
| 73 | + * |
|
| 74 | + * @var array |
|
| 75 | + */ |
|
| 76 | + protected $customCommands = []; |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * @param EntityMap $entityMap |
|
| 80 | + * @param DBAdapter $adapter |
|
| 81 | + * @param Dispatcher $dispatcher |
|
| 82 | + * @param Manager $manager |
|
| 83 | + */ |
|
| 84 | + public function __construct(EntityMap $entityMap, DBAdapter $adapter, Dispatcher $dispatcher, Manager $manager) |
|
| 85 | + { |
|
| 86 | + $this->entityMap = $entityMap; |
|
| 87 | + |
|
| 88 | + $this->adapter = $adapter; |
|
| 89 | + |
|
| 90 | + $this->dispatcher = $dispatcher; |
|
| 91 | + |
|
| 92 | + $this->manager = $manager; |
|
| 93 | + |
|
| 94 | + $this->cache = new EntityCache($entityMap); |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + /** |
|
| 98 | + * Persist an entity or an entity collection into the database |
|
| 99 | + * |
|
| 100 | + * @param Mappable|\Traversable|array $entity |
|
| 101 | + * @throws \InvalidArgumentException |
|
| 102 | + * @throws MappingException |
|
| 103 | + * @return Mappable|\Traversable|array |
|
| 104 | + */ |
|
| 105 | + public function store($entity) |
|
| 106 | + { |
|
| 107 | + if ($this->manager->isTraversable($entity)) { |
|
| 108 | + return $this->storeCollection($entity); |
|
| 109 | + } else { |
|
| 110 | + return $this->storeEntity($entity); |
|
| 111 | + } |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * Store an entity collection inside a single DB Transaction |
|
| 116 | + * |
|
| 117 | + * @param \Traversable|array $entities |
|
| 118 | + * @throws \InvalidArgumentException |
|
| 119 | + * @throws MappingException |
|
| 120 | + * @return \Traversable|array |
|
| 121 | + */ |
|
| 122 | + protected function storeCollection($entities) |
|
| 123 | + { |
|
| 124 | + $this->adapter->beginTransaction(); |
|
| 125 | + |
|
| 126 | + foreach ($entities as $entity) { |
|
| 127 | + $this->storeEntity($entity); |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + $this->adapter->commit(); |
|
| 131 | + |
|
| 132 | + return $entities; |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + /** |
|
| 136 | + * Store a single entity into the database |
|
| 137 | + * |
|
| 138 | + * @param Mappable $entity |
|
| 139 | + * @throws \InvalidArgumentException |
|
| 140 | + * @throws MappingException |
|
| 141 | + * @return \Analogue\ORM\Entity |
|
| 142 | + */ |
|
| 143 | + protected function storeEntity($entity) |
|
| 144 | + { |
|
| 145 | + $this->checkEntityType($entity); |
|
| 146 | + |
|
| 147 | + $store = new Store($this->aggregate($entity), $this->newQueryBuilder()); |
|
| 148 | + |
|
| 149 | + return $store->execute(); |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + /** |
|
| 153 | + * Check that the entity correspond to the current mapper. |
|
| 154 | + * |
|
| 155 | + * @param mixed $entity |
|
| 156 | + * @throws InvalidArgumentException |
|
| 157 | + * @return void |
|
| 158 | + */ |
|
| 159 | + protected function checkEntityType($entity) |
|
| 160 | + { |
|
| 161 | + if (get_class($entity) != $this->entityMap->getClass()) { |
|
| 162 | + $expected = $this->entityMap->getClass(); |
|
| 163 | + $actual = get_class($entity); |
|
| 164 | + throw new InvalidArgumentException("Expected : $expected, got $actual."); |
|
| 165 | + } |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + /** |
|
| 169 | + * Convert an entity into an aggregate root |
|
| 170 | + * |
|
| 171 | + * @param mixed $entity |
|
| 172 | + * @throws MappingException |
|
| 173 | + * @return \Analogue\ORM\System\Aggregate |
|
| 174 | + */ |
|
| 175 | + protected function aggregate($entity) |
|
| 176 | + { |
|
| 177 | + return new Aggregate($entity); |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + /** |
|
| 181 | + * Get a the Underlying QueryAdapter. |
|
| 182 | + * |
|
| 183 | + * @return \Analogue\ORM\Drivers\QueryAdapter |
|
| 184 | + */ |
|
| 185 | + public function newQueryBuilder() |
|
| 186 | + { |
|
| 187 | + return $this->adapter->getQuery(); |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + /** |
|
| 191 | + * Delete an entity or an entity collection from the database |
|
| 192 | + * |
|
| 193 | + * @param Mappable|\Traversable|array |
|
| 194 | + * @throws MappingException |
|
| 195 | + * @throws \InvalidArgumentException |
|
| 196 | + * @return \Traversable|array |
|
| 197 | + */ |
|
| 198 | + public function delete($entity) |
|
| 199 | + { |
|
| 200 | + if ($this->manager->isTraversable($entity)) { |
|
| 201 | + return $this->deleteCollection($entity); |
|
| 202 | + } else { |
|
| 203 | + $this->deleteEntity($entity); |
|
| 204 | + } |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + /** |
|
| 208 | + * Delete an Entity Collection inside a single db transaction |
|
| 209 | + * |
|
| 210 | + * @param \Traversable|array $entities |
|
| 211 | + * @throws \InvalidArgumentException |
|
| 212 | + * @throws MappingException |
|
| 213 | + * @return \Traversable|array |
|
| 214 | + */ |
|
| 215 | + protected function deleteCollection($entities) |
|
| 216 | + { |
|
| 217 | + $this->adapter->beginTransaction(); |
|
| 218 | + |
|
| 219 | + foreach ($entities as $entity) { |
|
| 220 | + $this->deleteEntity($entity); |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + $this->adapter->commit(); |
|
| 224 | + |
|
| 225 | + return $entities; |
|
| 226 | + } |
|
| 227 | + |
|
| 228 | + /** |
|
| 229 | + * Delete a single entity from the database. |
|
| 230 | + * |
|
| 231 | + * @param Mappable $entity |
|
| 232 | + * @throws \InvalidArgumentException |
|
| 233 | + * @throws MappingException |
|
| 234 | + * @return void |
|
| 235 | + */ |
|
| 236 | + protected function deleteEntity($entity) |
|
| 237 | + { |
|
| 238 | + $this->checkEntityType($entity); |
|
| 239 | + |
|
| 240 | + $delete = new Delete($this->aggregate($entity), $this->newQueryBuilder()); |
|
| 241 | + |
|
| 242 | + $delete->execute(); |
|
| 243 | + } |
|
| 244 | + |
|
| 245 | + /** |
|
| 246 | + * Return the entity map for this mapper |
|
| 247 | + * |
|
| 248 | + * @return EntityMap |
|
| 249 | + */ |
|
| 250 | + public function getEntityMap() |
|
| 251 | + { |
|
| 252 | + return $this->entityMap; |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + /** |
|
| 256 | + * Get the entity cache for the current mapper |
|
| 257 | + * |
|
| 258 | + * @return EntityCache $entityCache |
|
| 259 | + */ |
|
| 260 | + public function getEntityCache() |
|
| 261 | + { |
|
| 262 | + return $this->cache; |
|
| 263 | + } |
|
| 264 | + |
|
| 265 | + /** |
|
| 266 | + * Fire the given event for the entity |
|
| 267 | + * |
|
| 268 | + * @param string $event |
|
| 269 | + * @param \Analogue\ORM\Entity $entity |
|
| 270 | + * @param bool $halt |
|
| 271 | + * @throws InvalidArgumentException |
|
| 272 | + * @return mixed |
|
| 273 | + */ |
|
| 274 | + public function fireEvent($event, $entity, $halt = true) |
|
| 275 | + { |
|
| 276 | + if ($entity instanceof Wrapper) { |
|
| 277 | + throw new InvalidArgumentException('Fired Event with invalid Entity Object'); |
|
| 278 | + } |
|
| 279 | + |
|
| 280 | + $event = "analogue.{$event}." . $this->entityMap->getClass(); |
|
| 281 | + |
|
| 282 | + $method = $halt ? 'until' : 'fire'; |
|
| 283 | + |
|
| 284 | + return $this->dispatcher->$method($event, $entity); |
|
| 285 | + } |
|
| 286 | + |
|
| 287 | + /** |
|
| 288 | + * Register an entity event with the dispatcher. |
|
| 289 | + * |
|
| 290 | + * @param string $event |
|
| 291 | + * @param \Closure $callback |
|
| 292 | + * @return void |
|
| 293 | + */ |
|
| 294 | + public function registerEvent($event, $callback) |
|
| 295 | + { |
|
| 296 | + $name = $this->entityMap->getClass(); |
|
| 297 | + |
|
| 298 | + $this->dispatcher->listen("analogue.{$event}.{$name}", $callback); |
|
| 299 | + } |
|
| 300 | + |
|
| 301 | + /** |
|
| 302 | + * Add a global scope to this mapper query builder |
|
| 303 | + * |
|
| 304 | + * @param ScopeInterface $scope |
|
| 305 | + * @return void |
|
| 306 | + */ |
|
| 307 | + public function addGlobalScope(ScopeInterface $scope) |
|
| 308 | + { |
|
| 309 | + $this->globalScopes[get_class($scope)] = $scope; |
|
| 310 | + } |
|
| 311 | + |
|
| 312 | + /** |
|
| 313 | + * Determine if the mapper has a global scope. |
|
| 314 | + * |
|
| 315 | + * @param \Analogue\ORM\System\ScopeInterface $scope |
|
| 316 | + * @return bool |
|
| 317 | + */ |
|
| 318 | + public function hasGlobalScope($scope) |
|
| 319 | + { |
|
| 320 | + return !is_null($this->getGlobalScope($scope)); |
|
| 321 | + } |
|
| 322 | + |
|
| 323 | + /** |
|
| 324 | + * Get a global scope registered with the modal. |
|
| 325 | + * |
|
| 326 | + * @param \Analogue\ORM\System\ScopeInterface $scope |
|
| 327 | + * @return \Analogue\ORM\System\ScopeInterface|null |
|
| 328 | + */ |
|
| 329 | + public function getGlobalScope($scope) |
|
| 330 | + { |
|
| 331 | + return array_first($this->globalScopes, function ($key, $value) use ($scope) { |
|
| 332 | + return $scope instanceof $value; |
|
| 333 | + }); |
|
| 334 | + } |
|
| 335 | + |
|
| 336 | + /** |
|
| 337 | + * Get a new query instance without a given scope. |
|
| 338 | + * |
|
| 339 | + * @param \Analogue\ORM\System\ScopeInterface $scope |
|
| 340 | + * @return \Analogue\ORM\System\Query |
|
| 341 | + */ |
|
| 342 | + public function newQueryWithoutScope($scope) |
|
| 343 | + { |
|
| 344 | + $this->getGlobalScope($scope)->remove($query = $this->getQuery(), $this); |
|
| 345 | + |
|
| 346 | + return $query; |
|
| 347 | + } |
|
| 348 | + |
|
| 349 | + /** |
|
| 350 | + * Get the Analogue Query Builder for this instance |
|
| 351 | + * |
|
| 352 | + * @return \Analogue\ORM\System\Query |
|
| 353 | + */ |
|
| 354 | + public function getQuery() |
|
| 355 | + { |
|
| 356 | + $query = new Query($this, $this->adapter); |
|
| 357 | + |
|
| 358 | + return $this->applyGlobalScopes($query); |
|
| 359 | + } |
|
| 360 | + |
|
| 361 | + /** |
|
| 362 | + * Apply all of the global scopes to an Analogue Query builder. |
|
| 363 | + * |
|
| 364 | + * @param Query $query |
|
| 365 | + * @return \Analogue\ORM\System\Query |
|
| 366 | + */ |
|
| 367 | + public function applyGlobalScopes($query) |
|
| 368 | + { |
|
| 369 | + foreach ($this->getGlobalScopes() as $scope) { |
|
| 370 | + $scope->apply($query, $this); |
|
| 371 | + } |
|
| 372 | + |
|
| 373 | + return $query; |
|
| 374 | + } |
|
| 375 | + |
|
| 376 | + /** |
|
| 377 | + * Get the global scopes for this class instance. |
|
| 378 | + * |
|
| 379 | + * @return \Analogue\ORM\System\ScopeInterface |
|
| 380 | + */ |
|
| 381 | + public function getGlobalScopes() |
|
| 382 | + { |
|
| 383 | + return $this->globalScopes; |
|
| 384 | + } |
|
| 385 | + |
|
| 386 | + /** |
|
| 387 | + * Add a dynamic method that extends the mapper/repository |
|
| 388 | + * |
|
| 389 | + * @param string $command |
|
| 390 | + */ |
|
| 391 | + public function addCustomCommand($command) |
|
| 392 | + { |
|
| 393 | + $name = lcfirst(class_basename($command)); |
|
| 394 | + |
|
| 395 | + $this->customCommands[$name] = $command; |
|
| 396 | + } |
|
| 397 | + |
|
| 398 | + /** |
|
| 399 | + * Create a new instance of the mapped entity class |
|
| 400 | + * |
|
| 401 | + * @return mixed |
|
| 402 | + */ |
|
| 403 | + public function newInstance() |
|
| 404 | + { |
|
| 405 | + $class = $this->entityMap->getClass(); |
|
| 406 | + |
|
| 407 | + if($this->entityMap->useDependencyInjection()) { |
|
| 408 | + return $this->newInstanceUsingDependencyInjection($class); |
|
| 409 | + } |
|
| 410 | 410 | |
| 411 | - return $this->newInstanceUsingInstantiator($class); |
|
| 412 | - } |
|
| 413 | - |
|
| 414 | - /** |
|
| 415 | - * Return a new object instance using dependency injection |
|
| 416 | - * |
|
| 417 | - * @param string $class |
|
| 418 | - * @return mixed |
|
| 419 | - */ |
|
| 420 | - protected function newInstanceUsingDependencyInjection($class) |
|
| 421 | - { |
|
| 422 | - if(! class_exists(Container::class)) { |
|
| 423 | - throw new ErrorException("Illuminate\Container\Container is required to use Dependency Injection"); |
|
| 424 | - } |
|
| 425 | - |
|
| 426 | - return Container::getInstance()->make($class); |
|
| 427 | - } |
|
| 428 | - |
|
| 429 | - /** |
|
| 430 | - * Return a new object instance using doctrine's instantiator |
|
| 431 | - * |
|
| 432 | - * @param string $class |
|
| 433 | - * @return mixed |
|
| 434 | - */ |
|
| 435 | - protected function newInstanceUsingInstantiator($class) |
|
| 436 | - { |
|
| 437 | - $instantiator = new \Doctrine\Instantiator\Instantiator(); |
|
| 438 | - |
|
| 439 | - return $instantiator->instantiate($class); |
|
| 440 | - } |
|
| 441 | - |
|
| 442 | - /** |
|
| 443 | - * Get an unscoped Analogue Query Builder for this instance |
|
| 444 | - * |
|
| 445 | - * @return \Analogue\ORM\System\Query |
|
| 446 | - */ |
|
| 447 | - public function globalQuery() |
|
| 448 | - { |
|
| 449 | - return $this->newQueryWithoutScopes(); |
|
| 450 | - } |
|
| 451 | - |
|
| 452 | - /** |
|
| 453 | - * Get a new query builder that doesn't have any global scopes. |
|
| 454 | - * |
|
| 455 | - * @return Query |
|
| 456 | - */ |
|
| 457 | - public function newQueryWithoutScopes() |
|
| 458 | - { |
|
| 459 | - return $this->removeGlobalScopes($this->getQuery()); |
|
| 460 | - } |
|
| 461 | - |
|
| 462 | - /** |
|
| 463 | - * Remove all of the global scopes from an Analogue Query builder. |
|
| 464 | - * |
|
| 465 | - * @param Query $query |
|
| 466 | - * @return \Analogue\ORM\System\Query |
|
| 467 | - */ |
|
| 468 | - public function removeGlobalScopes($query) |
|
| 469 | - { |
|
| 470 | - foreach ($this->getGlobalScopes() as $scope) { |
|
| 471 | - $scope->remove($query, $this); |
|
| 472 | - } |
|
| 473 | - |
|
| 474 | - return $query; |
|
| 475 | - } |
|
| 476 | - |
|
| 477 | - /** |
|
| 478 | - * Return the manager instance |
|
| 479 | - * |
|
| 480 | - * @return \Analogue\ORM\System\Manager |
|
| 481 | - */ |
|
| 482 | - public function getManager() |
|
| 483 | - { |
|
| 484 | - return $this->manager; |
|
| 485 | - } |
|
| 486 | - |
|
| 487 | - /** |
|
| 488 | - * Dynamically handle calls to custom commands, or Redirects to query() |
|
| 489 | - * |
|
| 490 | - * @param string $method |
|
| 491 | - * @param array $parameters |
|
| 492 | - * @throws \Exception |
|
| 493 | - * @return mixed |
|
| 494 | - */ |
|
| 495 | - public function __call($method, $parameters) |
|
| 496 | - { |
|
| 497 | - // Check if method is a custom command on the mapper |
|
| 498 | - if ($this->hasCustomCommand($method)) { |
|
| 499 | - if (count($parameters) == 0) { |
|
| 500 | - throw new \Exception("$method must at least have 1 argument"); |
|
| 501 | - } |
|
| 502 | - |
|
| 503 | - return $this->executeCustomCommand($method, $parameters[0]); |
|
| 504 | - } |
|
| 505 | - |
|
| 506 | - // Redirect call on a new query instance |
|
| 507 | - return call_user_func_array([$this->query(), $method], $parameters); |
|
| 508 | - } |
|
| 509 | - |
|
| 510 | - /** |
|
| 511 | - * Check if this mapper supports this command |
|
| 512 | - * @param string $command |
|
| 513 | - * @return boolean |
|
| 514 | - */ |
|
| 515 | - public function hasCustomCommand($command) |
|
| 516 | - { |
|
| 517 | - return in_array($command, $this->getCustomCommands()); |
|
| 518 | - } |
|
| 519 | - |
|
| 520 | - /** |
|
| 521 | - * Get all the custom commands registered on this mapper |
|
| 522 | - * |
|
| 523 | - * @return array |
|
| 524 | - */ |
|
| 525 | - public function getCustomCommands() |
|
| 526 | - { |
|
| 527 | - return array_keys($this->customCommands); |
|
| 528 | - } |
|
| 529 | - |
|
| 530 | - /** |
|
| 531 | - * Execute a custom command on an Entity |
|
| 532 | - * |
|
| 533 | - * @param string $command |
|
| 534 | - * @param mixed|Collection|array $entity |
|
| 535 | - * @throws \InvalidArgumentException |
|
| 536 | - * @throws MappingException |
|
| 537 | - * @return mixed |
|
| 538 | - */ |
|
| 539 | - public function executeCustomCommand($command, $entity) |
|
| 540 | - { |
|
| 541 | - $commandClass = $this->customCommands[$command]; |
|
| 542 | - |
|
| 543 | - if ($this->manager->isTraversable($entity)) { |
|
| 544 | - foreach ($entity as $instance) { |
|
| 545 | - $this->executeSingleCustomCommand($commandClass, $instance); |
|
| 546 | - } |
|
| 547 | - } else { |
|
| 548 | - return $this->executeSingleCustomCommand($commandClass, $entity); |
|
| 549 | - } |
|
| 550 | - } |
|
| 551 | - |
|
| 552 | - /** |
|
| 553 | - * Execute a single command instance |
|
| 554 | - * |
|
| 555 | - * @param string $commandClass |
|
| 556 | - * @param mixed $entity |
|
| 557 | - * @throws \InvalidArgumentException |
|
| 558 | - * @throws MappingException |
|
| 559 | - * @return mixed |
|
| 560 | - */ |
|
| 561 | - protected function executeSingleCustomCommand($commandClass, $entity) |
|
| 562 | - { |
|
| 563 | - $this->checkEntityType($entity); |
|
| 564 | - |
|
| 565 | - $instance = new $commandClass($this->aggregate($entity), $this->newQueryBuilder()); |
|
| 566 | - |
|
| 567 | - return $instance->execute(); |
|
| 568 | - } |
|
| 569 | - |
|
| 570 | - /** |
|
| 571 | - * Get the Analogue Query Builder for this instance |
|
| 572 | - * |
|
| 573 | - * @return \Analogue\ORM\System\Query |
|
| 574 | - */ |
|
| 575 | - public function query() |
|
| 576 | - { |
|
| 577 | - return $this->getQuery(); |
|
| 578 | - } |
|
| 411 | + return $this->newInstanceUsingInstantiator($class); |
|
| 412 | + } |
|
| 413 | + |
|
| 414 | + /** |
|
| 415 | + * Return a new object instance using dependency injection |
|
| 416 | + * |
|
| 417 | + * @param string $class |
|
| 418 | + * @return mixed |
|
| 419 | + */ |
|
| 420 | + protected function newInstanceUsingDependencyInjection($class) |
|
| 421 | + { |
|
| 422 | + if(! class_exists(Container::class)) { |
|
| 423 | + throw new ErrorException("Illuminate\Container\Container is required to use Dependency Injection"); |
|
| 424 | + } |
|
| 425 | + |
|
| 426 | + return Container::getInstance()->make($class); |
|
| 427 | + } |
|
| 428 | + |
|
| 429 | + /** |
|
| 430 | + * Return a new object instance using doctrine's instantiator |
|
| 431 | + * |
|
| 432 | + * @param string $class |
|
| 433 | + * @return mixed |
|
| 434 | + */ |
|
| 435 | + protected function newInstanceUsingInstantiator($class) |
|
| 436 | + { |
|
| 437 | + $instantiator = new \Doctrine\Instantiator\Instantiator(); |
|
| 438 | + |
|
| 439 | + return $instantiator->instantiate($class); |
|
| 440 | + } |
|
| 441 | + |
|
| 442 | + /** |
|
| 443 | + * Get an unscoped Analogue Query Builder for this instance |
|
| 444 | + * |
|
| 445 | + * @return \Analogue\ORM\System\Query |
|
| 446 | + */ |
|
| 447 | + public function globalQuery() |
|
| 448 | + { |
|
| 449 | + return $this->newQueryWithoutScopes(); |
|
| 450 | + } |
|
| 451 | + |
|
| 452 | + /** |
|
| 453 | + * Get a new query builder that doesn't have any global scopes. |
|
| 454 | + * |
|
| 455 | + * @return Query |
|
| 456 | + */ |
|
| 457 | + public function newQueryWithoutScopes() |
|
| 458 | + { |
|
| 459 | + return $this->removeGlobalScopes($this->getQuery()); |
|
| 460 | + } |
|
| 461 | + |
|
| 462 | + /** |
|
| 463 | + * Remove all of the global scopes from an Analogue Query builder. |
|
| 464 | + * |
|
| 465 | + * @param Query $query |
|
| 466 | + * @return \Analogue\ORM\System\Query |
|
| 467 | + */ |
|
| 468 | + public function removeGlobalScopes($query) |
|
| 469 | + { |
|
| 470 | + foreach ($this->getGlobalScopes() as $scope) { |
|
| 471 | + $scope->remove($query, $this); |
|
| 472 | + } |
|
| 473 | + |
|
| 474 | + return $query; |
|
| 475 | + } |
|
| 476 | + |
|
| 477 | + /** |
|
| 478 | + * Return the manager instance |
|
| 479 | + * |
|
| 480 | + * @return \Analogue\ORM\System\Manager |
|
| 481 | + */ |
|
| 482 | + public function getManager() |
|
| 483 | + { |
|
| 484 | + return $this->manager; |
|
| 485 | + } |
|
| 486 | + |
|
| 487 | + /** |
|
| 488 | + * Dynamically handle calls to custom commands, or Redirects to query() |
|
| 489 | + * |
|
| 490 | + * @param string $method |
|
| 491 | + * @param array $parameters |
|
| 492 | + * @throws \Exception |
|
| 493 | + * @return mixed |
|
| 494 | + */ |
|
| 495 | + public function __call($method, $parameters) |
|
| 496 | + { |
|
| 497 | + // Check if method is a custom command on the mapper |
|
| 498 | + if ($this->hasCustomCommand($method)) { |
|
| 499 | + if (count($parameters) == 0) { |
|
| 500 | + throw new \Exception("$method must at least have 1 argument"); |
|
| 501 | + } |
|
| 502 | + |
|
| 503 | + return $this->executeCustomCommand($method, $parameters[0]); |
|
| 504 | + } |
|
| 505 | + |
|
| 506 | + // Redirect call on a new query instance |
|
| 507 | + return call_user_func_array([$this->query(), $method], $parameters); |
|
| 508 | + } |
|
| 509 | + |
|
| 510 | + /** |
|
| 511 | + * Check if this mapper supports this command |
|
| 512 | + * @param string $command |
|
| 513 | + * @return boolean |
|
| 514 | + */ |
|
| 515 | + public function hasCustomCommand($command) |
|
| 516 | + { |
|
| 517 | + return in_array($command, $this->getCustomCommands()); |
|
| 518 | + } |
|
| 519 | + |
|
| 520 | + /** |
|
| 521 | + * Get all the custom commands registered on this mapper |
|
| 522 | + * |
|
| 523 | + * @return array |
|
| 524 | + */ |
|
| 525 | + public function getCustomCommands() |
|
| 526 | + { |
|
| 527 | + return array_keys($this->customCommands); |
|
| 528 | + } |
|
| 529 | + |
|
| 530 | + /** |
|
| 531 | + * Execute a custom command on an Entity |
|
| 532 | + * |
|
| 533 | + * @param string $command |
|
| 534 | + * @param mixed|Collection|array $entity |
|
| 535 | + * @throws \InvalidArgumentException |
|
| 536 | + * @throws MappingException |
|
| 537 | + * @return mixed |
|
| 538 | + */ |
|
| 539 | + public function executeCustomCommand($command, $entity) |
|
| 540 | + { |
|
| 541 | + $commandClass = $this->customCommands[$command]; |
|
| 542 | + |
|
| 543 | + if ($this->manager->isTraversable($entity)) { |
|
| 544 | + foreach ($entity as $instance) { |
|
| 545 | + $this->executeSingleCustomCommand($commandClass, $instance); |
|
| 546 | + } |
|
| 547 | + } else { |
|
| 548 | + return $this->executeSingleCustomCommand($commandClass, $entity); |
|
| 549 | + } |
|
| 550 | + } |
|
| 551 | + |
|
| 552 | + /** |
|
| 553 | + * Execute a single command instance |
|
| 554 | + * |
|
| 555 | + * @param string $commandClass |
|
| 556 | + * @param mixed $entity |
|
| 557 | + * @throws \InvalidArgumentException |
|
| 558 | + * @throws MappingException |
|
| 559 | + * @return mixed |
|
| 560 | + */ |
|
| 561 | + protected function executeSingleCustomCommand($commandClass, $entity) |
|
| 562 | + { |
|
| 563 | + $this->checkEntityType($entity); |
|
| 564 | + |
|
| 565 | + $instance = new $commandClass($this->aggregate($entity), $this->newQueryBuilder()); |
|
| 566 | + |
|
| 567 | + return $instance->execute(); |
|
| 568 | + } |
|
| 569 | + |
|
| 570 | + /** |
|
| 571 | + * Get the Analogue Query Builder for this instance |
|
| 572 | + * |
|
| 573 | + * @return \Analogue\ORM\System\Query |
|
| 574 | + */ |
|
| 575 | + public function query() |
|
| 576 | + { |
|
| 577 | + return $this->getQuery(); |
|
| 578 | + } |
|
| 579 | 579 | } |
@@ -277,7 +277,7 @@ discard block |
||
| 277 | 277 | throw new InvalidArgumentException('Fired Event with invalid Entity Object'); |
| 278 | 278 | } |
| 279 | 279 | |
| 280 | - $event = "analogue.{$event}." . $this->entityMap->getClass(); |
|
| 280 | + $event = "analogue.{$event}.".$this->entityMap->getClass(); |
|
| 281 | 281 | |
| 282 | 282 | $method = $halt ? 'until' : 'fire'; |
| 283 | 283 | |
@@ -328,7 +328,7 @@ discard block |
||
| 328 | 328 | */ |
| 329 | 329 | public function getGlobalScope($scope) |
| 330 | 330 | { |
| 331 | - return array_first($this->globalScopes, function ($key, $value) use ($scope) { |
|
| 331 | + return array_first($this->globalScopes, function($key, $value) use ($scope) { |
|
| 332 | 332 | return $scope instanceof $value; |
| 333 | 333 | }); |
| 334 | 334 | } |
@@ -404,7 +404,7 @@ discard block |
||
| 404 | 404 | { |
| 405 | 405 | $class = $this->entityMap->getClass(); |
| 406 | 406 | |
| 407 | - if($this->entityMap->useDependencyInjection()) { |
|
| 407 | + if ($this->entityMap->useDependencyInjection()) { |
|
| 408 | 408 | return $this->newInstanceUsingDependencyInjection($class); |
| 409 | 409 | } |
| 410 | 410 | |
@@ -419,7 +419,7 @@ discard block |
||
| 419 | 419 | */ |
| 420 | 420 | protected function newInstanceUsingDependencyInjection($class) |
| 421 | 421 | { |
| 422 | - if(! class_exists(Container::class)) { |
|
| 422 | + if (!class_exists(Container::class)) { |
|
| 423 | 423 | throw new ErrorException("Illuminate\Container\Container is required to use Dependency Injection"); |
| 424 | 424 | } |
| 425 | 425 | |
@@ -7,31 +7,31 @@ |
||
| 7 | 7 | |
| 8 | 8 | abstract class Command |
| 9 | 9 | { |
| 10 | - /** |
|
| 11 | - * The aggregated entity on which the command is executed |
|
| 12 | - * |
|
| 13 | - * @var \Analogue\ORM\System\Aggregate |
|
| 14 | - */ |
|
| 15 | - protected $aggregate; |
|
| 10 | + /** |
|
| 11 | + * The aggregated entity on which the command is executed |
|
| 12 | + * |
|
| 13 | + * @var \Analogue\ORM\System\Aggregate |
|
| 14 | + */ |
|
| 15 | + protected $aggregate; |
|
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * Query Builder instance |
|
| 19 | - * |
|
| 20 | - * @var \Illuminate\Database\Query\Builder |
|
| 21 | - */ |
|
| 22 | - protected $query; |
|
| 17 | + /** |
|
| 18 | + * Query Builder instance |
|
| 19 | + * |
|
| 20 | + * @var \Illuminate\Database\Query\Builder |
|
| 21 | + */ |
|
| 22 | + protected $query; |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * Command constructor. |
|
| 26 | - * @param Aggregate $aggregate |
|
| 27 | - * @param QueryAdapter|\Analogue\ORM\Drivers\IlluminateQueryAdapter $query |
|
| 28 | - */ |
|
| 29 | - public function __construct(Aggregate $aggregate, Builder $query) |
|
| 30 | - { |
|
| 31 | - $this->aggregate = $aggregate; |
|
| 24 | + /** |
|
| 25 | + * Command constructor. |
|
| 26 | + * @param Aggregate $aggregate |
|
| 27 | + * @param QueryAdapter|\Analogue\ORM\Drivers\IlluminateQueryAdapter $query |
|
| 28 | + */ |
|
| 29 | + public function __construct(Aggregate $aggregate, Builder $query) |
|
| 30 | + { |
|
| 31 | + $this->aggregate = $aggregate; |
|
| 32 | 32 | |
| 33 | - $this->query = $query->from($aggregate->getEntityMap()->getTable()); |
|
| 34 | - } |
|
| 33 | + $this->query = $query->from($aggregate->getEntityMap()->getTable()); |
|
| 34 | + } |
|
| 35 | 35 | |
| 36 | - abstract public function execute(); |
|
| 36 | + abstract public function execute(); |
|
| 37 | 37 | } |
@@ -44,8 +44,7 @@ |
||
| 44 | 44 | $this->insert(); |
| 45 | 45 | |
| 46 | 46 | $mapper->fireEvent('created', $entity, false); |
| 47 | - } |
|
| 48 | - else if ($this->aggregate->isDirty()) { |
|
| 47 | + } else if ($this->aggregate->isDirty()) { |
|
| 49 | 48 | if ($mapper->fireEvent('updating', $entity) === false) { |
| 50 | 49 | return false; |
| 51 | 50 | } |
@@ -14,237 +14,237 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class Store extends Command |
| 16 | 16 | { |
| 17 | - /** |
|
| 18 | - * Persist the entity in the database |
|
| 19 | - * |
|
| 20 | - * @throws \InvalidArgumentException |
|
| 21 | - * @return false|mixed |
|
| 22 | - */ |
|
| 23 | - public function execute() |
|
| 24 | - { |
|
| 25 | - $entity = $this->aggregate->getEntityObject(); |
|
| 26 | - |
|
| 27 | - $mapper = $this->aggregate->getMapper(); |
|
| 28 | - |
|
| 29 | - if ($mapper->fireEvent('storing', $entity) === false) { |
|
| 30 | - return false; |
|
| 31 | - } |
|
| 32 | - |
|
| 33 | - $this->preStoreProcess(); |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * We will test the entity for existence |
|
| 37 | - * and run a creation if it doesn't exists |
|
| 38 | - */ |
|
| 39 | - if (!$this->aggregate->exists()) { |
|
| 40 | - if ($mapper->fireEvent('creating', $entity) === false) { |
|
| 41 | - return false; |
|
| 42 | - } |
|
| 43 | - |
|
| 44 | - $this->insert(); |
|
| 45 | - |
|
| 46 | - $mapper->fireEvent('created', $entity, false); |
|
| 47 | - } |
|
| 48 | - else if ($this->aggregate->isDirty()) { |
|
| 49 | - if ($mapper->fireEvent('updating', $entity) === false) { |
|
| 50 | - return false; |
|
| 51 | - } |
|
| 52 | - $this->update(); |
|
| 53 | - |
|
| 54 | - $mapper->fireEvent('updated', $entity, false); |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - $this->postStoreProcess(); |
|
| 58 | - |
|
| 59 | - $mapper->fireEvent('stored', $entity, false); |
|
| 60 | - |
|
| 61 | - return $entity; |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * Run all operations that have to occur before actually |
|
| 66 | - * storing the entity |
|
| 67 | - * |
|
| 68 | - * @throws \InvalidArgumentException |
|
| 69 | - * @return void |
|
| 70 | - */ |
|
| 71 | - protected function preStoreProcess() |
|
| 72 | - { |
|
| 73 | - // Create any related object that doesn't exist in the database. |
|
| 74 | - $localRelationships = $this->aggregate->getEntityMap()->getLocalRelationships(); |
|
| 75 | - |
|
| 76 | - $this->createRelatedEntities($localRelationships); |
|
| 77 | - |
|
| 78 | - // Now we can sync the related collections |
|
| 79 | - $this->aggregate->syncRelationships($localRelationships); |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * Check for existence and create non-existing related entities |
|
| 84 | - * |
|
| 85 | - * @param array |
|
| 86 | - * @throws \InvalidArgumentException |
|
| 87 | - * @return void |
|
| 88 | - */ |
|
| 89 | - protected function createRelatedEntities($relations) |
|
| 90 | - { |
|
| 91 | - $entitiesToCreate = $this->aggregate->getNonExistingRelated($relations); |
|
| 92 | - |
|
| 93 | - foreach ($entitiesToCreate as $aggregate) { |
|
| 94 | - $this->createStoreCommand($aggregate)->execute(); |
|
| 95 | - } |
|
| 17 | + /** |
|
| 18 | + * Persist the entity in the database |
|
| 19 | + * |
|
| 20 | + * @throws \InvalidArgumentException |
|
| 21 | + * @return false|mixed |
|
| 22 | + */ |
|
| 23 | + public function execute() |
|
| 24 | + { |
|
| 25 | + $entity = $this->aggregate->getEntityObject(); |
|
| 26 | + |
|
| 27 | + $mapper = $this->aggregate->getMapper(); |
|
| 28 | + |
|
| 29 | + if ($mapper->fireEvent('storing', $entity) === false) { |
|
| 30 | + return false; |
|
| 31 | + } |
|
| 32 | + |
|
| 33 | + $this->preStoreProcess(); |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * We will test the entity for existence |
|
| 37 | + * and run a creation if it doesn't exists |
|
| 38 | + */ |
|
| 39 | + if (!$this->aggregate->exists()) { |
|
| 40 | + if ($mapper->fireEvent('creating', $entity) === false) { |
|
| 41 | + return false; |
|
| 42 | + } |
|
| 43 | + |
|
| 44 | + $this->insert(); |
|
| 45 | + |
|
| 46 | + $mapper->fireEvent('created', $entity, false); |
|
| 47 | + } |
|
| 48 | + else if ($this->aggregate->isDirty()) { |
|
| 49 | + if ($mapper->fireEvent('updating', $entity) === false) { |
|
| 50 | + return false; |
|
| 51 | + } |
|
| 52 | + $this->update(); |
|
| 53 | + |
|
| 54 | + $mapper->fireEvent('updated', $entity, false); |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + $this->postStoreProcess(); |
|
| 58 | + |
|
| 59 | + $mapper->fireEvent('stored', $entity, false); |
|
| 60 | + |
|
| 61 | + return $entity; |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * Run all operations that have to occur before actually |
|
| 66 | + * storing the entity |
|
| 67 | + * |
|
| 68 | + * @throws \InvalidArgumentException |
|
| 69 | + * @return void |
|
| 70 | + */ |
|
| 71 | + protected function preStoreProcess() |
|
| 72 | + { |
|
| 73 | + // Create any related object that doesn't exist in the database. |
|
| 74 | + $localRelationships = $this->aggregate->getEntityMap()->getLocalRelationships(); |
|
| 75 | + |
|
| 76 | + $this->createRelatedEntities($localRelationships); |
|
| 77 | + |
|
| 78 | + // Now we can sync the related collections |
|
| 79 | + $this->aggregate->syncRelationships($localRelationships); |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * Check for existence and create non-existing related entities |
|
| 84 | + * |
|
| 85 | + * @param array |
|
| 86 | + * @throws \InvalidArgumentException |
|
| 87 | + * @return void |
|
| 88 | + */ |
|
| 89 | + protected function createRelatedEntities($relations) |
|
| 90 | + { |
|
| 91 | + $entitiesToCreate = $this->aggregate->getNonExistingRelated($relations); |
|
| 92 | + |
|
| 93 | + foreach ($entitiesToCreate as $aggregate) { |
|
| 94 | + $this->createStoreCommand($aggregate)->execute(); |
|
| 95 | + } |
|
| 96 | 96 | |
| 97 | - } |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * Create a new store command |
|
| 101 | - * |
|
| 102 | - * @param Aggregate $aggregate |
|
| 103 | - * @return Store |
|
| 104 | - */ |
|
| 105 | - protected function createStoreCommand(Aggregate $aggregate) |
|
| 106 | - { |
|
| 107 | - // We gotta retrieve the corresponding query adapter to use. |
|
| 108 | - $mapper = $aggregate->getMapper(); |
|
| 109 | - |
|
| 110 | - return new Store($aggregate, $mapper->newQueryBuilder()); |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * Run all operations that have to occur after the entity |
|
| 115 | - * is stored. |
|
| 116 | - * |
|
| 117 | - * @throws \InvalidArgumentException |
|
| 118 | - * @return void |
|
| 119 | - */ |
|
| 120 | - protected function postStoreProcess() |
|
| 121 | - { |
|
| 122 | - $aggregate = $this->aggregate; |
|
| 123 | - |
|
| 124 | - // Create any related object that doesn't exist in the database. |
|
| 125 | - $foreignRelationships = $aggregate->getEntityMap()->getForeignRelationships(); |
|
| 126 | - $this->createRelatedEntities($foreignRelationships); |
|
| 127 | - |
|
| 128 | - // Update any pivot tables that has been modified. |
|
| 129 | - $aggregate->updatePivotRecords(); |
|
| 130 | - |
|
| 131 | - // Update any dirty relationship. This include relationships that already exists, have |
|
| 132 | - // dirty attributes / newly created related entities / dirty related entities. |
|
| 133 | - $dirtyRelatedAggregates = $aggregate->getDirtyRelationships(); |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * Create a new store command |
|
| 101 | + * |
|
| 102 | + * @param Aggregate $aggregate |
|
| 103 | + * @return Store |
|
| 104 | + */ |
|
| 105 | + protected function createStoreCommand(Aggregate $aggregate) |
|
| 106 | + { |
|
| 107 | + // We gotta retrieve the corresponding query adapter to use. |
|
| 108 | + $mapper = $aggregate->getMapper(); |
|
| 109 | + |
|
| 110 | + return new Store($aggregate, $mapper->newQueryBuilder()); |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * Run all operations that have to occur after the entity |
|
| 115 | + * is stored. |
|
| 116 | + * |
|
| 117 | + * @throws \InvalidArgumentException |
|
| 118 | + * @return void |
|
| 119 | + */ |
|
| 120 | + protected function postStoreProcess() |
|
| 121 | + { |
|
| 122 | + $aggregate = $this->aggregate; |
|
| 123 | + |
|
| 124 | + // Create any related object that doesn't exist in the database. |
|
| 125 | + $foreignRelationships = $aggregate->getEntityMap()->getForeignRelationships(); |
|
| 126 | + $this->createRelatedEntities($foreignRelationships); |
|
| 127 | + |
|
| 128 | + // Update any pivot tables that has been modified. |
|
| 129 | + $aggregate->updatePivotRecords(); |
|
| 130 | + |
|
| 131 | + // Update any dirty relationship. This include relationships that already exists, have |
|
| 132 | + // dirty attributes / newly created related entities / dirty related entities. |
|
| 133 | + $dirtyRelatedAggregates = $aggregate->getDirtyRelationships(); |
|
| 134 | 134 | |
| 135 | - foreach ($dirtyRelatedAggregates as $related) { |
|
| 136 | - $this->createStoreCommand($related)->execute(); |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - // Now we can sync the related collections |
|
| 140 | - if ($this->aggregate->exists()) { |
|
| 141 | - $this->aggregate->syncRelationships($foreignRelationships); |
|
| 142 | - } |
|
| 135 | + foreach ($dirtyRelatedAggregates as $related) { |
|
| 136 | + $this->createStoreCommand($related)->execute(); |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + // Now we can sync the related collections |
|
| 140 | + if ($this->aggregate->exists()) { |
|
| 141 | + $this->aggregate->syncRelationships($foreignRelationships); |
|
| 142 | + } |
|
| 143 | 143 | |
| 144 | - // TODO be move it to the wrapper class |
|
| 145 | - // so it's the same code for the entity builder |
|
| 146 | - $aggregate->setProxies(); |
|
| 147 | - |
|
| 148 | - // Update Entity Cache |
|
| 149 | - $aggregate->getMapper()->getEntityCache()->refresh($aggregate); |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - /** |
|
| 153 | - * Update Related Entities which attributes have |
|
| 154 | - * been modified. |
|
| 155 | - * |
|
| 156 | - * @return void |
|
| 157 | - */ |
|
| 158 | - protected function updateDirtyRelated() |
|
| 159 | - { |
|
| 160 | - $relations = $this->entityMap->getRelationships(); |
|
| 161 | - $attributes = $this->getAttributes(); |
|
| 162 | - |
|
| 163 | - foreach ($relations as $relation) { |
|
| 164 | - if (!array_key_exists($relation, $attributes)) { |
|
| 165 | - continue; |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - $value = $attributes[$relation]; |
|
| 169 | - |
|
| 170 | - if ($value == null) { |
|
| 171 | - continue; |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - if ($value instanceof EntityProxy) { |
|
| 175 | - continue; |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - if ($value instanceof CollectionProxy && $value->isLoaded()) { |
|
| 179 | - $value = $value->getUnderlyingCollection(); |
|
| 180 | - } |
|
| 181 | - if ($value instanceof CollectionProxy && !$value->isLoaded()) { |
|
| 182 | - foreach ($value->getAddedItems() as $entity) { |
|
| 183 | - $this->updateEntityIfDirty($entity); |
|
| 184 | - } |
|
| 185 | - continue; |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - if ($value instanceof EntityCollection) { |
|
| 189 | - foreach ($value as $entity) { |
|
| 190 | - if (!$this->createEntityIfNotExists($entity)) { |
|
| 191 | - $this->updateEntityIfDirty($entity); |
|
| 192 | - } |
|
| 193 | - } |
|
| 194 | - continue; |
|
| 195 | - } |
|
| 196 | - if ($value instanceof Mappable) { |
|
| 197 | - $this->updateEntityIfDirty($value); |
|
| 198 | - continue; |
|
| 199 | - } |
|
| 200 | - } |
|
| 201 | - } |
|
| 202 | - |
|
| 203 | - /** |
|
| 204 | - * Execute an insert statement on the database |
|
| 205 | - * |
|
| 206 | - * @return void |
|
| 207 | - */ |
|
| 208 | - protected function insert() |
|
| 209 | - { |
|
| 210 | - $aggregate = $this->aggregate; |
|
| 211 | - |
|
| 212 | - $attributes = $aggregate->getRawAttributes(); |
|
| 144 | + // TODO be move it to the wrapper class |
|
| 145 | + // so it's the same code for the entity builder |
|
| 146 | + $aggregate->setProxies(); |
|
| 147 | + |
|
| 148 | + // Update Entity Cache |
|
| 149 | + $aggregate->getMapper()->getEntityCache()->refresh($aggregate); |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + /** |
|
| 153 | + * Update Related Entities which attributes have |
|
| 154 | + * been modified. |
|
| 155 | + * |
|
| 156 | + * @return void |
|
| 157 | + */ |
|
| 158 | + protected function updateDirtyRelated() |
|
| 159 | + { |
|
| 160 | + $relations = $this->entityMap->getRelationships(); |
|
| 161 | + $attributes = $this->getAttributes(); |
|
| 162 | + |
|
| 163 | + foreach ($relations as $relation) { |
|
| 164 | + if (!array_key_exists($relation, $attributes)) { |
|
| 165 | + continue; |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + $value = $attributes[$relation]; |
|
| 169 | + |
|
| 170 | + if ($value == null) { |
|
| 171 | + continue; |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + if ($value instanceof EntityProxy) { |
|
| 175 | + continue; |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + if ($value instanceof CollectionProxy && $value->isLoaded()) { |
|
| 179 | + $value = $value->getUnderlyingCollection(); |
|
| 180 | + } |
|
| 181 | + if ($value instanceof CollectionProxy && !$value->isLoaded()) { |
|
| 182 | + foreach ($value->getAddedItems() as $entity) { |
|
| 183 | + $this->updateEntityIfDirty($entity); |
|
| 184 | + } |
|
| 185 | + continue; |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + if ($value instanceof EntityCollection) { |
|
| 189 | + foreach ($value as $entity) { |
|
| 190 | + if (!$this->createEntityIfNotExists($entity)) { |
|
| 191 | + $this->updateEntityIfDirty($entity); |
|
| 192 | + } |
|
| 193 | + } |
|
| 194 | + continue; |
|
| 195 | + } |
|
| 196 | + if ($value instanceof Mappable) { |
|
| 197 | + $this->updateEntityIfDirty($value); |
|
| 198 | + continue; |
|
| 199 | + } |
|
| 200 | + } |
|
| 201 | + } |
|
| 202 | + |
|
| 203 | + /** |
|
| 204 | + * Execute an insert statement on the database |
|
| 205 | + * |
|
| 206 | + * @return void |
|
| 207 | + */ |
|
| 208 | + protected function insert() |
|
| 209 | + { |
|
| 210 | + $aggregate = $this->aggregate; |
|
| 211 | + |
|
| 212 | + $attributes = $aggregate->getRawAttributes(); |
|
| 213 | 213 | |
| 214 | - $keyName = $aggregate->getEntityMap()->getKeyName(); |
|
| 214 | + $keyName = $aggregate->getEntityMap()->getKeyName(); |
|
| 215 | 215 | |
| 216 | - // Check if the primary key is defined in the attributes |
|
| 217 | - if (array_key_exists($keyName, $attributes) && $attributes[$keyName] != null) { |
|
| 218 | - $this->query->insert($attributes); |
|
| 219 | - } else { |
|
| 220 | - $sequence = $aggregate->getEntityMap()->getSequence(); |
|
| 216 | + // Check if the primary key is defined in the attributes |
|
| 217 | + if (array_key_exists($keyName, $attributes) && $attributes[$keyName] != null) { |
|
| 218 | + $this->query->insert($attributes); |
|
| 219 | + } else { |
|
| 220 | + $sequence = $aggregate->getEntityMap()->getSequence(); |
|
| 221 | 221 | |
| 222 | - $id = $this->query->insertGetId($attributes, $sequence); |
|
| 222 | + $id = $this->query->insertGetId($attributes, $sequence); |
|
| 223 | 223 | |
| 224 | - $aggregate->setEntityAttribute($keyName, $id); |
|
| 225 | - } |
|
| 224 | + $aggregate->setEntityAttribute($keyName, $id); |
|
| 225 | + } |
|
| 226 | 226 | |
| 227 | - } |
|
| 227 | + } |
|
| 228 | 228 | |
| 229 | - /** |
|
| 230 | - * Run an update statement on the entity |
|
| 231 | - * |
|
| 232 | - * @throws \InvalidArgumentException |
|
| 233 | - * |
|
| 234 | - * @return void |
|
| 235 | - */ |
|
| 236 | - protected function update() |
|
| 237 | - { |
|
| 238 | - $query = $this->query; |
|
| 229 | + /** |
|
| 230 | + * Run an update statement on the entity |
|
| 231 | + * |
|
| 232 | + * @throws \InvalidArgumentException |
|
| 233 | + * |
|
| 234 | + * @return void |
|
| 235 | + */ |
|
| 236 | + protected function update() |
|
| 237 | + { |
|
| 238 | + $query = $this->query; |
|
| 239 | 239 | |
| 240 | - $keyName = $this->aggregate->getEntityKey(); |
|
| 240 | + $keyName = $this->aggregate->getEntityKey(); |
|
| 241 | 241 | |
| 242 | - $query = $query->where($keyName, '=', $this->aggregate->getEntityId()); |
|
| 242 | + $query = $query->where($keyName, '=', $this->aggregate->getEntityId()); |
|
| 243 | 243 | |
| 244 | - $dirtyAttributes = $this->aggregate->getDirtyRawAttributes(); |
|
| 244 | + $dirtyAttributes = $this->aggregate->getDirtyRawAttributes(); |
|
| 245 | 245 | |
| 246 | - if (count($dirtyAttributes) > 0) { |
|
| 247 | - $query->update($dirtyAttributes); |
|
| 248 | - } |
|
| 249 | - } |
|
| 246 | + if (count($dirtyAttributes) > 0) { |
|
| 247 | + $query->update($dirtyAttributes); |
|
| 248 | + } |
|
| 249 | + } |
|
| 250 | 250 | } |
@@ -6,5 +6,5 @@ |
||
| 6 | 6 | |
| 7 | 7 | class EntityMapNotFoundException extends RuntimeException |
| 8 | 8 | { |
| 9 | - // |
|
| 9 | + // |
|
| 10 | 10 | } |
@@ -6,133 +6,133 @@ |
||
| 6 | 6 | |
| 7 | 7 | class Entity extends ValueObject |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * Entities Hidden Attributes, that will be discarded when converting |
|
| 11 | - * the entity to Array/Json |
|
| 12 | - * (can include any embedded object's attribute) |
|
| 13 | - * |
|
| 14 | - * @var array |
|
| 15 | - */ |
|
| 16 | - protected $hidden = []; |
|
| 9 | + /** |
|
| 10 | + * Entities Hidden Attributes, that will be discarded when converting |
|
| 11 | + * the entity to Array/Json |
|
| 12 | + * (can include any embedded object's attribute) |
|
| 13 | + * |
|
| 14 | + * @var array |
|
| 15 | + */ |
|
| 16 | + protected $hidden = []; |
|
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * Return the entity's attribute |
|
| 20 | - * @param string $key |
|
| 21 | - * @return mixed |
|
| 22 | - */ |
|
| 23 | - public function __get($key) |
|
| 24 | - { |
|
| 25 | - if ($this->hasGetMutator($key)) { |
|
| 26 | - $method = 'get' . $this->getMutatorMethod($key); |
|
| 18 | + /** |
|
| 19 | + * Return the entity's attribute |
|
| 20 | + * @param string $key |
|
| 21 | + * @return mixed |
|
| 22 | + */ |
|
| 23 | + public function __get($key) |
|
| 24 | + { |
|
| 25 | + if ($this->hasGetMutator($key)) { |
|
| 26 | + $method = 'get' . $this->getMutatorMethod($key); |
|
| 27 | 27 | |
| 28 | - $attribute = null; |
|
| 28 | + $attribute = null; |
|
| 29 | 29 | |
| 30 | - if (isset($this->attributes[$key])) { |
|
| 31 | - $attribute = $this->attributes[$key]; |
|
| 32 | - } |
|
| 30 | + if (isset($this->attributes[$key])) { |
|
| 31 | + $attribute = $this->attributes[$key]; |
|
| 32 | + } |
|
| 33 | 33 | |
| 34 | - return $this->$method($attribute); |
|
| 35 | - } |
|
| 36 | - if (!array_key_exists($key, $this->attributes)) { |
|
| 37 | - return null; |
|
| 38 | - } |
|
| 39 | - if ($this->attributes[$key] instanceof EntityProxy) { |
|
| 40 | - $this->attributes[$key] = $this->attributes[$key]->load(); |
|
| 41 | - } |
|
| 42 | - return $this->attributes[$key]; |
|
| 43 | - } |
|
| 34 | + return $this->$method($attribute); |
|
| 35 | + } |
|
| 36 | + if (!array_key_exists($key, $this->attributes)) { |
|
| 37 | + return null; |
|
| 38 | + } |
|
| 39 | + if ($this->attributes[$key] instanceof EntityProxy) { |
|
| 40 | + $this->attributes[$key] = $this->attributes[$key]->load(); |
|
| 41 | + } |
|
| 42 | + return $this->attributes[$key]; |
|
| 43 | + } |
|
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * Dynamically set attributes on the entity. |
|
| 47 | - * |
|
| 48 | - * @param string $key |
|
| 49 | - * @param mixed $value |
|
| 50 | - * @return void |
|
| 51 | - */ |
|
| 52 | - public function __set($key, $value) |
|
| 53 | - { |
|
| 54 | - if ($this->hasSetMutator($key)) { |
|
| 55 | - $method = 'set' . $this->getMutatorMethod($key); |
|
| 45 | + /** |
|
| 46 | + * Dynamically set attributes on the entity. |
|
| 47 | + * |
|
| 48 | + * @param string $key |
|
| 49 | + * @param mixed $value |
|
| 50 | + * @return void |
|
| 51 | + */ |
|
| 52 | + public function __set($key, $value) |
|
| 53 | + { |
|
| 54 | + if ($this->hasSetMutator($key)) { |
|
| 55 | + $method = 'set' . $this->getMutatorMethod($key); |
|
| 56 | 56 | |
| 57 | - $this->$method($value); |
|
| 58 | - } else { |
|
| 59 | - $this->attributes[$key] = $value; |
|
| 60 | - } |
|
| 61 | - } |
|
| 57 | + $this->$method($value); |
|
| 58 | + } else { |
|
| 59 | + $this->attributes[$key] = $value; |
|
| 60 | + } |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | - /** |
|
| 64 | - * Is a getter method defined ? |
|
| 65 | - * |
|
| 66 | - * @param string $key |
|
| 67 | - * @return boolean |
|
| 68 | - */ |
|
| 69 | - protected function hasGetMutator($key) |
|
| 70 | - { |
|
| 71 | - return method_exists($this, 'get' . $this->getMutatorMethod($key)) ? true : false; |
|
| 72 | - } |
|
| 63 | + /** |
|
| 64 | + * Is a getter method defined ? |
|
| 65 | + * |
|
| 66 | + * @param string $key |
|
| 67 | + * @return boolean |
|
| 68 | + */ |
|
| 69 | + protected function hasGetMutator($key) |
|
| 70 | + { |
|
| 71 | + return method_exists($this, 'get' . $this->getMutatorMethod($key)) ? true : false; |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | - /** |
|
| 75 | - * Is a setter method defined ? |
|
| 76 | - * |
|
| 77 | - * @param string $key |
|
| 78 | - * @return boolean |
|
| 79 | - */ |
|
| 80 | - protected function hasSetMutator($key) |
|
| 81 | - { |
|
| 82 | - return method_exists($this, 'set' . $this->getMutatorMethod($key)) ? true : false; |
|
| 83 | - } |
|
| 74 | + /** |
|
| 75 | + * Is a setter method defined ? |
|
| 76 | + * |
|
| 77 | + * @param string $key |
|
| 78 | + * @return boolean |
|
| 79 | + */ |
|
| 80 | + protected function hasSetMutator($key) |
|
| 81 | + { |
|
| 82 | + return method_exists($this, 'set' . $this->getMutatorMethod($key)) ? true : false; |
|
| 83 | + } |
|
| 84 | 84 | |
| 85 | - /** |
|
| 86 | - * @param $key |
|
| 87 | - * @return string |
|
| 88 | - */ |
|
| 89 | - protected function getMutatorMethod($key) |
|
| 90 | - { |
|
| 91 | - $key = ucwords(str_replace(['-', '_'], ' ', $key)); |
|
| 92 | - return str_replace(' ', '', $key) . "Attribute"; |
|
| 93 | - } |
|
| 85 | + /** |
|
| 86 | + * @param $key |
|
| 87 | + * @return string |
|
| 88 | + */ |
|
| 89 | + protected function getMutatorMethod($key) |
|
| 90 | + { |
|
| 91 | + $key = ucwords(str_replace(['-', '_'], ' ', $key)); |
|
| 92 | + return str_replace(' ', '', $key) . "Attribute"; |
|
| 93 | + } |
|
| 94 | 94 | |
| 95 | - /** |
|
| 96 | - * Convert every attributes to value / arrays |
|
| 97 | - * |
|
| 98 | - * @return array |
|
| 99 | - */ |
|
| 100 | - public function toArray() |
|
| 101 | - { |
|
| 102 | - // First, call the trait method before filtering |
|
| 103 | - // with Entity specific methods |
|
| 104 | - $attributes = $this->attributesToArray($this->attributes); |
|
| 95 | + /** |
|
| 96 | + * Convert every attributes to value / arrays |
|
| 97 | + * |
|
| 98 | + * @return array |
|
| 99 | + */ |
|
| 100 | + public function toArray() |
|
| 101 | + { |
|
| 102 | + // First, call the trait method before filtering |
|
| 103 | + // with Entity specific methods |
|
| 104 | + $attributes = $this->attributesToArray($this->attributes); |
|
| 105 | 105 | |
| 106 | - foreach ($this->attributes as $key => $attribute) { |
|
| 107 | - if (in_array($key, $this->hidden)) { |
|
| 108 | - unset($attributes[$key]); |
|
| 109 | - continue; |
|
| 110 | - } |
|
| 111 | - if ($this->hasGetMutator($key)) { |
|
| 112 | - $method = 'get' . $this->getMutatorMethod($key); |
|
| 113 | - $attributes[$key] = $this->$method($attribute); |
|
| 114 | - } |
|
| 115 | - } |
|
| 116 | - return $attributes; |
|
| 117 | - } |
|
| 106 | + foreach ($this->attributes as $key => $attribute) { |
|
| 107 | + if (in_array($key, $this->hidden)) { |
|
| 108 | + unset($attributes[$key]); |
|
| 109 | + continue; |
|
| 110 | + } |
|
| 111 | + if ($this->hasGetMutator($key)) { |
|
| 112 | + $method = 'get' . $this->getMutatorMethod($key); |
|
| 113 | + $attributes[$key] = $this->$method($attribute); |
|
| 114 | + } |
|
| 115 | + } |
|
| 116 | + return $attributes; |
|
| 117 | + } |
|
| 118 | 118 | |
| 119 | - /** |
|
| 120 | - * Fill an entity with key-value pairs |
|
| 121 | - * |
|
| 122 | - * @param array $attributes |
|
| 123 | - * @return void |
|
| 124 | - */ |
|
| 125 | - public function fill(array $attributes) |
|
| 126 | - { |
|
| 127 | - foreach ($attributes as $key => $attribute) { |
|
| 119 | + /** |
|
| 120 | + * Fill an entity with key-value pairs |
|
| 121 | + * |
|
| 122 | + * @param array $attributes |
|
| 123 | + * @return void |
|
| 124 | + */ |
|
| 125 | + public function fill(array $attributes) |
|
| 126 | + { |
|
| 127 | + foreach ($attributes as $key => $attribute) { |
|
| 128 | 128 | |
| 129 | - if ($this->hasSetMutator($key)) { |
|
| 130 | - $method = 'set' . $this->getMutatorMethod($key); |
|
| 131 | - $this->attributes[$key] = $this->$method($attribute); |
|
| 132 | - } |
|
| 133 | - else { |
|
| 134 | - $this->attributes[$key] = $attribute; |
|
| 135 | - } |
|
| 136 | - } |
|
| 137 | - } |
|
| 129 | + if ($this->hasSetMutator($key)) { |
|
| 130 | + $method = 'set' . $this->getMutatorMethod($key); |
|
| 131 | + $this->attributes[$key] = $this->$method($attribute); |
|
| 132 | + } |
|
| 133 | + else { |
|
| 134 | + $this->attributes[$key] = $attribute; |
|
| 135 | + } |
|
| 136 | + } |
|
| 137 | + } |
|
| 138 | 138 | } |
@@ -23,7 +23,7 @@ discard block |
||
| 23 | 23 | public function __get($key) |
| 24 | 24 | { |
| 25 | 25 | if ($this->hasGetMutator($key)) { |
| 26 | - $method = 'get' . $this->getMutatorMethod($key); |
|
| 26 | + $method = 'get'.$this->getMutatorMethod($key); |
|
| 27 | 27 | |
| 28 | 28 | $attribute = null; |
| 29 | 29 | |
@@ -52,7 +52,7 @@ discard block |
||
| 52 | 52 | public function __set($key, $value) |
| 53 | 53 | { |
| 54 | 54 | if ($this->hasSetMutator($key)) { |
| 55 | - $method = 'set' . $this->getMutatorMethod($key); |
|
| 55 | + $method = 'set'.$this->getMutatorMethod($key); |
|
| 56 | 56 | |
| 57 | 57 | $this->$method($value); |
| 58 | 58 | } else { |
@@ -68,7 +68,7 @@ discard block |
||
| 68 | 68 | */ |
| 69 | 69 | protected function hasGetMutator($key) |
| 70 | 70 | { |
| 71 | - return method_exists($this, 'get' . $this->getMutatorMethod($key)) ? true : false; |
|
| 71 | + return method_exists($this, 'get'.$this->getMutatorMethod($key)) ? true : false; |
|
| 72 | 72 | } |
| 73 | 73 | |
| 74 | 74 | /** |
@@ -79,7 +79,7 @@ discard block |
||
| 79 | 79 | */ |
| 80 | 80 | protected function hasSetMutator($key) |
| 81 | 81 | { |
| 82 | - return method_exists($this, 'set' . $this->getMutatorMethod($key)) ? true : false; |
|
| 82 | + return method_exists($this, 'set'.$this->getMutatorMethod($key)) ? true : false; |
|
| 83 | 83 | } |
| 84 | 84 | |
| 85 | 85 | /** |
@@ -89,7 +89,7 @@ discard block |
||
| 89 | 89 | protected function getMutatorMethod($key) |
| 90 | 90 | { |
| 91 | 91 | $key = ucwords(str_replace(['-', '_'], ' ', $key)); |
| 92 | - return str_replace(' ', '', $key) . "Attribute"; |
|
| 92 | + return str_replace(' ', '', $key)."Attribute"; |
|
| 93 | 93 | } |
| 94 | 94 | |
| 95 | 95 | /** |
@@ -109,7 +109,7 @@ discard block |
||
| 109 | 109 | continue; |
| 110 | 110 | } |
| 111 | 111 | if ($this->hasGetMutator($key)) { |
| 112 | - $method = 'get' . $this->getMutatorMethod($key); |
|
| 112 | + $method = 'get'.$this->getMutatorMethod($key); |
|
| 113 | 113 | $attributes[$key] = $this->$method($attribute); |
| 114 | 114 | } |
| 115 | 115 | } |
@@ -127,7 +127,7 @@ discard block |
||
| 127 | 127 | foreach ($attributes as $key => $attribute) { |
| 128 | 128 | |
| 129 | 129 | if ($this->hasSetMutator($key)) { |
| 130 | - $method = 'set' . $this->getMutatorMethod($key); |
|
| 130 | + $method = 'set'.$this->getMutatorMethod($key); |
|
| 131 | 131 | $this->attributes[$key] = $this->$method($attribute); |
| 132 | 132 | } |
| 133 | 133 | else { |
@@ -129,8 +129,7 @@ |
||
| 129 | 129 | if ($this->hasSetMutator($key)) { |
| 130 | 130 | $method = 'set' . $this->getMutatorMethod($key); |
| 131 | 131 | $this->attributes[$key] = $this->$method($attribute); |
| 132 | - } |
|
| 133 | - else { |
|
| 132 | + } else { |
|
| 134 | 133 | $this->attributes[$key] = $attribute; |
| 135 | 134 | } |
| 136 | 135 | } |
@@ -6,6 +6,9 @@ |
||
| 6 | 6 | |
| 7 | 7 | class Avatar extends Entity |
| 8 | 8 | { |
| 9 | + /** |
|
| 10 | + * @param string $name |
|
| 11 | + */ |
|
| 9 | 12 | public function __construct($name, User $user = null) |
| 10 | 13 | { |
| 11 | 14 | $this->name = $name; |
@@ -6,14 +6,14 @@ |
||
| 6 | 6 | |
| 7 | 7 | class Avatar extends Entity |
| 8 | 8 | { |
| 9 | - public function __construct($name, User $user = null) |
|
| 10 | - { |
|
| 11 | - $this->name = $name; |
|
| 12 | - $this->user = $user; |
|
| 13 | - } |
|
| 9 | + public function __construct($name, User $user = null) |
|
| 10 | + { |
|
| 11 | + $this->name = $name; |
|
| 12 | + $this->user = $user; |
|
| 13 | + } |
|
| 14 | 14 | |
| 15 | - public function getPathAttribute() |
|
| 16 | - { |
|
| 17 | - return $this->image->path; |
|
| 18 | - } |
|
| 15 | + public function getPathAttribute() |
|
| 16 | + { |
|
| 17 | + return $this->image->path; |
|
| 18 | + } |
|
| 19 | 19 | } |