@@ -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 | } |
@@ -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 | } |
@@ -141,7 +141,7 @@ discard block |
||
| 141 | 141 | * Create a mapper for a given entity |
| 142 | 142 | * |
| 143 | 143 | * @param \Analogue\ORM\Mappable|string|array|\Traversable $entity |
| 144 | - * @param mixed $entityMap |
|
| 144 | + * @param null|EntityMap $entityMap |
|
| 145 | 145 | * @throws MappingException |
| 146 | 146 | * @throws \InvalidArgumentException |
| 147 | 147 | * @return Mapper |
@@ -243,7 +243,7 @@ discard block |
||
| 243 | 243 | /** |
| 244 | 244 | * Check if the entity is already registered |
| 245 | 245 | * |
| 246 | - * @param string|Entity $entity |
|
| 246 | + * @param string $entity |
|
| 247 | 247 | * @return boolean |
| 248 | 248 | */ |
| 249 | 249 | public function isRegisteredEntity($entity) |
@@ -312,7 +312,7 @@ discard block |
||
| 312 | 312 | /** |
| 313 | 313 | * Register an entity |
| 314 | 314 | * |
| 315 | - * @param string|\Analogue\ORM\Mappable $entity entity's class name |
|
| 315 | + * @param string $entity entity's class name |
|
| 316 | 316 | * @param string|EntityMap $entityMap map's class name |
| 317 | 317 | * @throws MappingException |
| 318 | 318 | * @return void |
@@ -5,7 +5,6 @@ |
||
| 5 | 5 | use Analogue\ORM\Entity; |
| 6 | 6 | use Analogue\ORM\Plugins\AnaloguePluginInterface; |
| 7 | 7 | use Analogue\ORM\ValueMap; |
| 8 | -use Exception; |
|
| 9 | 8 | use Analogue\ORM\EntityMap; |
| 10 | 9 | use Analogue\ORM\Repository; |
| 11 | 10 | use Illuminate\Support\Collection; |
@@ -177,7 +177,7 @@ discard block |
||
| 177 | 177 | // We first check if the entity is traversable and we'll resolve |
| 178 | 178 | // the entity based on the first item of the object. |
| 179 | 179 | if ($this->isTraversable($entity)) { |
| 180 | - if (! count($entity)) { |
|
| 180 | + if (!count($entity)) { |
|
| 181 | 181 | throw new \InvalidArgumentException('Length of Entity collection must be greater than 0'); |
| 182 | 182 | } |
| 183 | 183 | |
@@ -341,7 +341,7 @@ discard block |
||
| 341 | 341 | } |
| 342 | 342 | |
| 343 | 343 | if (!$entityMap instanceof EntityMap) { |
| 344 | - throw new MappingException(get_class($entityMap) . ' must be an instance of EntityMap.'); |
|
| 344 | + throw new MappingException(get_class($entityMap).' must be an instance of EntityMap.'); |
|
| 345 | 345 | } |
| 346 | 346 | |
| 347 | 347 | $entityMap->setClass($entity); |
@@ -359,8 +359,8 @@ discard block |
||
| 359 | 359 | */ |
| 360 | 360 | protected function getEntityMapInstanceFor($entity) |
| 361 | 361 | { |
| 362 | - if (class_exists($entity . 'Map')) { |
|
| 363 | - $map = $entity . 'Map'; |
|
| 362 | + if (class_exists($entity.'Map')) { |
|
| 363 | + $map = $entity.'Map'; |
|
| 364 | 364 | $map = new $map; |
| 365 | 365 | return $map; |
| 366 | 366 | } |
@@ -388,7 +388,7 @@ discard block |
||
| 388 | 388 | protected function getMapFromNamespaces($class) |
| 389 | 389 | { |
| 390 | 390 | foreach ($this->customMapNamespaces as $namespace) { |
| 391 | - if($map = $this->findMapInNamespace($class, $namespace)) { |
|
| 391 | + if ($map = $this->findMapInNamespace($class, $namespace)) { |
|
| 392 | 392 | return $map; |
| 393 | 393 | } |
| 394 | 394 | } |
@@ -530,15 +530,15 @@ discard block |
||
| 530 | 530 | |
| 531 | 531 | // First, we'll look into registered namespaces for Entity Maps, |
| 532 | 532 | // if any. Then we'll fallback to the same namespace of the object |
| 533 | - if(! $valueMap = $this->getMapFromNamespaces($valueObject)) { |
|
| 534 | - $valueMap = $valueObject . 'Map'; |
|
| 533 | + if (!$valueMap = $this->getMapFromNamespaces($valueObject)) { |
|
| 534 | + $valueMap = $valueObject.'Map'; |
|
| 535 | 535 | } |
| 536 | 536 | else { |
| 537 | 537 | $valueMap = get_class($valueMap); |
| 538 | 538 | } |
| 539 | 539 | } |
| 540 | 540 | |
| 541 | - if (! class_exists($valueMap)) { |
|
| 541 | + if (!class_exists($valueMap)) { |
|
| 542 | 542 | throw new MappingException("$valueMap doesn't exists"); |
| 543 | 543 | } |
| 544 | 544 | |
@@ -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 | } |
@@ -21,656 +21,656 @@ |
||
| 21 | 21 | */ |
| 22 | 22 | class Manager |
| 23 | 23 | { |
| 24 | - /** |
|
| 25 | - * Manager instance |
|
| 26 | - * |
|
| 27 | - * @var Manager |
|
| 28 | - */ |
|
| 29 | - protected static $instance; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * Driver Manager |
|
| 33 | - * |
|
| 34 | - * @var \Analogue\ORM\Drivers\Manager |
|
| 35 | - */ |
|
| 36 | - protected $drivers; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * Registered entity classes and corresponding map objects. |
|
| 40 | - * |
|
| 41 | - * @var array |
|
| 42 | - */ |
|
| 43 | - protected $entityClasses = []; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * Key value store of ValueObject Classes and corresponding map classes |
|
| 47 | - * |
|
| 48 | - * @var array|ValueMap[] |
|
| 49 | - */ |
|
| 50 | - protected $valueClasses = []; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * Morph map |
|
| 54 | - */ |
|
| 55 | - protected $morphMap = []; |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * Loaded Mappers |
|
| 59 | - * |
|
| 60 | - * @var array |
|
| 61 | - */ |
|
| 62 | - protected $mappers = []; |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * Loaded Repositories |
|
| 66 | - * |
|
| 67 | - * @var array |
|
| 68 | - */ |
|
| 69 | - protected $repositories = []; |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * Event dispatcher instance |
|
| 73 | - * |
|
| 74 | - * @var \Illuminate\Contracts\Events\Dispatcher |
|
| 75 | - */ |
|
| 76 | - protected $eventDispatcher; |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * Available Analogue Events |
|
| 80 | - * |
|
| 81 | - * @var array |
|
| 82 | - */ |
|
| 83 | - protected $events = [ |
|
| 84 | - 'initializing', |
|
| 85 | - 'initialized', |
|
| 86 | - 'store', |
|
| 87 | - 'stored', |
|
| 88 | - 'creating', |
|
| 89 | - 'created', |
|
| 90 | - 'updating', |
|
| 91 | - 'updated', |
|
| 92 | - 'deleting', |
|
| 93 | - 'deleted', |
|
| 94 | - ]; |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * If strictMode is set to true, Manager will throw |
|
| 98 | - * an exception if no entityMap class are registered |
|
| 99 | - * for a given entity class. |
|
| 100 | - * |
|
| 101 | - * @var boolean |
|
| 102 | - */ |
|
| 103 | - protected $strictMode = true; |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * We can add namespaces in this array where the manager |
|
| 107 | - * will look for when auto registering entityMaps. |
|
| 108 | - * |
|
| 109 | - * @var array |
|
| 110 | - */ |
|
| 111 | - protected $customMapNamespaces = []; |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * @param \Analogue\ORM\Drivers\Manager $driverManager |
|
| 115 | - * @param Dispatcher $event |
|
| 116 | - */ |
|
| 117 | - public function __construct(DriverManager $driverManager, Dispatcher $event) |
|
| 118 | - { |
|
| 119 | - $this->drivers = $driverManager; |
|
| 120 | - |
|
| 121 | - $this->eventDispatcher = $event; |
|
| 122 | - |
|
| 123 | - static::$instance = $this; |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * Create a mapper for a given entity (static alias) |
|
| 128 | - * |
|
| 129 | - * @param \Analogue\ORM\Mappable|string $entity |
|
| 130 | - * @param null|EntityMap $entityMap |
|
| 131 | - * @throws MappingException |
|
| 132 | - * @return Mapper |
|
| 133 | - * @throws \InvalidArgumentException |
|
| 134 | - */ |
|
| 135 | - public static function getMapper($entity, $entityMap = null) |
|
| 136 | - { |
|
| 137 | - return static::$instance->mapper($entity, $entityMap); |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * Create a mapper for a given entity |
|
| 142 | - * |
|
| 143 | - * @param \Analogue\ORM\Mappable|string|array|\Traversable $entity |
|
| 144 | - * @param mixed $entityMap |
|
| 145 | - * @throws MappingException |
|
| 146 | - * @throws \InvalidArgumentException |
|
| 147 | - * @return Mapper |
|
| 148 | - */ |
|
| 149 | - public function mapper($entity, $entityMap = null) |
|
| 150 | - { |
|
| 151 | - if ($entity instanceof Wrapper) { |
|
| 152 | - throw new MappingException('Tried to instantiate mapper on wrapped Entity'); |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - $entity = $this->resolveEntityClass($entity); |
|
| 156 | - |
|
| 157 | - $entity = $this->getInverseMorphMap($entity); |
|
| 158 | - |
|
| 159 | - // Return existing mapper instance if exists. |
|
| 160 | - if (array_key_exists($entity, $this->mappers)) { |
|
| 161 | - return $this->mappers[$entity]; |
|
| 162 | - } else { |
|
| 163 | - return $this->buildMapper($entity, $entityMap); |
|
| 164 | - } |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - /** |
|
| 168 | - * This method resolve entity class from mappable instances or iterators |
|
| 169 | - * |
|
| 170 | - * @param \Analogue\ORM\Mappable|string|array|\Traversable $entity |
|
| 171 | - * @return string |
|
| 172 | - * |
|
| 173 | - * @throws \InvalidArgumentException |
|
| 174 | - */ |
|
| 175 | - protected function resolveEntityClass($entity) |
|
| 176 | - { |
|
| 177 | - // We first check if the entity is traversable and we'll resolve |
|
| 178 | - // the entity based on the first item of the object. |
|
| 179 | - if ($this->isTraversable($entity)) { |
|
| 180 | - if (! count($entity)) { |
|
| 181 | - throw new \InvalidArgumentException('Length of Entity collection must be greater than 0'); |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - $firstEntityItem = ($entity instanceof \Iterator) |
|
| 185 | - ? $entity->current() |
|
| 186 | - : current($entity); |
|
| 187 | - |
|
| 188 | - return $this->resolveEntityClass($firstEntityItem); |
|
| 189 | - } |
|
| 24 | + /** |
|
| 25 | + * Manager instance |
|
| 26 | + * |
|
| 27 | + * @var Manager |
|
| 28 | + */ |
|
| 29 | + protected static $instance; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * Driver Manager |
|
| 33 | + * |
|
| 34 | + * @var \Analogue\ORM\Drivers\Manager |
|
| 35 | + */ |
|
| 36 | + protected $drivers; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * Registered entity classes and corresponding map objects. |
|
| 40 | + * |
|
| 41 | + * @var array |
|
| 42 | + */ |
|
| 43 | + protected $entityClasses = []; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * Key value store of ValueObject Classes and corresponding map classes |
|
| 47 | + * |
|
| 48 | + * @var array|ValueMap[] |
|
| 49 | + */ |
|
| 50 | + protected $valueClasses = []; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * Morph map |
|
| 54 | + */ |
|
| 55 | + protected $morphMap = []; |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * Loaded Mappers |
|
| 59 | + * |
|
| 60 | + * @var array |
|
| 61 | + */ |
|
| 62 | + protected $mappers = []; |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * Loaded Repositories |
|
| 66 | + * |
|
| 67 | + * @var array |
|
| 68 | + */ |
|
| 69 | + protected $repositories = []; |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * Event dispatcher instance |
|
| 73 | + * |
|
| 74 | + * @var \Illuminate\Contracts\Events\Dispatcher |
|
| 75 | + */ |
|
| 76 | + protected $eventDispatcher; |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * Available Analogue Events |
|
| 80 | + * |
|
| 81 | + * @var array |
|
| 82 | + */ |
|
| 83 | + protected $events = [ |
|
| 84 | + 'initializing', |
|
| 85 | + 'initialized', |
|
| 86 | + 'store', |
|
| 87 | + 'stored', |
|
| 88 | + 'creating', |
|
| 89 | + 'created', |
|
| 90 | + 'updating', |
|
| 91 | + 'updated', |
|
| 92 | + 'deleting', |
|
| 93 | + 'deleted', |
|
| 94 | + ]; |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * If strictMode is set to true, Manager will throw |
|
| 98 | + * an exception if no entityMap class are registered |
|
| 99 | + * for a given entity class. |
|
| 100 | + * |
|
| 101 | + * @var boolean |
|
| 102 | + */ |
|
| 103 | + protected $strictMode = true; |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * We can add namespaces in this array where the manager |
|
| 107 | + * will look for when auto registering entityMaps. |
|
| 108 | + * |
|
| 109 | + * @var array |
|
| 110 | + */ |
|
| 111 | + protected $customMapNamespaces = []; |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * @param \Analogue\ORM\Drivers\Manager $driverManager |
|
| 115 | + * @param Dispatcher $event |
|
| 116 | + */ |
|
| 117 | + public function __construct(DriverManager $driverManager, Dispatcher $event) |
|
| 118 | + { |
|
| 119 | + $this->drivers = $driverManager; |
|
| 120 | + |
|
| 121 | + $this->eventDispatcher = $event; |
|
| 122 | + |
|
| 123 | + static::$instance = $this; |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * Create a mapper for a given entity (static alias) |
|
| 128 | + * |
|
| 129 | + * @param \Analogue\ORM\Mappable|string $entity |
|
| 130 | + * @param null|EntityMap $entityMap |
|
| 131 | + * @throws MappingException |
|
| 132 | + * @return Mapper |
|
| 133 | + * @throws \InvalidArgumentException |
|
| 134 | + */ |
|
| 135 | + public static function getMapper($entity, $entityMap = null) |
|
| 136 | + { |
|
| 137 | + return static::$instance->mapper($entity, $entityMap); |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * Create a mapper for a given entity |
|
| 142 | + * |
|
| 143 | + * @param \Analogue\ORM\Mappable|string|array|\Traversable $entity |
|
| 144 | + * @param mixed $entityMap |
|
| 145 | + * @throws MappingException |
|
| 146 | + * @throws \InvalidArgumentException |
|
| 147 | + * @return Mapper |
|
| 148 | + */ |
|
| 149 | + public function mapper($entity, $entityMap = null) |
|
| 150 | + { |
|
| 151 | + if ($entity instanceof Wrapper) { |
|
| 152 | + throw new MappingException('Tried to instantiate mapper on wrapped Entity'); |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + $entity = $this->resolveEntityClass($entity); |
|
| 156 | + |
|
| 157 | + $entity = $this->getInverseMorphMap($entity); |
|
| 158 | + |
|
| 159 | + // Return existing mapper instance if exists. |
|
| 160 | + if (array_key_exists($entity, $this->mappers)) { |
|
| 161 | + return $this->mappers[$entity]; |
|
| 162 | + } else { |
|
| 163 | + return $this->buildMapper($entity, $entityMap); |
|
| 164 | + } |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + /** |
|
| 168 | + * This method resolve entity class from mappable instances or iterators |
|
| 169 | + * |
|
| 170 | + * @param \Analogue\ORM\Mappable|string|array|\Traversable $entity |
|
| 171 | + * @return string |
|
| 172 | + * |
|
| 173 | + * @throws \InvalidArgumentException |
|
| 174 | + */ |
|
| 175 | + protected function resolveEntityClass($entity) |
|
| 176 | + { |
|
| 177 | + // We first check if the entity is traversable and we'll resolve |
|
| 178 | + // the entity based on the first item of the object. |
|
| 179 | + if ($this->isTraversable($entity)) { |
|
| 180 | + if (! count($entity)) { |
|
| 181 | + throw new \InvalidArgumentException('Length of Entity collection must be greater than 0'); |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + $firstEntityItem = ($entity instanceof \Iterator) |
|
| 185 | + ? $entity->current() |
|
| 186 | + : current($entity); |
|
| 187 | + |
|
| 188 | + return $this->resolveEntityClass($firstEntityItem); |
|
| 189 | + } |
|
| 190 | 190 | |
| 191 | - if (is_object($entity)) { |
|
| 192 | - return get_class($entity); |
|
| 193 | - } |
|
| 191 | + if (is_object($entity)) { |
|
| 192 | + return get_class($entity); |
|
| 193 | + } |
|
| 194 | 194 | |
| 195 | - if (is_string($entity)) { |
|
| 196 | - return $entity; |
|
| 197 | - } |
|
| 195 | + if (is_string($entity)) { |
|
| 196 | + return $entity; |
|
| 197 | + } |
|
| 198 | 198 | |
| 199 | - throw new \InvalidArgumentException('Invalid entity type'); |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - /** |
|
| 203 | - * @param string $key |
|
| 204 | - * @return string |
|
| 205 | - */ |
|
| 206 | - public function getInverseMorphMap($key) |
|
| 207 | - { |
|
| 208 | - return array_key_exists($key, $this->morphMap) ? $this->morphMap[$key] : $key; |
|
| 209 | - } |
|
| 210 | - |
|
| 211 | - /** |
|
| 212 | - * Build a new Mapper instance for a given Entity |
|
| 213 | - * |
|
| 214 | - * @param string $entity |
|
| 215 | - * @param $entityMap |
|
| 216 | - * @throws MappingException |
|
| 217 | - * @return Mapper |
|
| 218 | - */ |
|
| 219 | - protected function buildMapper($entity, $entityMap) |
|
| 220 | - { |
|
| 221 | - // If an EntityMap hasn't been manually registered by the user |
|
| 222 | - // register it at runtime. |
|
| 223 | - if (!$this->isRegisteredEntity($entity)) { |
|
| 224 | - $this->register($entity, $entityMap); |
|
| 225 | - } |
|
| 226 | - |
|
| 227 | - $entityMap = $this->entityClasses[$entity]; |
|
| 228 | - |
|
| 229 | - $factory = new MapperFactory($this->drivers, $this->eventDispatcher, $this); |
|
| 230 | - |
|
| 231 | - $mapper = $factory->make($entity, $entityMap); |
|
| 232 | - |
|
| 233 | - $this->mappers[$entity] = $mapper; |
|
| 234 | - |
|
| 235 | - // At this point we can safely call the boot() method on the entityMap as |
|
| 236 | - // the mapper is now instantiated & registered within the manager. |
|
| 237 | - |
|
| 238 | - $mapper->getEntityMap()->boot(); |
|
| 239 | - |
|
| 240 | - return $mapper; |
|
| 241 | - } |
|
| 242 | - |
|
| 243 | - /** |
|
| 244 | - * Check if the entity is already registered |
|
| 245 | - * |
|
| 246 | - * @param string|Entity $entity |
|
| 247 | - * @return boolean |
|
| 248 | - */ |
|
| 249 | - public function isRegisteredEntity($entity) |
|
| 250 | - { |
|
| 251 | - if (!is_string($entity)) { |
|
| 252 | - $entity = get_class($entity); |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - return array_key_exists($entity, $this->entityClasses); |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - /** |
|
| 259 | - * Return an array containing registered entities & entityMap instances |
|
| 260 | - * |
|
| 261 | - * @return array |
|
| 262 | - */ |
|
| 263 | - public function getRegisteredEntities() |
|
| 264 | - { |
|
| 265 | - return $this->entityClasses; |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - /** |
|
| 269 | - * Check if a value class is already registered |
|
| 270 | - * |
|
| 271 | - * @param string|sdtClass $object |
|
| 272 | - * @return boolean |
|
| 273 | - * |
|
| 274 | - */ |
|
| 275 | - public function isRegisteredValueObject($object) |
|
| 276 | - { |
|
| 277 | - if (!is_string($object)) { |
|
| 278 | - $object = get_class($object); |
|
| 279 | - } |
|
| 280 | - |
|
| 281 | - return array_key_exists($object, $this->valueClasses); |
|
| 282 | - } |
|
| 283 | - |
|
| 284 | - /** |
|
| 285 | - * Return true if an object is an array or iterator |
|
| 286 | - * |
|
| 287 | - * @param mixed $argument |
|
| 288 | - * @return boolean |
|
| 289 | - */ |
|
| 290 | - public function isTraversable($argument) |
|
| 291 | - { |
|
| 292 | - return $argument instanceof \Traversable || is_array($argument); |
|
| 293 | - } |
|
| 294 | - |
|
| 295 | - /** |
|
| 296 | - * Set strict mode for entityMap instantiation |
|
| 297 | - * |
|
| 298 | - * @param boolean $mode |
|
| 299 | - */ |
|
| 300 | - public function setStrictMode($mode) |
|
| 301 | - { |
|
| 302 | - $this->strictMode = $mode; |
|
| 303 | - } |
|
| 304 | - |
|
| 305 | - /** |
|
| 306 | - * Register a namespace in where Analogue |
|
| 307 | - * will scan for EntityMaps & ValueMaps |
|
| 308 | - * |
|
| 309 | - * @param string $namespace |
|
| 310 | - * @return void |
|
| 311 | - */ |
|
| 312 | - public function registerMapNamespace($namespace) |
|
| 313 | - { |
|
| 314 | - // Add a trailing antislash to namespace if not present |
|
| 315 | - if (substr("testers", -1) != "\\") { |
|
| 316 | - $namespace = $namespace."\\"; |
|
| 317 | - } |
|
| 318 | - |
|
| 319 | - $this->customMapNamespaces[] = $namespace; |
|
| 320 | - } |
|
| 321 | - |
|
| 322 | - /** |
|
| 323 | - * Register an entity |
|
| 324 | - * |
|
| 325 | - * @param string|\Analogue\ORM\Mappable $entity entity's class name |
|
| 326 | - * @param string|EntityMap $entityMap map's class name |
|
| 327 | - * @throws MappingException |
|
| 328 | - * @return void |
|
| 329 | - */ |
|
| 330 | - public function register($entity, $entityMap = null) |
|
| 331 | - { |
|
| 332 | - // If an object is provider, get the class name from it |
|
| 333 | - if (!is_string($entity)) { |
|
| 334 | - $entity = get_class($entity); |
|
| 335 | - } |
|
| 336 | - |
|
| 337 | - if ($this->isRegisteredEntity($entity)) { |
|
| 338 | - throw new MappingException("Entity $entity is already registered."); |
|
| 339 | - } |
|
| 340 | - |
|
| 341 | - if (!class_exists($entity)) { |
|
| 342 | - throw new MappingException("Class $entity does not exists"); |
|
| 343 | - } |
|
| 344 | - |
|
| 345 | - if ($entityMap === null) { |
|
| 346 | - $entityMap = $this->getEntityMapInstanceFor($entity); |
|
| 347 | - } |
|
| 348 | - |
|
| 349 | - if (is_string($entityMap)) { |
|
| 350 | - $entityMap = new $entityMap; |
|
| 351 | - } |
|
| 352 | - |
|
| 353 | - if (!$entityMap instanceof EntityMap) { |
|
| 354 | - throw new MappingException(get_class($entityMap) . ' must be an instance of EntityMap.'); |
|
| 355 | - } |
|
| 356 | - |
|
| 357 | - $entityMap->setClass($entity); |
|
| 358 | - |
|
| 359 | - $this->entityClasses[$entity] = $entityMap; |
|
| 360 | - } |
|
| 361 | - |
|
| 362 | - /** |
|
| 363 | - * Get the entity map instance for a custom entity |
|
| 364 | - * |
|
| 365 | - * @param string $entity |
|
| 366 | - * @return \Analogue\ORM\EntityMap |
|
| 367 | - */ |
|
| 368 | - protected function getEntityMapInstanceFor($entity) |
|
| 369 | - { |
|
| 370 | - if (class_exists($entity . 'Map')) { |
|
| 371 | - $map = $entity . 'Map'; |
|
| 372 | - $map = new $map; |
|
| 373 | - return $map; |
|
| 374 | - } |
|
| 375 | - |
|
| 376 | - if ($map = $this->getMapFromNamespaces($entity)) { |
|
| 377 | - return $map; |
|
| 378 | - } |
|
| 199 | + throw new \InvalidArgumentException('Invalid entity type'); |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + /** |
|
| 203 | + * @param string $key |
|
| 204 | + * @return string |
|
| 205 | + */ |
|
| 206 | + public function getInverseMorphMap($key) |
|
| 207 | + { |
|
| 208 | + return array_key_exists($key, $this->morphMap) ? $this->morphMap[$key] : $key; |
|
| 209 | + } |
|
| 210 | + |
|
| 211 | + /** |
|
| 212 | + * Build a new Mapper instance for a given Entity |
|
| 213 | + * |
|
| 214 | + * @param string $entity |
|
| 215 | + * @param $entityMap |
|
| 216 | + * @throws MappingException |
|
| 217 | + * @return Mapper |
|
| 218 | + */ |
|
| 219 | + protected function buildMapper($entity, $entityMap) |
|
| 220 | + { |
|
| 221 | + // If an EntityMap hasn't been manually registered by the user |
|
| 222 | + // register it at runtime. |
|
| 223 | + if (!$this->isRegisteredEntity($entity)) { |
|
| 224 | + $this->register($entity, $entityMap); |
|
| 225 | + } |
|
| 226 | + |
|
| 227 | + $entityMap = $this->entityClasses[$entity]; |
|
| 228 | + |
|
| 229 | + $factory = new MapperFactory($this->drivers, $this->eventDispatcher, $this); |
|
| 230 | + |
|
| 231 | + $mapper = $factory->make($entity, $entityMap); |
|
| 232 | + |
|
| 233 | + $this->mappers[$entity] = $mapper; |
|
| 234 | + |
|
| 235 | + // At this point we can safely call the boot() method on the entityMap as |
|
| 236 | + // the mapper is now instantiated & registered within the manager. |
|
| 237 | + |
|
| 238 | + $mapper->getEntityMap()->boot(); |
|
| 239 | + |
|
| 240 | + return $mapper; |
|
| 241 | + } |
|
| 242 | + |
|
| 243 | + /** |
|
| 244 | + * Check if the entity is already registered |
|
| 245 | + * |
|
| 246 | + * @param string|Entity $entity |
|
| 247 | + * @return boolean |
|
| 248 | + */ |
|
| 249 | + public function isRegisteredEntity($entity) |
|
| 250 | + { |
|
| 251 | + if (!is_string($entity)) { |
|
| 252 | + $entity = get_class($entity); |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + return array_key_exists($entity, $this->entityClasses); |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + /** |
|
| 259 | + * Return an array containing registered entities & entityMap instances |
|
| 260 | + * |
|
| 261 | + * @return array |
|
| 262 | + */ |
|
| 263 | + public function getRegisteredEntities() |
|
| 264 | + { |
|
| 265 | + return $this->entityClasses; |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + /** |
|
| 269 | + * Check if a value class is already registered |
|
| 270 | + * |
|
| 271 | + * @param string|sdtClass $object |
|
| 272 | + * @return boolean |
|
| 273 | + * |
|
| 274 | + */ |
|
| 275 | + public function isRegisteredValueObject($object) |
|
| 276 | + { |
|
| 277 | + if (!is_string($object)) { |
|
| 278 | + $object = get_class($object); |
|
| 279 | + } |
|
| 280 | + |
|
| 281 | + return array_key_exists($object, $this->valueClasses); |
|
| 282 | + } |
|
| 283 | + |
|
| 284 | + /** |
|
| 285 | + * Return true if an object is an array or iterator |
|
| 286 | + * |
|
| 287 | + * @param mixed $argument |
|
| 288 | + * @return boolean |
|
| 289 | + */ |
|
| 290 | + public function isTraversable($argument) |
|
| 291 | + { |
|
| 292 | + return $argument instanceof \Traversable || is_array($argument); |
|
| 293 | + } |
|
| 294 | + |
|
| 295 | + /** |
|
| 296 | + * Set strict mode for entityMap instantiation |
|
| 297 | + * |
|
| 298 | + * @param boolean $mode |
|
| 299 | + */ |
|
| 300 | + public function setStrictMode($mode) |
|
| 301 | + { |
|
| 302 | + $this->strictMode = $mode; |
|
| 303 | + } |
|
| 304 | + |
|
| 305 | + /** |
|
| 306 | + * Register a namespace in where Analogue |
|
| 307 | + * will scan for EntityMaps & ValueMaps |
|
| 308 | + * |
|
| 309 | + * @param string $namespace |
|
| 310 | + * @return void |
|
| 311 | + */ |
|
| 312 | + public function registerMapNamespace($namespace) |
|
| 313 | + { |
|
| 314 | + // Add a trailing antislash to namespace if not present |
|
| 315 | + if (substr("testers", -1) != "\\") { |
|
| 316 | + $namespace = $namespace."\\"; |
|
| 317 | + } |
|
| 318 | + |
|
| 319 | + $this->customMapNamespaces[] = $namespace; |
|
| 320 | + } |
|
| 321 | + |
|
| 322 | + /** |
|
| 323 | + * Register an entity |
|
| 324 | + * |
|
| 325 | + * @param string|\Analogue\ORM\Mappable $entity entity's class name |
|
| 326 | + * @param string|EntityMap $entityMap map's class name |
|
| 327 | + * @throws MappingException |
|
| 328 | + * @return void |
|
| 329 | + */ |
|
| 330 | + public function register($entity, $entityMap = null) |
|
| 331 | + { |
|
| 332 | + // If an object is provider, get the class name from it |
|
| 333 | + if (!is_string($entity)) { |
|
| 334 | + $entity = get_class($entity); |
|
| 335 | + } |
|
| 336 | + |
|
| 337 | + if ($this->isRegisteredEntity($entity)) { |
|
| 338 | + throw new MappingException("Entity $entity is already registered."); |
|
| 339 | + } |
|
| 340 | + |
|
| 341 | + if (!class_exists($entity)) { |
|
| 342 | + throw new MappingException("Class $entity does not exists"); |
|
| 343 | + } |
|
| 344 | + |
|
| 345 | + if ($entityMap === null) { |
|
| 346 | + $entityMap = $this->getEntityMapInstanceFor($entity); |
|
| 347 | + } |
|
| 348 | + |
|
| 349 | + if (is_string($entityMap)) { |
|
| 350 | + $entityMap = new $entityMap; |
|
| 351 | + } |
|
| 352 | + |
|
| 353 | + if (!$entityMap instanceof EntityMap) { |
|
| 354 | + throw new MappingException(get_class($entityMap) . ' must be an instance of EntityMap.'); |
|
| 355 | + } |
|
| 356 | + |
|
| 357 | + $entityMap->setClass($entity); |
|
| 358 | + |
|
| 359 | + $this->entityClasses[$entity] = $entityMap; |
|
| 360 | + } |
|
| 361 | + |
|
| 362 | + /** |
|
| 363 | + * Get the entity map instance for a custom entity |
|
| 364 | + * |
|
| 365 | + * @param string $entity |
|
| 366 | + * @return \Analogue\ORM\EntityMap |
|
| 367 | + */ |
|
| 368 | + protected function getEntityMapInstanceFor($entity) |
|
| 369 | + { |
|
| 370 | + if (class_exists($entity . 'Map')) { |
|
| 371 | + $map = $entity . 'Map'; |
|
| 372 | + $map = new $map; |
|
| 373 | + return $map; |
|
| 374 | + } |
|
| 375 | + |
|
| 376 | + if ($map = $this->getMapFromNamespaces($entity)) { |
|
| 377 | + return $map; |
|
| 378 | + } |
|
| 379 | 379 | |
| 380 | - if ($this->strictMode) { |
|
| 381 | - throw new EntityMapNotFoundException("No Map registered for $entity"); |
|
| 382 | - } |
|
| 380 | + if ($this->strictMode) { |
|
| 381 | + throw new EntityMapNotFoundException("No Map registered for $entity"); |
|
| 382 | + } |
|
| 383 | 383 | |
| 384 | - $map = $this->getNewEntityMap(); |
|
| 385 | - |
|
| 386 | - return $map; |
|
| 387 | - } |
|
| 388 | - |
|
| 389 | - /** |
|
| 390 | - * Scan through registered custom namespace |
|
| 391 | - * for an Entity/ValueMap |
|
| 392 | - * |
|
| 393 | - * @param string $class |
|
| 394 | - * @return ValueMap|EntityMap|boolean |
|
| 395 | - */ |
|
| 396 | - protected function getMapFromNamespaces($class) |
|
| 397 | - { |
|
| 398 | - foreach ($this->customMapNamespaces as $namespace) { |
|
| 399 | - if($map = $this->findMapInNamespace($class, $namespace)) { |
|
| 400 | - return $map; |
|
| 401 | - } |
|
| 402 | - } |
|
| 403 | - |
|
| 404 | - return false; |
|
| 405 | - } |
|
| 406 | - |
|
| 407 | - /** |
|
| 408 | - * Look in a custom namespace for an Entity/ValueMap |
|
| 409 | - * |
|
| 410 | - * @param string $class |
|
| 411 | - * @param string $namespace |
|
| 412 | - * @return ValueMap|EntityMap|boolean |
|
| 413 | - */ |
|
| 414 | - protected function findMapInNamespace($class, $namespace) |
|
| 415 | - { |
|
| 416 | - $parts = explode("\\", $class); |
|
| 384 | + $map = $this->getNewEntityMap(); |
|
| 385 | + |
|
| 386 | + return $map; |
|
| 387 | + } |
|
| 388 | + |
|
| 389 | + /** |
|
| 390 | + * Scan through registered custom namespace |
|
| 391 | + * for an Entity/ValueMap |
|
| 392 | + * |
|
| 393 | + * @param string $class |
|
| 394 | + * @return ValueMap|EntityMap|boolean |
|
| 395 | + */ |
|
| 396 | + protected function getMapFromNamespaces($class) |
|
| 397 | + { |
|
| 398 | + foreach ($this->customMapNamespaces as $namespace) { |
|
| 399 | + if($map = $this->findMapInNamespace($class, $namespace)) { |
|
| 400 | + return $map; |
|
| 401 | + } |
|
| 402 | + } |
|
| 403 | + |
|
| 404 | + return false; |
|
| 405 | + } |
|
| 406 | + |
|
| 407 | + /** |
|
| 408 | + * Look in a custom namespace for an Entity/ValueMap |
|
| 409 | + * |
|
| 410 | + * @param string $class |
|
| 411 | + * @param string $namespace |
|
| 412 | + * @return ValueMap|EntityMap|boolean |
|
| 413 | + */ |
|
| 414 | + protected function findMapInNamespace($class, $namespace) |
|
| 415 | + { |
|
| 416 | + $parts = explode("\\", $class); |
|
| 417 | 417 | |
| 418 | - $baseClass = $parts[count($parts) - 1]; |
|
| 419 | - |
|
| 420 | - $expectedClass = $namespace.$baseClass.'Map'; |
|
| 421 | - |
|
| 422 | - if (class_exists($expectedClass)) { |
|
| 423 | - return new $expectedClass; |
|
| 424 | - } |
|
| 425 | - |
|
| 426 | - return false; |
|
| 427 | - } |
|
| 428 | - |
|
| 429 | - /** |
|
| 430 | - * Dynamically create an entity map for a custom entity class |
|
| 431 | - * |
|
| 432 | - * @return EntityMap |
|
| 433 | - */ |
|
| 434 | - protected function getNewEntityMap() |
|
| 435 | - { |
|
| 436 | - return new EntityMap; |
|
| 437 | - } |
|
| 438 | - |
|
| 439 | - /** |
|
| 440 | - * Return the Singleton instance of the manager |
|
| 441 | - * |
|
| 442 | - * @return Manager |
|
| 443 | - */ |
|
| 444 | - public static function getInstance() |
|
| 445 | - { |
|
| 446 | - return static::$instance; |
|
| 447 | - } |
|
| 448 | - |
|
| 449 | - /** |
|
| 450 | - * Return the Driver Manager's instance |
|
| 451 | - * |
|
| 452 | - * @return \Analogue\ORM\Drivers\Manager |
|
| 453 | - */ |
|
| 454 | - public function getDriverManager() |
|
| 455 | - { |
|
| 456 | - return $this->drivers; |
|
| 457 | - } |
|
| 458 | - |
|
| 459 | - /** |
|
| 460 | - * Get the Repository instance for the given Entity |
|
| 461 | - * |
|
| 462 | - * @param \Analogue\ORM\Mappable|string $entity |
|
| 463 | - * @throws \InvalidArgumentException |
|
| 464 | - * @throws MappingException |
|
| 465 | - * @return \Analogue\ORM\Repository |
|
| 466 | - */ |
|
| 467 | - public function repository($entity) |
|
| 468 | - { |
|
| 469 | - if (!is_string($entity)) { |
|
| 470 | - $entity = get_class($entity); |
|
| 471 | - } |
|
| 472 | - |
|
| 473 | - // First we check if the repository is not already created. |
|
| 474 | - if (array_key_exists($entity, $this->repositories)) { |
|
| 475 | - return $this->repositories[$entity]; |
|
| 476 | - } |
|
| 477 | - |
|
| 478 | - $this->repositories[$entity] = new Repository($this->mapper($entity)); |
|
| 479 | - |
|
| 480 | - return $this->repositories[$entity]; |
|
| 481 | - } |
|
| 482 | - |
|
| 483 | - /** |
|
| 484 | - * Return true is the object is registered as value object |
|
| 485 | - * |
|
| 486 | - * @param mixed $object |
|
| 487 | - * @return boolean |
|
| 488 | - */ |
|
| 489 | - public function isValueObject($object) |
|
| 490 | - { |
|
| 491 | - if (!is_string($object)) { |
|
| 492 | - $object = get_class($object); |
|
| 493 | - } |
|
| 494 | - |
|
| 495 | - return array_key_exists($object, $this->valueClasses); |
|
| 496 | - } |
|
| 497 | - |
|
| 498 | - /** |
|
| 499 | - * Get the Value Map for a given Value Object Class |
|
| 500 | - * |
|
| 501 | - * @param string $valueObject |
|
| 502 | - * @throws MappingException |
|
| 503 | - * @return \Analogue\ORM\ValueMap |
|
| 504 | - */ |
|
| 505 | - public function getValueMap($valueObject) |
|
| 506 | - { |
|
| 507 | - if (!is_string($valueObject)) { |
|
| 508 | - $valueObject = get_class($valueObject); |
|
| 509 | - } |
|
| 510 | - |
|
| 511 | - if (!array_key_exists($valueObject, $this->valueClasses)) { |
|
| 512 | - $this->registerValueObject($valueObject); |
|
| 513 | - } |
|
| 514 | - |
|
| 515 | - /** @var ValueMap $valueMap */ |
|
| 516 | - $valueMap = new $this->valueClasses[$valueObject]; |
|
| 517 | - |
|
| 518 | - $valueMap->setClass($valueObject); |
|
| 519 | - |
|
| 520 | - return $valueMap; |
|
| 521 | - } |
|
| 522 | - |
|
| 523 | - /** |
|
| 524 | - * Register a Value Object |
|
| 525 | - * |
|
| 526 | - * @param string $valueObject |
|
| 527 | - * @param string $valueMap |
|
| 528 | - * @throws MappingException |
|
| 529 | - * @return void |
|
| 530 | - */ |
|
| 531 | - public function registerValueObject($valueObject, $valueMap = null) |
|
| 532 | - { |
|
| 533 | - if (!is_string($valueObject)) { |
|
| 534 | - $valueObject = get_class($valueObject); |
|
| 535 | - } |
|
| 536 | - |
|
| 537 | - if ($valueMap === null) { |
|
| 538 | - |
|
| 539 | - // First, we'll look into registered namespaces for Entity Maps, |
|
| 540 | - // if any. Then we'll fallback to the same namespace of the object |
|
| 541 | - if(! $valueMap = $this->getMapFromNamespaces($valueObject)) { |
|
| 542 | - $valueMap = $valueObject . 'Map'; |
|
| 543 | - } |
|
| 544 | - else { |
|
| 545 | - $valueMap = get_class($valueMap); |
|
| 546 | - } |
|
| 547 | - } |
|
| 548 | - |
|
| 549 | - if (! class_exists($valueMap)) { |
|
| 550 | - throw new MappingException("$valueMap doesn't exists"); |
|
| 551 | - } |
|
| 552 | - |
|
| 553 | - $this->valueClasses[$valueObject] = $valueMap; |
|
| 554 | - } |
|
| 555 | - |
|
| 556 | - /** |
|
| 557 | - * Instantiate a new Value Object instance |
|
| 558 | - * |
|
| 559 | - * @param string $valueObject |
|
| 560 | - * @return \Analogue\ORM\ValueObject |
|
| 561 | - */ |
|
| 562 | - public function getValueObjectInstance($valueObject) |
|
| 563 | - { |
|
| 564 | - $prototype = unserialize(sprintf('O:%d:"%s":0:{}', strlen($valueObject), $valueObject)); |
|
| 565 | - |
|
| 566 | - return $prototype; |
|
| 567 | - } |
|
| 568 | - |
|
| 569 | - /** |
|
| 570 | - * Register Analogue Plugin |
|
| 571 | - * |
|
| 572 | - * @param string $plugin class |
|
| 573 | - * @return void |
|
| 574 | - */ |
|
| 575 | - public function registerPlugin($plugin) |
|
| 576 | - { |
|
| 577 | - /** @var AnaloguePluginInterface $plugin */ |
|
| 578 | - $plugin = new $plugin($this); |
|
| 579 | - |
|
| 580 | - $this->events = array_merge($this->events, $plugin->getCustomEvents()); |
|
| 581 | - |
|
| 582 | - $plugin->register(); |
|
| 583 | - } |
|
| 584 | - |
|
| 585 | - /** |
|
| 586 | - * Register event listeners that will be fired regardless the type |
|
| 587 | - * of the entity. |
|
| 588 | - * |
|
| 589 | - * @param string $event |
|
| 590 | - * @param \Closure $callback |
|
| 591 | - * @throws \LogicException |
|
| 592 | - * @return void |
|
| 593 | - */ |
|
| 594 | - public function registerGlobalEvent($event, $callback) |
|
| 595 | - { |
|
| 596 | - if (!in_array($event, $this->events, false)) { |
|
| 597 | - throw new \LogicException("Analogue : Event $event doesn't exist"); |
|
| 598 | - } |
|
| 599 | - |
|
| 600 | - $this->eventDispatcher->listen("analogue.{$event}.*", $callback); |
|
| 601 | - } |
|
| 602 | - |
|
| 603 | - /** |
|
| 604 | - * Shortcut to Mapper store |
|
| 605 | - * |
|
| 606 | - * @param mixed $entity |
|
| 607 | - * @throws MappingException |
|
| 608 | - * @return mixed |
|
| 609 | - * @throws \InvalidArgumentException |
|
| 610 | - */ |
|
| 611 | - public function store($entity) |
|
| 612 | - { |
|
| 613 | - return $this->mapper($entity)->store($entity); |
|
| 614 | - } |
|
| 615 | - |
|
| 616 | - /** |
|
| 617 | - * Shortcut to Mapper delete |
|
| 618 | - * |
|
| 619 | - * @param mixed $entity |
|
| 620 | - * @throws MappingException |
|
| 621 | - * @return \Illuminate\Support\Collection|null |
|
| 622 | - * @throws \InvalidArgumentException |
|
| 623 | - */ |
|
| 624 | - public function delete($entity) |
|
| 625 | - { |
|
| 626 | - return $this->mapper($entity)->delete($entity); |
|
| 627 | - } |
|
| 628 | - |
|
| 629 | - /** |
|
| 630 | - * Shortcut to Mapper query |
|
| 631 | - * |
|
| 632 | - * @param mixed $entity |
|
| 633 | - * @throws MappingException |
|
| 634 | - * @return Query |
|
| 635 | - * @throws \InvalidArgumentException |
|
| 636 | - */ |
|
| 637 | - public function query($entity) |
|
| 638 | - { |
|
| 639 | - return $this->mapper($entity)->query(); |
|
| 640 | - } |
|
| 641 | - |
|
| 642 | - /** |
|
| 643 | - * Shortcut to Mapper Global Query |
|
| 644 | - * |
|
| 645 | - * @param mixed $entity |
|
| 646 | - * @throws MappingException |
|
| 647 | - * @return Query |
|
| 648 | - * @throws \InvalidArgumentException |
|
| 649 | - */ |
|
| 650 | - public function globalQuery($entity) |
|
| 651 | - { |
|
| 652 | - return $this->mapper($entity)->globalQuery(); |
|
| 653 | - } |
|
| 654 | - |
|
| 655 | - /** |
|
| 656 | - * @param array $morphMap |
|
| 657 | - * @return $this |
|
| 658 | - */ |
|
| 659 | - public function morphMap(array $morphMap) |
|
| 660 | - { |
|
| 661 | - $this->morphMap = $morphMap; |
|
| 662 | - |
|
| 663 | - return $this; |
|
| 664 | - } |
|
| 665 | - |
|
| 666 | - /** |
|
| 667 | - * @param string $class |
|
| 668 | - * @return mixed |
|
| 669 | - */ |
|
| 670 | - public function getMorphMap($class) |
|
| 671 | - { |
|
| 672 | - $key = array_search($class, $this->morphMap, false); |
|
| 673 | - |
|
| 674 | - return $key !== false ? $key : $class; |
|
| 675 | - } |
|
| 418 | + $baseClass = $parts[count($parts) - 1]; |
|
| 419 | + |
|
| 420 | + $expectedClass = $namespace.$baseClass.'Map'; |
|
| 421 | + |
|
| 422 | + if (class_exists($expectedClass)) { |
|
| 423 | + return new $expectedClass; |
|
| 424 | + } |
|
| 425 | + |
|
| 426 | + return false; |
|
| 427 | + } |
|
| 428 | + |
|
| 429 | + /** |
|
| 430 | + * Dynamically create an entity map for a custom entity class |
|
| 431 | + * |
|
| 432 | + * @return EntityMap |
|
| 433 | + */ |
|
| 434 | + protected function getNewEntityMap() |
|
| 435 | + { |
|
| 436 | + return new EntityMap; |
|
| 437 | + } |
|
| 438 | + |
|
| 439 | + /** |
|
| 440 | + * Return the Singleton instance of the manager |
|
| 441 | + * |
|
| 442 | + * @return Manager |
|
| 443 | + */ |
|
| 444 | + public static function getInstance() |
|
| 445 | + { |
|
| 446 | + return static::$instance; |
|
| 447 | + } |
|
| 448 | + |
|
| 449 | + /** |
|
| 450 | + * Return the Driver Manager's instance |
|
| 451 | + * |
|
| 452 | + * @return \Analogue\ORM\Drivers\Manager |
|
| 453 | + */ |
|
| 454 | + public function getDriverManager() |
|
| 455 | + { |
|
| 456 | + return $this->drivers; |
|
| 457 | + } |
|
| 458 | + |
|
| 459 | + /** |
|
| 460 | + * Get the Repository instance for the given Entity |
|
| 461 | + * |
|
| 462 | + * @param \Analogue\ORM\Mappable|string $entity |
|
| 463 | + * @throws \InvalidArgumentException |
|
| 464 | + * @throws MappingException |
|
| 465 | + * @return \Analogue\ORM\Repository |
|
| 466 | + */ |
|
| 467 | + public function repository($entity) |
|
| 468 | + { |
|
| 469 | + if (!is_string($entity)) { |
|
| 470 | + $entity = get_class($entity); |
|
| 471 | + } |
|
| 472 | + |
|
| 473 | + // First we check if the repository is not already created. |
|
| 474 | + if (array_key_exists($entity, $this->repositories)) { |
|
| 475 | + return $this->repositories[$entity]; |
|
| 476 | + } |
|
| 477 | + |
|
| 478 | + $this->repositories[$entity] = new Repository($this->mapper($entity)); |
|
| 479 | + |
|
| 480 | + return $this->repositories[$entity]; |
|
| 481 | + } |
|
| 482 | + |
|
| 483 | + /** |
|
| 484 | + * Return true is the object is registered as value object |
|
| 485 | + * |
|
| 486 | + * @param mixed $object |
|
| 487 | + * @return boolean |
|
| 488 | + */ |
|
| 489 | + public function isValueObject($object) |
|
| 490 | + { |
|
| 491 | + if (!is_string($object)) { |
|
| 492 | + $object = get_class($object); |
|
| 493 | + } |
|
| 494 | + |
|
| 495 | + return array_key_exists($object, $this->valueClasses); |
|
| 496 | + } |
|
| 497 | + |
|
| 498 | + /** |
|
| 499 | + * Get the Value Map for a given Value Object Class |
|
| 500 | + * |
|
| 501 | + * @param string $valueObject |
|
| 502 | + * @throws MappingException |
|
| 503 | + * @return \Analogue\ORM\ValueMap |
|
| 504 | + */ |
|
| 505 | + public function getValueMap($valueObject) |
|
| 506 | + { |
|
| 507 | + if (!is_string($valueObject)) { |
|
| 508 | + $valueObject = get_class($valueObject); |
|
| 509 | + } |
|
| 510 | + |
|
| 511 | + if (!array_key_exists($valueObject, $this->valueClasses)) { |
|
| 512 | + $this->registerValueObject($valueObject); |
|
| 513 | + } |
|
| 514 | + |
|
| 515 | + /** @var ValueMap $valueMap */ |
|
| 516 | + $valueMap = new $this->valueClasses[$valueObject]; |
|
| 517 | + |
|
| 518 | + $valueMap->setClass($valueObject); |
|
| 519 | + |
|
| 520 | + return $valueMap; |
|
| 521 | + } |
|
| 522 | + |
|
| 523 | + /** |
|
| 524 | + * Register a Value Object |
|
| 525 | + * |
|
| 526 | + * @param string $valueObject |
|
| 527 | + * @param string $valueMap |
|
| 528 | + * @throws MappingException |
|
| 529 | + * @return void |
|
| 530 | + */ |
|
| 531 | + public function registerValueObject($valueObject, $valueMap = null) |
|
| 532 | + { |
|
| 533 | + if (!is_string($valueObject)) { |
|
| 534 | + $valueObject = get_class($valueObject); |
|
| 535 | + } |
|
| 536 | + |
|
| 537 | + if ($valueMap === null) { |
|
| 538 | + |
|
| 539 | + // First, we'll look into registered namespaces for Entity Maps, |
|
| 540 | + // if any. Then we'll fallback to the same namespace of the object |
|
| 541 | + if(! $valueMap = $this->getMapFromNamespaces($valueObject)) { |
|
| 542 | + $valueMap = $valueObject . 'Map'; |
|
| 543 | + } |
|
| 544 | + else { |
|
| 545 | + $valueMap = get_class($valueMap); |
|
| 546 | + } |
|
| 547 | + } |
|
| 548 | + |
|
| 549 | + if (! class_exists($valueMap)) { |
|
| 550 | + throw new MappingException("$valueMap doesn't exists"); |
|
| 551 | + } |
|
| 552 | + |
|
| 553 | + $this->valueClasses[$valueObject] = $valueMap; |
|
| 554 | + } |
|
| 555 | + |
|
| 556 | + /** |
|
| 557 | + * Instantiate a new Value Object instance |
|
| 558 | + * |
|
| 559 | + * @param string $valueObject |
|
| 560 | + * @return \Analogue\ORM\ValueObject |
|
| 561 | + */ |
|
| 562 | + public function getValueObjectInstance($valueObject) |
|
| 563 | + { |
|
| 564 | + $prototype = unserialize(sprintf('O:%d:"%s":0:{}', strlen($valueObject), $valueObject)); |
|
| 565 | + |
|
| 566 | + return $prototype; |
|
| 567 | + } |
|
| 568 | + |
|
| 569 | + /** |
|
| 570 | + * Register Analogue Plugin |
|
| 571 | + * |
|
| 572 | + * @param string $plugin class |
|
| 573 | + * @return void |
|
| 574 | + */ |
|
| 575 | + public function registerPlugin($plugin) |
|
| 576 | + { |
|
| 577 | + /** @var AnaloguePluginInterface $plugin */ |
|
| 578 | + $plugin = new $plugin($this); |
|
| 579 | + |
|
| 580 | + $this->events = array_merge($this->events, $plugin->getCustomEvents()); |
|
| 581 | + |
|
| 582 | + $plugin->register(); |
|
| 583 | + } |
|
| 584 | + |
|
| 585 | + /** |
|
| 586 | + * Register event listeners that will be fired regardless the type |
|
| 587 | + * of the entity. |
|
| 588 | + * |
|
| 589 | + * @param string $event |
|
| 590 | + * @param \Closure $callback |
|
| 591 | + * @throws \LogicException |
|
| 592 | + * @return void |
|
| 593 | + */ |
|
| 594 | + public function registerGlobalEvent($event, $callback) |
|
| 595 | + { |
|
| 596 | + if (!in_array($event, $this->events, false)) { |
|
| 597 | + throw new \LogicException("Analogue : Event $event doesn't exist"); |
|
| 598 | + } |
|
| 599 | + |
|
| 600 | + $this->eventDispatcher->listen("analogue.{$event}.*", $callback); |
|
| 601 | + } |
|
| 602 | + |
|
| 603 | + /** |
|
| 604 | + * Shortcut to Mapper store |
|
| 605 | + * |
|
| 606 | + * @param mixed $entity |
|
| 607 | + * @throws MappingException |
|
| 608 | + * @return mixed |
|
| 609 | + * @throws \InvalidArgumentException |
|
| 610 | + */ |
|
| 611 | + public function store($entity) |
|
| 612 | + { |
|
| 613 | + return $this->mapper($entity)->store($entity); |
|
| 614 | + } |
|
| 615 | + |
|
| 616 | + /** |
|
| 617 | + * Shortcut to Mapper delete |
|
| 618 | + * |
|
| 619 | + * @param mixed $entity |
|
| 620 | + * @throws MappingException |
|
| 621 | + * @return \Illuminate\Support\Collection|null |
|
| 622 | + * @throws \InvalidArgumentException |
|
| 623 | + */ |
|
| 624 | + public function delete($entity) |
|
| 625 | + { |
|
| 626 | + return $this->mapper($entity)->delete($entity); |
|
| 627 | + } |
|
| 628 | + |
|
| 629 | + /** |
|
| 630 | + * Shortcut to Mapper query |
|
| 631 | + * |
|
| 632 | + * @param mixed $entity |
|
| 633 | + * @throws MappingException |
|
| 634 | + * @return Query |
|
| 635 | + * @throws \InvalidArgumentException |
|
| 636 | + */ |
|
| 637 | + public function query($entity) |
|
| 638 | + { |
|
| 639 | + return $this->mapper($entity)->query(); |
|
| 640 | + } |
|
| 641 | + |
|
| 642 | + /** |
|
| 643 | + * Shortcut to Mapper Global Query |
|
| 644 | + * |
|
| 645 | + * @param mixed $entity |
|
| 646 | + * @throws MappingException |
|
| 647 | + * @return Query |
|
| 648 | + * @throws \InvalidArgumentException |
|
| 649 | + */ |
|
| 650 | + public function globalQuery($entity) |
|
| 651 | + { |
|
| 652 | + return $this->mapper($entity)->globalQuery(); |
|
| 653 | + } |
|
| 654 | + |
|
| 655 | + /** |
|
| 656 | + * @param array $morphMap |
|
| 657 | + * @return $this |
|
| 658 | + */ |
|
| 659 | + public function morphMap(array $morphMap) |
|
| 660 | + { |
|
| 661 | + $this->morphMap = $morphMap; |
|
| 662 | + |
|
| 663 | + return $this; |
|
| 664 | + } |
|
| 665 | + |
|
| 666 | + /** |
|
| 667 | + * @param string $class |
|
| 668 | + * @return mixed |
|
| 669 | + */ |
|
| 670 | + public function getMorphMap($class) |
|
| 671 | + { |
|
| 672 | + $key = array_search($class, $this->morphMap, false); |
|
| 673 | + |
|
| 674 | + return $key !== false ? $key : $class; |
|
| 675 | + } |
|
| 676 | 676 | } |
@@ -42,7 +42,7 @@ discard block |
||
| 42 | 42 | $key = $this->getEntityKey($key); |
| 43 | 43 | } |
| 44 | 44 | |
| 45 | - return array_first($this->items, function ($entity, $itemKey) use ($key) { |
|
| 45 | + return array_first($this->items, function($entity, $itemKey) use ($key) { |
|
| 46 | 46 | |
| 47 | 47 | return $this->getEntityKey($entity) == $key; |
| 48 | 48 | }, $default); |
@@ -153,14 +153,14 @@ discard block |
||
| 153 | 153 | */ |
| 154 | 154 | public function getEntityHashes() |
| 155 | 155 | { |
| 156 | - return array_map(function ($entity) { |
|
| 156 | + return array_map(function($entity) { |
|
| 157 | 157 | $class = get_class($entity); |
| 158 | 158 | |
| 159 | 159 | $mapper = Manager::getMapper($class); |
| 160 | 160 | |
| 161 | 161 | $keyName = $mapper->getEntityMap()->getKeyName(); |
| 162 | 162 | |
| 163 | - return $class . '.' . $entity->getEntityAttribute($keyName); |
|
| 163 | + return $class.'.'.$entity->getEntityAttribute($keyName); |
|
| 164 | 164 | }, |
| 165 | 165 | $this->items); |
| 166 | 166 | } |
@@ -183,7 +183,7 @@ discard block |
||
| 183 | 183 | |
| 184 | 184 | $keyName = $mapper->getEntityMap()->getKeyName(); |
| 185 | 185 | |
| 186 | - if (in_array($class . '.' . $item->$keyName, $hashes)) { |
|
| 186 | + if (in_array($class.'.'.$item->$keyName, $hashes)) { |
|
| 187 | 187 | $subset[] = $item; |
| 188 | 188 | } |
| 189 | 189 | } |
@@ -330,7 +330,7 @@ discard block |
||
| 330 | 330 | */ |
| 331 | 331 | public function max($key = null) |
| 332 | 332 | { |
| 333 | - return $this->reduce(function ($result, $item) use ($key) { |
|
| 333 | + return $this->reduce(function($result, $item) use ($key) { |
|
| 334 | 334 | $wrapper = $this->factory->make($item); |
| 335 | 335 | |
| 336 | 336 | return (is_null($result) || $wrapper->getEntityAttribute($key) > $result) ? |
@@ -347,7 +347,7 @@ discard block |
||
| 347 | 347 | */ |
| 348 | 348 | public function min($key = null) |
| 349 | 349 | { |
| 350 | - return $this->reduce(function ($result, $item) use ($key) { |
|
| 350 | + return $this->reduce(function($result, $item) use ($key) { |
|
| 351 | 351 | $wrapper = $this->factory->make($item); |
| 352 | 352 | |
| 353 | 353 | return (is_null($result) || $wrapper->getEntityAttribute($key) < $result) |
@@ -78,7 +78,7 @@ |
||
| 78 | 78 | /** |
| 79 | 79 | * Push an item onto the end of the collection. |
| 80 | 80 | * |
| 81 | - * @param mixed $value |
|
| 81 | + * @param Mappable $value |
|
| 82 | 82 | * @return void |
| 83 | 83 | */ |
| 84 | 84 | public function push($value) |
@@ -10,414 +10,414 @@ |
||
| 10 | 10 | |
| 11 | 11 | class EntityCollection extends Collection |
| 12 | 12 | { |
| 13 | - /** |
|
| 14 | - * Wrapper Factory |
|
| 15 | - * |
|
| 16 | - * @var \Analogue\ORM\System\Wrappers\Factory |
|
| 17 | - */ |
|
| 18 | - protected $factory; |
|
| 19 | - |
|
| 20 | - /** |
|
| 21 | - * EntityCollection constructor. |
|
| 22 | - * @param array|null $entities |
|
| 23 | - */ |
|
| 24 | - public function __construct(array $entities = null) |
|
| 25 | - { |
|
| 26 | - $this->factory = new Factory; |
|
| 27 | - |
|
| 28 | - parent::__construct($entities); |
|
| 29 | - } |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * Find an entity in the collection by key. |
|
| 33 | - * |
|
| 34 | - * @param mixed $key |
|
| 35 | - * @param mixed $default |
|
| 36 | - * @throws MappingException |
|
| 37 | - * @return \Analogue\ORM\Entity |
|
| 38 | - */ |
|
| 39 | - public function find($key, $default = null) |
|
| 40 | - { |
|
| 41 | - if ($key instanceof Mappable) { |
|
| 42 | - $key = $this->getEntityKey($key); |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - return array_first($this->items, function ($entity, $itemKey) use ($key) { |
|
| 46 | - |
|
| 47 | - return $this->getEntityKey($entity) == $key; |
|
| 48 | - }, $default); |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * Add an entity to the collection. |
|
| 53 | - * |
|
| 54 | - * @param Mappable $entity |
|
| 55 | - * @return $this |
|
| 56 | - */ |
|
| 57 | - public function add($entity) |
|
| 58 | - { |
|
| 59 | - $this->push($entity); |
|
| 60 | - |
|
| 61 | - return $this; |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * Remove an entity from the collection |
|
| 66 | - * |
|
| 67 | - * @param $entity |
|
| 68 | - * @throws MappingException |
|
| 69 | - * @return mixed |
|
| 70 | - */ |
|
| 71 | - public function remove($entity) |
|
| 72 | - { |
|
| 73 | - $key = $this->getEntityKey($entity); |
|
| 74 | - |
|
| 75 | - return $this->pull($key); |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * Push an item onto the end of the collection. |
|
| 80 | - * |
|
| 81 | - * @param mixed $value |
|
| 82 | - * @return void |
|
| 83 | - */ |
|
| 84 | - public function push($value) |
|
| 85 | - { |
|
| 86 | - $this->offsetSet(null, $value); |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * Put an item in the collection by key. |
|
| 91 | - * |
|
| 92 | - * @param mixed $key |
|
| 93 | - * @param mixed $value |
|
| 94 | - * @return void |
|
| 95 | - */ |
|
| 96 | - public function put($key, $value) |
|
| 97 | - { |
|
| 98 | - $this->offsetSet($key, $value); |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * Set the item at a given offset. |
|
| 103 | - * |
|
| 104 | - * @param mixed $key |
|
| 105 | - * @param mixed $value |
|
| 106 | - * @return void |
|
| 107 | - */ |
|
| 108 | - public function offsetSet($key, $value) |
|
| 109 | - { |
|
| 110 | - if (is_null($key)) { |
|
| 111 | - $this->items[] = $value; |
|
| 112 | - } else { |
|
| 113 | - $this->items[$key] = $value; |
|
| 114 | - } |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * Determine if a key exists in the collection. |
|
| 119 | - * |
|
| 120 | - * @param mixed $key |
|
| 121 | - * @param mixed|null $value |
|
| 122 | - * @return bool |
|
| 123 | - */ |
|
| 124 | - public function contains($key, $value = null) |
|
| 125 | - { |
|
| 126 | - if (func_num_args() == 2) { |
|
| 127 | - return !$this->where($key, $value)->isEmpty(); |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - if ($this->useAsCallable($key)) { |
|
| 131 | - return !is_null($this->first($key)); |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - return !is_null($this->find($key)); |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - /** |
|
| 138 | - * Fetch a nested element of the collection. |
|
| 139 | - * |
|
| 140 | - * @param string $key |
|
| 141 | - * @return self |
|
| 142 | - */ |
|
| 143 | - public function fetch($key) |
|
| 144 | - { |
|
| 145 | - return new static(array_fetch($this->toArray(), $key)); |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - /** |
|
| 149 | - * Generic function for returning class.key value pairs |
|
| 150 | - * |
|
| 151 | - * @throws MappingException |
|
| 152 | - * @return string |
|
| 153 | - */ |
|
| 154 | - public function getEntityHashes() |
|
| 155 | - { |
|
| 156 | - return array_map(function ($entity) { |
|
| 157 | - $class = get_class($entity); |
|
| 158 | - |
|
| 159 | - $mapper = Manager::getMapper($class); |
|
| 160 | - |
|
| 161 | - $keyName = $mapper->getEntityMap()->getKeyName(); |
|
| 162 | - |
|
| 163 | - return $class . '.' . $entity->getEntityAttribute($keyName); |
|
| 164 | - }, |
|
| 165 | - $this->items); |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - /** |
|
| 169 | - * Get a subset of the collection from entity hashes |
|
| 170 | - * |
|
| 171 | - * @param array $hashes |
|
| 172 | - * @throws MappingException |
|
| 173 | - * @return array |
|
| 174 | - */ |
|
| 175 | - public function getSubsetByHashes(array $hashes) |
|
| 176 | - { |
|
| 177 | - $subset = []; |
|
| 178 | - |
|
| 179 | - foreach ($this->items as $item) { |
|
| 180 | - $class = get_class($item); |
|
| 181 | - |
|
| 182 | - $mapper = Manager::getMapper($class); |
|
| 183 | - |
|
| 184 | - $keyName = $mapper->getEntityMap()->getKeyName(); |
|
| 185 | - |
|
| 186 | - if (in_array($class . '.' . $item->$keyName, $hashes)) { |
|
| 187 | - $subset[] = $item; |
|
| 188 | - } |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - return $subset; |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - /** |
|
| 195 | - * Merge the collection with the given items. |
|
| 196 | - * |
|
| 197 | - * @param array $items |
|
| 198 | - * @throws MappingException |
|
| 199 | - * @return self |
|
| 200 | - */ |
|
| 201 | - public function merge($items) |
|
| 202 | - { |
|
| 203 | - $dictionary = $this->getDictionary(); |
|
| 204 | - |
|
| 205 | - foreach ($items as $item) { |
|
| 206 | - $dictionary[$this->getEntityKey($item)] = $item; |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - return new static(array_values($dictionary)); |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - /** |
|
| 213 | - * Diff the collection with the given items. |
|
| 214 | - * |
|
| 215 | - * @param \ArrayAccess|array $items |
|
| 216 | - * @return self |
|
| 217 | - */ |
|
| 218 | - public function diff($items) |
|
| 219 | - { |
|
| 220 | - $diff = new static; |
|
| 221 | - |
|
| 222 | - $dictionary = $this->getDictionary($items); |
|
| 223 | - |
|
| 224 | - foreach ($this->items as $item) { |
|
| 225 | - if (!isset($dictionary[$this->getEntityKey($item)])) { |
|
| 226 | - $diff->add($item); |
|
| 227 | - } |
|
| 228 | - } |
|
| 229 | - |
|
| 230 | - return $diff; |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - /** |
|
| 234 | - * Intersect the collection with the given items. |
|
| 235 | - * |
|
| 236 | - * @param \ArrayAccess|array $items |
|
| 237 | - * @throws MappingException |
|
| 238 | - * @return self |
|
| 239 | - */ |
|
| 240 | - public function intersect($items) |
|
| 241 | - { |
|
| 242 | - $intersect = new static; |
|
| 243 | - |
|
| 244 | - $dictionary = $this->getDictionary($items); |
|
| 245 | - |
|
| 246 | - foreach ($this->items as $item) { |
|
| 247 | - if (isset($dictionary[$this->getEntityKey($item)])) { |
|
| 248 | - $intersect->add($item); |
|
| 249 | - } |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - return $intersect; |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - /** |
|
| 256 | - * Returns only the models from the collection with the specified keys. |
|
| 257 | - * |
|
| 258 | - * @param mixed $keys |
|
| 259 | - * @return self |
|
| 260 | - */ |
|
| 261 | - public function only($keys) |
|
| 262 | - { |
|
| 263 | - $dictionary = array_only($this->getDictionary(), $keys); |
|
| 264 | - |
|
| 265 | - return new static(array_values($dictionary)); |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - /** |
|
| 269 | - * Returns all models in the collection except the models with specified keys. |
|
| 270 | - * |
|
| 271 | - * @param mixed $keys |
|
| 272 | - * @return self |
|
| 273 | - */ |
|
| 274 | - public function except($keys) |
|
| 275 | - { |
|
| 276 | - $dictionary = array_except($this->getDictionary(), $keys); |
|
| 277 | - |
|
| 278 | - return new static(array_values($dictionary)); |
|
| 279 | - } |
|
| 280 | - |
|
| 281 | - /** |
|
| 282 | - * Get a dictionary keyed by primary keys. |
|
| 283 | - * |
|
| 284 | - * @param \ArrayAccess|array $items |
|
| 285 | - * @throws MappingException |
|
| 286 | - * @return array |
|
| 287 | - */ |
|
| 288 | - public function getDictionary($items = null) |
|
| 289 | - { |
|
| 290 | - $items = is_null($items) ? $this->items : $items; |
|
| 291 | - |
|
| 292 | - $dictionary = []; |
|
| 293 | - |
|
| 294 | - foreach ($items as $value) { |
|
| 295 | - $dictionary[$this->getEntityKey($value)] = $value; |
|
| 296 | - } |
|
| 297 | - |
|
| 298 | - return $dictionary; |
|
| 299 | - } |
|
| 300 | - |
|
| 301 | - /** |
|
| 302 | - * @throws MappingException |
|
| 303 | - * @return array |
|
| 304 | - */ |
|
| 305 | - public function getEntityKeys() |
|
| 306 | - { |
|
| 307 | - return array_keys($this->getDictionary()); |
|
| 308 | - } |
|
| 309 | - |
|
| 310 | - /** |
|
| 311 | - * @param $entity |
|
| 312 | - * @throws MappingException |
|
| 313 | - * @return mixed |
|
| 314 | - */ |
|
| 315 | - protected function getEntityKey($entity) |
|
| 316 | - { |
|
| 317 | - $keyName = Manager::getMapper($entity)->getEntityMap()->getKeyName(); |
|
| 318 | - |
|
| 319 | - $wrapper = $this->factory->make($entity); |
|
| 320 | - |
|
| 321 | - return $wrapper->getEntityAttribute($keyName); |
|
| 322 | - } |
|
| 323 | - |
|
| 324 | - /** |
|
| 325 | - * Get the max value of a given key. |
|
| 326 | - * |
|
| 327 | - * @param string|null $key |
|
| 328 | - * @throws MappingException |
|
| 329 | - * @return mixed |
|
| 330 | - */ |
|
| 331 | - public function max($key = null) |
|
| 332 | - { |
|
| 333 | - return $this->reduce(function ($result, $item) use ($key) { |
|
| 334 | - $wrapper = $this->factory->make($item); |
|
| 335 | - |
|
| 336 | - return (is_null($result) || $wrapper->getEntityAttribute($key) > $result) ? |
|
| 337 | - $wrapper->getEntityAttribute($key) : $result; |
|
| 338 | - }); |
|
| 339 | - } |
|
| 340 | - |
|
| 341 | - /** |
|
| 342 | - * Get the min value of a given key. |
|
| 343 | - * |
|
| 344 | - * @param string|null $key |
|
| 345 | - * @throws MappingException |
|
| 346 | - * @return mixed |
|
| 347 | - */ |
|
| 348 | - public function min($key = null) |
|
| 349 | - { |
|
| 350 | - return $this->reduce(function ($result, $item) use ($key) { |
|
| 351 | - $wrapper = $this->factory->make($item); |
|
| 352 | - |
|
| 353 | - return (is_null($result) || $wrapper->getEntityAttribute($key) < $result) |
|
| 354 | - ? $wrapper->getEntityAttribute($key) : $result; |
|
| 355 | - }); |
|
| 356 | - } |
|
| 357 | - |
|
| 358 | - /** |
|
| 359 | - * Get an array with the values of a given key. |
|
| 360 | - * |
|
| 361 | - * @param string $value |
|
| 362 | - * @param string|null $key |
|
| 363 | - * @return self |
|
| 364 | - */ |
|
| 365 | - public function pluck($value, $key = null) |
|
| 366 | - { |
|
| 367 | - return new Collection(Arr::pluck($this->items, $value, $key)); |
|
| 368 | - } |
|
| 369 | - |
|
| 370 | - /** |
|
| 371 | - * Alias for the "pluck" method. |
|
| 372 | - * |
|
| 373 | - * @param string $value |
|
| 374 | - * @param string|null $key |
|
| 375 | - * @return self |
|
| 376 | - */ |
|
| 377 | - public function lists($value, $key = null) |
|
| 378 | - { |
|
| 379 | - return $this->pluck($value, $key); |
|
| 380 | - } |
|
| 381 | - |
|
| 382 | - /** |
|
| 383 | - * Return only unique items from the collection. |
|
| 384 | - * |
|
| 385 | - * @param string|null $key |
|
| 386 | - * @param bool $strict |
|
| 387 | - * @throws MappingException |
|
| 388 | - * @return self |
|
| 389 | - */ |
|
| 390 | - public function unique($key = null, $strict = false) |
|
| 391 | - { |
|
| 392 | - $dictionary = $this->getDictionary(); |
|
| 393 | - |
|
| 394 | - return new static(array_values($dictionary)); |
|
| 395 | - } |
|
| 396 | - |
|
| 397 | - /** |
|
| 398 | - * Get a base Support collection instance from this collection. |
|
| 399 | - * |
|
| 400 | - * @return \Illuminate\Support\Collection |
|
| 401 | - */ |
|
| 402 | - public function toBase() |
|
| 403 | - { |
|
| 404 | - return new Collection($this->items); |
|
| 405 | - } |
|
| 406 | - |
|
| 407 | - public function toArray() |
|
| 408 | - { |
|
| 409 | - return array_values(parent::toArray()); |
|
| 410 | - } |
|
| 411 | - |
|
| 412 | - /** |
|
| 413 | - * Get the collection of items as JSON. |
|
| 414 | - * |
|
| 415 | - * @param int $options |
|
| 416 | - * @return string |
|
| 417 | - */ |
|
| 418 | - public function toJson($options = 0) |
|
| 419 | - { |
|
| 420 | - $collection = new Collection(array_values($this->items)); |
|
| 421 | - return $collection->toJson($options); |
|
| 422 | - } |
|
| 13 | + /** |
|
| 14 | + * Wrapper Factory |
|
| 15 | + * |
|
| 16 | + * @var \Analogue\ORM\System\Wrappers\Factory |
|
| 17 | + */ |
|
| 18 | + protected $factory; |
|
| 19 | + |
|
| 20 | + /** |
|
| 21 | + * EntityCollection constructor. |
|
| 22 | + * @param array|null $entities |
|
| 23 | + */ |
|
| 24 | + public function __construct(array $entities = null) |
|
| 25 | + { |
|
| 26 | + $this->factory = new Factory; |
|
| 27 | + |
|
| 28 | + parent::__construct($entities); |
|
| 29 | + } |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * Find an entity in the collection by key. |
|
| 33 | + * |
|
| 34 | + * @param mixed $key |
|
| 35 | + * @param mixed $default |
|
| 36 | + * @throws MappingException |
|
| 37 | + * @return \Analogue\ORM\Entity |
|
| 38 | + */ |
|
| 39 | + public function find($key, $default = null) |
|
| 40 | + { |
|
| 41 | + if ($key instanceof Mappable) { |
|
| 42 | + $key = $this->getEntityKey($key); |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + return array_first($this->items, function ($entity, $itemKey) use ($key) { |
|
| 46 | + |
|
| 47 | + return $this->getEntityKey($entity) == $key; |
|
| 48 | + }, $default); |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * Add an entity to the collection. |
|
| 53 | + * |
|
| 54 | + * @param Mappable $entity |
|
| 55 | + * @return $this |
|
| 56 | + */ |
|
| 57 | + public function add($entity) |
|
| 58 | + { |
|
| 59 | + $this->push($entity); |
|
| 60 | + |
|
| 61 | + return $this; |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * Remove an entity from the collection |
|
| 66 | + * |
|
| 67 | + * @param $entity |
|
| 68 | + * @throws MappingException |
|
| 69 | + * @return mixed |
|
| 70 | + */ |
|
| 71 | + public function remove($entity) |
|
| 72 | + { |
|
| 73 | + $key = $this->getEntityKey($entity); |
|
| 74 | + |
|
| 75 | + return $this->pull($key); |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * Push an item onto the end of the collection. |
|
| 80 | + * |
|
| 81 | + * @param mixed $value |
|
| 82 | + * @return void |
|
| 83 | + */ |
|
| 84 | + public function push($value) |
|
| 85 | + { |
|
| 86 | + $this->offsetSet(null, $value); |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * Put an item in the collection by key. |
|
| 91 | + * |
|
| 92 | + * @param mixed $key |
|
| 93 | + * @param mixed $value |
|
| 94 | + * @return void |
|
| 95 | + */ |
|
| 96 | + public function put($key, $value) |
|
| 97 | + { |
|
| 98 | + $this->offsetSet($key, $value); |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * Set the item at a given offset. |
|
| 103 | + * |
|
| 104 | + * @param mixed $key |
|
| 105 | + * @param mixed $value |
|
| 106 | + * @return void |
|
| 107 | + */ |
|
| 108 | + public function offsetSet($key, $value) |
|
| 109 | + { |
|
| 110 | + if (is_null($key)) { |
|
| 111 | + $this->items[] = $value; |
|
| 112 | + } else { |
|
| 113 | + $this->items[$key] = $value; |
|
| 114 | + } |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * Determine if a key exists in the collection. |
|
| 119 | + * |
|
| 120 | + * @param mixed $key |
|
| 121 | + * @param mixed|null $value |
|
| 122 | + * @return bool |
|
| 123 | + */ |
|
| 124 | + public function contains($key, $value = null) |
|
| 125 | + { |
|
| 126 | + if (func_num_args() == 2) { |
|
| 127 | + return !$this->where($key, $value)->isEmpty(); |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + if ($this->useAsCallable($key)) { |
|
| 131 | + return !is_null($this->first($key)); |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + return !is_null($this->find($key)); |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + /** |
|
| 138 | + * Fetch a nested element of the collection. |
|
| 139 | + * |
|
| 140 | + * @param string $key |
|
| 141 | + * @return self |
|
| 142 | + */ |
|
| 143 | + public function fetch($key) |
|
| 144 | + { |
|
| 145 | + return new static(array_fetch($this->toArray(), $key)); |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + /** |
|
| 149 | + * Generic function for returning class.key value pairs |
|
| 150 | + * |
|
| 151 | + * @throws MappingException |
|
| 152 | + * @return string |
|
| 153 | + */ |
|
| 154 | + public function getEntityHashes() |
|
| 155 | + { |
|
| 156 | + return array_map(function ($entity) { |
|
| 157 | + $class = get_class($entity); |
|
| 158 | + |
|
| 159 | + $mapper = Manager::getMapper($class); |
|
| 160 | + |
|
| 161 | + $keyName = $mapper->getEntityMap()->getKeyName(); |
|
| 162 | + |
|
| 163 | + return $class . '.' . $entity->getEntityAttribute($keyName); |
|
| 164 | + }, |
|
| 165 | + $this->items); |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + /** |
|
| 169 | + * Get a subset of the collection from entity hashes |
|
| 170 | + * |
|
| 171 | + * @param array $hashes |
|
| 172 | + * @throws MappingException |
|
| 173 | + * @return array |
|
| 174 | + */ |
|
| 175 | + public function getSubsetByHashes(array $hashes) |
|
| 176 | + { |
|
| 177 | + $subset = []; |
|
| 178 | + |
|
| 179 | + foreach ($this->items as $item) { |
|
| 180 | + $class = get_class($item); |
|
| 181 | + |
|
| 182 | + $mapper = Manager::getMapper($class); |
|
| 183 | + |
|
| 184 | + $keyName = $mapper->getEntityMap()->getKeyName(); |
|
| 185 | + |
|
| 186 | + if (in_array($class . '.' . $item->$keyName, $hashes)) { |
|
| 187 | + $subset[] = $item; |
|
| 188 | + } |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + return $subset; |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + /** |
|
| 195 | + * Merge the collection with the given items. |
|
| 196 | + * |
|
| 197 | + * @param array $items |
|
| 198 | + * @throws MappingException |
|
| 199 | + * @return self |
|
| 200 | + */ |
|
| 201 | + public function merge($items) |
|
| 202 | + { |
|
| 203 | + $dictionary = $this->getDictionary(); |
|
| 204 | + |
|
| 205 | + foreach ($items as $item) { |
|
| 206 | + $dictionary[$this->getEntityKey($item)] = $item; |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + return new static(array_values($dictionary)); |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + /** |
|
| 213 | + * Diff the collection with the given items. |
|
| 214 | + * |
|
| 215 | + * @param \ArrayAccess|array $items |
|
| 216 | + * @return self |
|
| 217 | + */ |
|
| 218 | + public function diff($items) |
|
| 219 | + { |
|
| 220 | + $diff = new static; |
|
| 221 | + |
|
| 222 | + $dictionary = $this->getDictionary($items); |
|
| 223 | + |
|
| 224 | + foreach ($this->items as $item) { |
|
| 225 | + if (!isset($dictionary[$this->getEntityKey($item)])) { |
|
| 226 | + $diff->add($item); |
|
| 227 | + } |
|
| 228 | + } |
|
| 229 | + |
|
| 230 | + return $diff; |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + /** |
|
| 234 | + * Intersect the collection with the given items. |
|
| 235 | + * |
|
| 236 | + * @param \ArrayAccess|array $items |
|
| 237 | + * @throws MappingException |
|
| 238 | + * @return self |
|
| 239 | + */ |
|
| 240 | + public function intersect($items) |
|
| 241 | + { |
|
| 242 | + $intersect = new static; |
|
| 243 | + |
|
| 244 | + $dictionary = $this->getDictionary($items); |
|
| 245 | + |
|
| 246 | + foreach ($this->items as $item) { |
|
| 247 | + if (isset($dictionary[$this->getEntityKey($item)])) { |
|
| 248 | + $intersect->add($item); |
|
| 249 | + } |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + return $intersect; |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + /** |
|
| 256 | + * Returns only the models from the collection with the specified keys. |
|
| 257 | + * |
|
| 258 | + * @param mixed $keys |
|
| 259 | + * @return self |
|
| 260 | + */ |
|
| 261 | + public function only($keys) |
|
| 262 | + { |
|
| 263 | + $dictionary = array_only($this->getDictionary(), $keys); |
|
| 264 | + |
|
| 265 | + return new static(array_values($dictionary)); |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + /** |
|
| 269 | + * Returns all models in the collection except the models with specified keys. |
|
| 270 | + * |
|
| 271 | + * @param mixed $keys |
|
| 272 | + * @return self |
|
| 273 | + */ |
|
| 274 | + public function except($keys) |
|
| 275 | + { |
|
| 276 | + $dictionary = array_except($this->getDictionary(), $keys); |
|
| 277 | + |
|
| 278 | + return new static(array_values($dictionary)); |
|
| 279 | + } |
|
| 280 | + |
|
| 281 | + /** |
|
| 282 | + * Get a dictionary keyed by primary keys. |
|
| 283 | + * |
|
| 284 | + * @param \ArrayAccess|array $items |
|
| 285 | + * @throws MappingException |
|
| 286 | + * @return array |
|
| 287 | + */ |
|
| 288 | + public function getDictionary($items = null) |
|
| 289 | + { |
|
| 290 | + $items = is_null($items) ? $this->items : $items; |
|
| 291 | + |
|
| 292 | + $dictionary = []; |
|
| 293 | + |
|
| 294 | + foreach ($items as $value) { |
|
| 295 | + $dictionary[$this->getEntityKey($value)] = $value; |
|
| 296 | + } |
|
| 297 | + |
|
| 298 | + return $dictionary; |
|
| 299 | + } |
|
| 300 | + |
|
| 301 | + /** |
|
| 302 | + * @throws MappingException |
|
| 303 | + * @return array |
|
| 304 | + */ |
|
| 305 | + public function getEntityKeys() |
|
| 306 | + { |
|
| 307 | + return array_keys($this->getDictionary()); |
|
| 308 | + } |
|
| 309 | + |
|
| 310 | + /** |
|
| 311 | + * @param $entity |
|
| 312 | + * @throws MappingException |
|
| 313 | + * @return mixed |
|
| 314 | + */ |
|
| 315 | + protected function getEntityKey($entity) |
|
| 316 | + { |
|
| 317 | + $keyName = Manager::getMapper($entity)->getEntityMap()->getKeyName(); |
|
| 318 | + |
|
| 319 | + $wrapper = $this->factory->make($entity); |
|
| 320 | + |
|
| 321 | + return $wrapper->getEntityAttribute($keyName); |
|
| 322 | + } |
|
| 323 | + |
|
| 324 | + /** |
|
| 325 | + * Get the max value of a given key. |
|
| 326 | + * |
|
| 327 | + * @param string|null $key |
|
| 328 | + * @throws MappingException |
|
| 329 | + * @return mixed |
|
| 330 | + */ |
|
| 331 | + public function max($key = null) |
|
| 332 | + { |
|
| 333 | + return $this->reduce(function ($result, $item) use ($key) { |
|
| 334 | + $wrapper = $this->factory->make($item); |
|
| 335 | + |
|
| 336 | + return (is_null($result) || $wrapper->getEntityAttribute($key) > $result) ? |
|
| 337 | + $wrapper->getEntityAttribute($key) : $result; |
|
| 338 | + }); |
|
| 339 | + } |
|
| 340 | + |
|
| 341 | + /** |
|
| 342 | + * Get the min value of a given key. |
|
| 343 | + * |
|
| 344 | + * @param string|null $key |
|
| 345 | + * @throws MappingException |
|
| 346 | + * @return mixed |
|
| 347 | + */ |
|
| 348 | + public function min($key = null) |
|
| 349 | + { |
|
| 350 | + return $this->reduce(function ($result, $item) use ($key) { |
|
| 351 | + $wrapper = $this->factory->make($item); |
|
| 352 | + |
|
| 353 | + return (is_null($result) || $wrapper->getEntityAttribute($key) < $result) |
|
| 354 | + ? $wrapper->getEntityAttribute($key) : $result; |
|
| 355 | + }); |
|
| 356 | + } |
|
| 357 | + |
|
| 358 | + /** |
|
| 359 | + * Get an array with the values of a given key. |
|
| 360 | + * |
|
| 361 | + * @param string $value |
|
| 362 | + * @param string|null $key |
|
| 363 | + * @return self |
|
| 364 | + */ |
|
| 365 | + public function pluck($value, $key = null) |
|
| 366 | + { |
|
| 367 | + return new Collection(Arr::pluck($this->items, $value, $key)); |
|
| 368 | + } |
|
| 369 | + |
|
| 370 | + /** |
|
| 371 | + * Alias for the "pluck" method. |
|
| 372 | + * |
|
| 373 | + * @param string $value |
|
| 374 | + * @param string|null $key |
|
| 375 | + * @return self |
|
| 376 | + */ |
|
| 377 | + public function lists($value, $key = null) |
|
| 378 | + { |
|
| 379 | + return $this->pluck($value, $key); |
|
| 380 | + } |
|
| 381 | + |
|
| 382 | + /** |
|
| 383 | + * Return only unique items from the collection. |
|
| 384 | + * |
|
| 385 | + * @param string|null $key |
|
| 386 | + * @param bool $strict |
|
| 387 | + * @throws MappingException |
|
| 388 | + * @return self |
|
| 389 | + */ |
|
| 390 | + public function unique($key = null, $strict = false) |
|
| 391 | + { |
|
| 392 | + $dictionary = $this->getDictionary(); |
|
| 393 | + |
|
| 394 | + return new static(array_values($dictionary)); |
|
| 395 | + } |
|
| 396 | + |
|
| 397 | + /** |
|
| 398 | + * Get a base Support collection instance from this collection. |
|
| 399 | + * |
|
| 400 | + * @return \Illuminate\Support\Collection |
|
| 401 | + */ |
|
| 402 | + public function toBase() |
|
| 403 | + { |
|
| 404 | + return new Collection($this->items); |
|
| 405 | + } |
|
| 406 | + |
|
| 407 | + public function toArray() |
|
| 408 | + { |
|
| 409 | + return array_values(parent::toArray()); |
|
| 410 | + } |
|
| 411 | + |
|
| 412 | + /** |
|
| 413 | + * Get the collection of items as JSON. |
|
| 414 | + * |
|
| 415 | + * @param int $options |
|
| 416 | + * @return string |
|
| 417 | + */ |
|
| 418 | + public function toJson($options = 0) |
|
| 419 | + { |
|
| 420 | + $collection = new Collection(array_values($this->items)); |
|
| 421 | + return $collection->toJson($options); |
|
| 422 | + } |
|
| 423 | 423 | } |
@@ -6,239 +6,239 @@ |
||
| 6 | 6 | |
| 7 | 7 | class PlainObjectWrapper extends Wrapper |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * The list of attributes for the managed entity |
|
| 11 | - * |
|
| 12 | - * @var array |
|
| 13 | - */ |
|
| 14 | - protected $attributeList; |
|
| 15 | - |
|
| 16 | - /** |
|
| 17 | - * The reflection class for the managed entity |
|
| 18 | - * |
|
| 19 | - * @var ReflectionClass |
|
| 20 | - */ |
|
| 21 | - protected $reflection; |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * Attributes which have no existence on the actual entity |
|
| 25 | - * but which exists in DB (eg : foreign key) |
|
| 26 | - * |
|
| 27 | - * @var array |
|
| 28 | - */ |
|
| 29 | - protected $virtualAttributes = []; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * PlainObjectWrapper constructor. |
|
| 33 | - * |
|
| 34 | - * @param $popoEntity |
|
| 35 | - * @param $entityMap |
|
| 36 | - */ |
|
| 37 | - public function __construct($popoEntity, $entityMap) |
|
| 38 | - { |
|
| 39 | - $this->reflection = new ReflectionClass($popoEntity); |
|
| 40 | - |
|
| 41 | - parent::__construct($popoEntity, $entityMap); |
|
| 42 | - |
|
| 43 | - $this->attributeList = $this->getAttributeList(); |
|
| 44 | - } |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * Get Compiled Attributes (key, attributes, embed, relations) |
|
| 48 | - * |
|
| 49 | - * @return array |
|
| 50 | - */ |
|
| 51 | - protected function getAttributeList() |
|
| 52 | - { |
|
| 53 | - return $this->entityMap->getCompiledAttributes(); |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * Extract Attributes from a Plain Php Object |
|
| 58 | - * |
|
| 59 | - * @return array $attributes |
|
| 60 | - */ |
|
| 61 | - protected function extract() |
|
| 62 | - { |
|
| 63 | - $properties = $this->getMappedProperties(); |
|
| 64 | - |
|
| 65 | - $attributes = []; |
|
| 66 | - |
|
| 67 | - foreach ($properties as $property) { |
|
| 68 | - $name = $property->getName(); |
|
| 69 | - |
|
| 70 | - if ($property->isPublic()) { |
|
| 71 | - $attributes[$name] = $this->entity->$name; |
|
| 72 | - } else { |
|
| 73 | - $property->setAccessible(true); |
|
| 9 | + /** |
|
| 10 | + * The list of attributes for the managed entity |
|
| 11 | + * |
|
| 12 | + * @var array |
|
| 13 | + */ |
|
| 14 | + protected $attributeList; |
|
| 15 | + |
|
| 16 | + /** |
|
| 17 | + * The reflection class for the managed entity |
|
| 18 | + * |
|
| 19 | + * @var ReflectionClass |
|
| 20 | + */ |
|
| 21 | + protected $reflection; |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * Attributes which have no existence on the actual entity |
|
| 25 | + * but which exists in DB (eg : foreign key) |
|
| 26 | + * |
|
| 27 | + * @var array |
|
| 28 | + */ |
|
| 29 | + protected $virtualAttributes = []; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * PlainObjectWrapper constructor. |
|
| 33 | + * |
|
| 34 | + * @param $popoEntity |
|
| 35 | + * @param $entityMap |
|
| 36 | + */ |
|
| 37 | + public function __construct($popoEntity, $entityMap) |
|
| 38 | + { |
|
| 39 | + $this->reflection = new ReflectionClass($popoEntity); |
|
| 40 | + |
|
| 41 | + parent::__construct($popoEntity, $entityMap); |
|
| 42 | + |
|
| 43 | + $this->attributeList = $this->getAttributeList(); |
|
| 44 | + } |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * Get Compiled Attributes (key, attributes, embed, relations) |
|
| 48 | + * |
|
| 49 | + * @return array |
|
| 50 | + */ |
|
| 51 | + protected function getAttributeList() |
|
| 52 | + { |
|
| 53 | + return $this->entityMap->getCompiledAttributes(); |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * Extract Attributes from a Plain Php Object |
|
| 58 | + * |
|
| 59 | + * @return array $attributes |
|
| 60 | + */ |
|
| 61 | + protected function extract() |
|
| 62 | + { |
|
| 63 | + $properties = $this->getMappedProperties(); |
|
| 64 | + |
|
| 65 | + $attributes = []; |
|
| 66 | + |
|
| 67 | + foreach ($properties as $property) { |
|
| 68 | + $name = $property->getName(); |
|
| 69 | + |
|
| 70 | + if ($property->isPublic()) { |
|
| 71 | + $attributes[$name] = $this->entity->$name; |
|
| 72 | + } else { |
|
| 73 | + $property->setAccessible(true); |
|
| 74 | 74 | |
| 75 | - $attributes[$name] = $property->getValue($this->entity); |
|
| 76 | - } |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - return $attributes; |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * @return \ReflectionProperty[] |
|
| 84 | - */ |
|
| 85 | - protected function getMappedProperties() |
|
| 86 | - { |
|
| 87 | - $objectProperties = $this->reflection->getProperties(); |
|
| 88 | - |
|
| 89 | - $attributeList = $this->getAttributeList(); |
|
| 90 | - |
|
| 91 | - // We need to filter out properties that could belong to the object |
|
| 92 | - // and which are not intended to be handled by the ORM |
|
| 93 | - return array_filter($objectProperties, function (\ReflectionProperty $item) use ($attributeList) { |
|
| 94 | - if (in_array($item->getName(), $attributeList)) { |
|
| 95 | - return true; |
|
| 96 | - } |
|
| 97 | - }); |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * Get the property's value from reflection |
|
| 102 | - * |
|
| 103 | - * @param string $name |
|
| 104 | - * @return \ReflectionProperty | null |
|
| 105 | - */ |
|
| 106 | - protected function getMappedProperty($name) |
|
| 107 | - { |
|
| 108 | - return $this->reflection->getProperty($name); |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * Hydrate Plain PHP Object with wrapped attributes |
|
| 113 | - * |
|
| 114 | - * @param $attributes |
|
| 115 | - * @return void |
|
| 116 | - */ |
|
| 117 | - protected function hydrate($attributes) |
|
| 118 | - { |
|
| 119 | - $properties = $this->getMappedProperties(); |
|
| 120 | - |
|
| 121 | - foreach ($properties as $property) { |
|
| 122 | - $name = $property->getName(); |
|
| 123 | - |
|
| 124 | - if ($property->isPublic()) { |
|
| 125 | - $this->entity->$name = $attributes[$name]; |
|
| 126 | - } else { |
|
| 127 | - $property->setAccessible(true); |
|
| 128 | - |
|
| 129 | - $property->setValue($this->entity, $attributes[$name]); |
|
| 130 | - } |
|
| 131 | - } |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - /** |
|
| 135 | - * Method used by the mapper to set the object |
|
| 136 | - * attribute raw values (hydration) |
|
| 137 | - * |
|
| 138 | - * @param array $attributes |
|
| 139 | - * |
|
| 140 | - * @return void |
|
| 141 | - */ |
|
| 142 | - public function setEntityAttributes(array $attributes) |
|
| 143 | - { |
|
| 144 | - $this->hydrate($attributes); |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - /** |
|
| 148 | - * Method used by the mapper to get the |
|
| 149 | - * raw object's values. |
|
| 150 | - * |
|
| 151 | - * @return array |
|
| 152 | - */ |
|
| 153 | - public function getEntityAttributes() |
|
| 154 | - { |
|
| 155 | - $properties = $this->extract(); |
|
| 156 | - |
|
| 157 | - return array_merge($properties, $this->virtualAttributes); |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - /** |
|
| 161 | - * Method used by the mapper to set raw |
|
| 162 | - * key-value pair |
|
| 163 | - * |
|
| 164 | - * @param string $key |
|
| 165 | - * @param string $value |
|
| 166 | - * |
|
| 167 | - * @return void |
|
| 168 | - */ |
|
| 169 | - public function setEntityAttribute($key, $value) |
|
| 170 | - { |
|
| 171 | - if(! $this->reflection->hasProperty($key)) { |
|
| 172 | - $this->virtualAttributes[$key] = $value; |
|
| 173 | - return; |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - $property = $this->getMappedProperty($key); |
|
| 177 | - |
|
| 178 | - if ($property->isPublic()) { |
|
| 179 | - $this->entity->$key = $value; |
|
| 180 | - } else { |
|
| 181 | - $property->setAccessible(true); |
|
| 75 | + $attributes[$name] = $property->getValue($this->entity); |
|
| 76 | + } |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + return $attributes; |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * @return \ReflectionProperty[] |
|
| 84 | + */ |
|
| 85 | + protected function getMappedProperties() |
|
| 86 | + { |
|
| 87 | + $objectProperties = $this->reflection->getProperties(); |
|
| 88 | + |
|
| 89 | + $attributeList = $this->getAttributeList(); |
|
| 90 | + |
|
| 91 | + // We need to filter out properties that could belong to the object |
|
| 92 | + // and which are not intended to be handled by the ORM |
|
| 93 | + return array_filter($objectProperties, function (\ReflectionProperty $item) use ($attributeList) { |
|
| 94 | + if (in_array($item->getName(), $attributeList)) { |
|
| 95 | + return true; |
|
| 96 | + } |
|
| 97 | + }); |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * Get the property's value from reflection |
|
| 102 | + * |
|
| 103 | + * @param string $name |
|
| 104 | + * @return \ReflectionProperty | null |
|
| 105 | + */ |
|
| 106 | + protected function getMappedProperty($name) |
|
| 107 | + { |
|
| 108 | + return $this->reflection->getProperty($name); |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * Hydrate Plain PHP Object with wrapped attributes |
|
| 113 | + * |
|
| 114 | + * @param $attributes |
|
| 115 | + * @return void |
|
| 116 | + */ |
|
| 117 | + protected function hydrate($attributes) |
|
| 118 | + { |
|
| 119 | + $properties = $this->getMappedProperties(); |
|
| 120 | + |
|
| 121 | + foreach ($properties as $property) { |
|
| 122 | + $name = $property->getName(); |
|
| 123 | + |
|
| 124 | + if ($property->isPublic()) { |
|
| 125 | + $this->entity->$name = $attributes[$name]; |
|
| 126 | + } else { |
|
| 127 | + $property->setAccessible(true); |
|
| 128 | + |
|
| 129 | + $property->setValue($this->entity, $attributes[$name]); |
|
| 130 | + } |
|
| 131 | + } |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + /** |
|
| 135 | + * Method used by the mapper to set the object |
|
| 136 | + * attribute raw values (hydration) |
|
| 137 | + * |
|
| 138 | + * @param array $attributes |
|
| 139 | + * |
|
| 140 | + * @return void |
|
| 141 | + */ |
|
| 142 | + public function setEntityAttributes(array $attributes) |
|
| 143 | + { |
|
| 144 | + $this->hydrate($attributes); |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + /** |
|
| 148 | + * Method used by the mapper to get the |
|
| 149 | + * raw object's values. |
|
| 150 | + * |
|
| 151 | + * @return array |
|
| 152 | + */ |
|
| 153 | + public function getEntityAttributes() |
|
| 154 | + { |
|
| 155 | + $properties = $this->extract(); |
|
| 156 | + |
|
| 157 | + return array_merge($properties, $this->virtualAttributes); |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + /** |
|
| 161 | + * Method used by the mapper to set raw |
|
| 162 | + * key-value pair |
|
| 163 | + * |
|
| 164 | + * @param string $key |
|
| 165 | + * @param string $value |
|
| 166 | + * |
|
| 167 | + * @return void |
|
| 168 | + */ |
|
| 169 | + public function setEntityAttribute($key, $value) |
|
| 170 | + { |
|
| 171 | + if(! $this->reflection->hasProperty($key)) { |
|
| 172 | + $this->virtualAttributes[$key] = $value; |
|
| 173 | + return; |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + $property = $this->getMappedProperty($key); |
|
| 177 | + |
|
| 178 | + if ($property->isPublic()) { |
|
| 179 | + $this->entity->$key = $value; |
|
| 180 | + } else { |
|
| 181 | + $property->setAccessible(true); |
|
| 182 | 182 | |
| 183 | - $property->setValue($this->entity, $value); |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - $this->attributes[$key] = $value; |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - /** |
|
| 190 | - * Method used by the mapper to get single |
|
| 191 | - * key-value pair |
|
| 192 | - * |
|
| 193 | - * @param string $key |
|
| 194 | - * @return mixed|null |
|
| 195 | - */ |
|
| 196 | - public function getEntityAttribute($key) |
|
| 197 | - { |
|
| 198 | - if(! $this->reflection->hasProperty($key)) { |
|
| 199 | - return $this->getVirtualAttribute($key); |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - $property = $this->getMappedProperty($key); |
|
| 203 | - |
|
| 204 | - if ($property->isPublic()) { |
|
| 205 | - $value = $this->entity->$key; |
|
| 206 | - } else { |
|
| 207 | - $property->setAccessible(true); |
|
| 208 | - $value = $property->getValue($this->entity); |
|
| 209 | - } |
|
| 210 | - |
|
| 211 | - return $value; |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - /** |
|
| 215 | - * Return a virtual attributes |
|
| 216 | - * |
|
| 217 | - * @param string $key |
|
| 218 | - * @return mixed |
|
| 219 | - */ |
|
| 220 | - protected function getVirtualAttribute($key) |
|
| 221 | - { |
|
| 222 | - if(array_key_exists($key, $this->virtualAttributes)) { |
|
| 223 | - return $this->virtualAttributes[$key]; |
|
| 224 | - } |
|
| 225 | - else { |
|
| 226 | - return null; |
|
| 227 | - } |
|
| 228 | - } |
|
| 229 | - |
|
| 230 | - /** |
|
| 231 | - * Test if a given attribute exists |
|
| 232 | - * |
|
| 233 | - * @param string $key |
|
| 234 | - * @return boolean |
|
| 235 | - */ |
|
| 236 | - public function hasAttribute($key) |
|
| 237 | - { |
|
| 238 | - if (in_array($key, $this->attributeList)) { |
|
| 239 | - return true; |
|
| 240 | - } else { |
|
| 241 | - return false; |
|
| 242 | - } |
|
| 243 | - } |
|
| 183 | + $property->setValue($this->entity, $value); |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + $this->attributes[$key] = $value; |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + /** |
|
| 190 | + * Method used by the mapper to get single |
|
| 191 | + * key-value pair |
|
| 192 | + * |
|
| 193 | + * @param string $key |
|
| 194 | + * @return mixed|null |
|
| 195 | + */ |
|
| 196 | + public function getEntityAttribute($key) |
|
| 197 | + { |
|
| 198 | + if(! $this->reflection->hasProperty($key)) { |
|
| 199 | + return $this->getVirtualAttribute($key); |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + $property = $this->getMappedProperty($key); |
|
| 203 | + |
|
| 204 | + if ($property->isPublic()) { |
|
| 205 | + $value = $this->entity->$key; |
|
| 206 | + } else { |
|
| 207 | + $property->setAccessible(true); |
|
| 208 | + $value = $property->getValue($this->entity); |
|
| 209 | + } |
|
| 210 | + |
|
| 211 | + return $value; |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + /** |
|
| 215 | + * Return a virtual attributes |
|
| 216 | + * |
|
| 217 | + * @param string $key |
|
| 218 | + * @return mixed |
|
| 219 | + */ |
|
| 220 | + protected function getVirtualAttribute($key) |
|
| 221 | + { |
|
| 222 | + if(array_key_exists($key, $this->virtualAttributes)) { |
|
| 223 | + return $this->virtualAttributes[$key]; |
|
| 224 | + } |
|
| 225 | + else { |
|
| 226 | + return null; |
|
| 227 | + } |
|
| 228 | + } |
|
| 229 | + |
|
| 230 | + /** |
|
| 231 | + * Test if a given attribute exists |
|
| 232 | + * |
|
| 233 | + * @param string $key |
|
| 234 | + * @return boolean |
|
| 235 | + */ |
|
| 236 | + public function hasAttribute($key) |
|
| 237 | + { |
|
| 238 | + if (in_array($key, $this->attributeList)) { |
|
| 239 | + return true; |
|
| 240 | + } else { |
|
| 241 | + return false; |
|
| 242 | + } |
|
| 243 | + } |
|
| 244 | 244 | } |
@@ -90,7 +90,7 @@ discard block |
||
| 90 | 90 | |
| 91 | 91 | // We need to filter out properties that could belong to the object |
| 92 | 92 | // and which are not intended to be handled by the ORM |
| 93 | - return array_filter($objectProperties, function (\ReflectionProperty $item) use ($attributeList) { |
|
| 93 | + return array_filter($objectProperties, function(\ReflectionProperty $item) use ($attributeList) { |
|
| 94 | 94 | if (in_array($item->getName(), $attributeList)) { |
| 95 | 95 | return true; |
| 96 | 96 | } |
@@ -168,7 +168,7 @@ discard block |
||
| 168 | 168 | */ |
| 169 | 169 | public function setEntityAttribute($key, $value) |
| 170 | 170 | { |
| 171 | - if(! $this->reflection->hasProperty($key)) { |
|
| 171 | + if (!$this->reflection->hasProperty($key)) { |
|
| 172 | 172 | $this->virtualAttributes[$key] = $value; |
| 173 | 173 | return; |
| 174 | 174 | } |
@@ -195,7 +195,7 @@ discard block |
||
| 195 | 195 | */ |
| 196 | 196 | public function getEntityAttribute($key) |
| 197 | 197 | { |
| 198 | - if(! $this->reflection->hasProperty($key)) { |
|
| 198 | + if (!$this->reflection->hasProperty($key)) { |
|
| 199 | 199 | return $this->getVirtualAttribute($key); |
| 200 | 200 | } |
| 201 | 201 | |
@@ -219,7 +219,7 @@ discard block |
||
| 219 | 219 | */ |
| 220 | 220 | protected function getVirtualAttribute($key) |
| 221 | 221 | { |
| 222 | - if(array_key_exists($key, $this->virtualAttributes)) { |
|
| 222 | + if (array_key_exists($key, $this->virtualAttributes)) { |
|
| 223 | 223 | return $this->virtualAttributes[$key]; |
| 224 | 224 | } |
| 225 | 225 | else { |
@@ -221,8 +221,7 @@ |
||
| 221 | 221 | { |
| 222 | 222 | if(array_key_exists($key, $this->virtualAttributes)) { |
| 223 | 223 | return $this->virtualAttributes[$key]; |
| 224 | - } |
|
| 225 | - else { |
|
| 224 | + } else { |
|
| 226 | 225 | return null; |
| 227 | 226 | } |
| 228 | 227 | } |
@@ -122,7 +122,7 @@ |
||
| 122 | 122 | |
| 123 | 123 | $columnMap = $this->entityMap->getDiscriminatorColumnMap(); |
| 124 | 124 | |
| 125 | - $class = isset($columnMap[$type]) ? $columnMap[$type]: $type; |
|
| 125 | + $class = isset($columnMap[$type]) ? $columnMap[$type] : $type; |
|
| 126 | 126 | |
| 127 | 127 | if (!isset($this->builders[$type])) { |
| 128 | 128 | $this->builders[$type] = new EntityBuilder($this->manager->mapper($class), array_keys($this->eagerLoads)); |
@@ -6,128 +6,128 @@ |
||
| 6 | 6 | |
| 7 | 7 | class ResultBuilder |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * An instance of the entity manager class. |
|
| 11 | - * @var \Analogue\ORM\System\Manager |
|
| 12 | - */ |
|
| 13 | - protected $manager; |
|
| 14 | - |
|
| 15 | - /** |
|
| 16 | - * The default mapper used to build entities with. |
|
| 17 | - * @var \Analogue\ORM\System\Mapper |
|
| 18 | - */ |
|
| 19 | - protected $defaultMapper; |
|
| 20 | - |
|
| 21 | - /** |
|
| 22 | - * Relations that will be eager loaded on this query |
|
| 23 | - * |
|
| 24 | - * @var array |
|
| 25 | - */ |
|
| 26 | - protected $eagerLoads; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * The Entity Map for the entity to build. |
|
| 30 | - * |
|
| 31 | - * @var \Analogue\ORM\EntityMap |
|
| 32 | - */ |
|
| 33 | - protected $entityMap; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * An array of builders used by this class to build necessary |
|
| 37 | - * entities for each result type. |
|
| 38 | - * |
|
| 39 | - * @var array |
|
| 40 | - */ |
|
| 41 | - protected $builders = []; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * @param Manager $manager |
|
| 45 | - * @param Mapper $defaultMapper |
|
| 46 | - * @param array $eagerLoads |
|
| 47 | - */ |
|
| 48 | - public function __construct(Manager $manager, Mapper $defaultMapper, array $eagerLoads) |
|
| 49 | - { |
|
| 50 | - $this->manager = $manager; |
|
| 51 | - |
|
| 52 | - $this->defaultMapper = $defaultMapper; |
|
| 53 | - |
|
| 54 | - $this->eagerLoads = $eagerLoads; |
|
| 55 | - |
|
| 56 | - $this->entityMap = $defaultMapper->getEntityMap(); |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * Convert a result set into an array of entities |
|
| 61 | - * |
|
| 62 | - * @param array $results |
|
| 63 | - * @return \Illuminate\Support\Collection |
|
| 64 | - */ |
|
| 65 | - public function build($results) |
|
| 66 | - { |
|
| 67 | - switch ($this->entityMap->getInheritanceType()) { |
|
| 68 | - case 'single_table': |
|
| 69 | - return $this->buildUsingSingleTableInheritance($results); |
|
| 70 | - break; |
|
| 71 | - |
|
| 72 | - default: |
|
| 73 | - return $this->buildWithDefaultMapper($results); |
|
| 74 | - break; |
|
| 75 | - } |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * Build an entity from results, using the default mapper on this builder. |
|
| 80 | - * This is the default build plan when no table inheritance is being used. |
|
| 81 | - * |
|
| 82 | - * @param array $results |
|
| 83 | - * @return Collection |
|
| 84 | - */ |
|
| 85 | - protected function buildWithDefaultMapper($results) |
|
| 86 | - { |
|
| 87 | - $builder = new EntityBuilder($this->defaultMapper, array_keys($this->eagerLoads)); |
|
| 88 | - |
|
| 89 | - return collect($results)->map(function($item, $key) use ($builder) { |
|
| 90 | - return $builder->build((array) $item); |
|
| 91 | - })->all(); |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * Build an entity from results, using single table inheritance. |
|
| 96 | - * |
|
| 97 | - * @param array $results |
|
| 98 | - * @return Collection |
|
| 99 | - */ |
|
| 100 | - protected function buildUsingSingleTableInheritance($results) |
|
| 101 | - { |
|
| 102 | - return collect($results)->map(function($item, $key) { |
|
| 103 | - $builder = $this->builderForResult((array) $item); |
|
| 104 | - |
|
| 105 | - return $builder->build((array) $item); |
|
| 106 | - })->all(); |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * Given a result array, return the entity builder needed to correctly |
|
| 111 | - * build the result into an entity. If no getDiscriminatorColumnMap property |
|
| 112 | - * has been defined on the EntityMap, we'll assume that the value stored in |
|
| 113 | - * the $type column is the fully qualified class name of the entity and |
|
| 114 | - * we'll use it instead. |
|
| 115 | - * |
|
| 116 | - * @param array $result |
|
| 117 | - * @return EntityBuilder |
|
| 118 | - */ |
|
| 119 | - protected function builderForResult(array $result) |
|
| 120 | - { |
|
| 121 | - $type = $result[$this->entityMap->getDiscriminatorColumn()]; |
|
| 122 | - |
|
| 123 | - $columnMap = $this->entityMap->getDiscriminatorColumnMap(); |
|
| 124 | - |
|
| 125 | - $class = isset($columnMap[$type]) ? $columnMap[$type]: $type; |
|
| 126 | - |
|
| 127 | - if (!isset($this->builders[$type])) { |
|
| 128 | - $this->builders[$type] = new EntityBuilder($this->manager->mapper($class), array_keys($this->eagerLoads)); |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - return $this->builders[$type]; |
|
| 132 | - } |
|
| 9 | + /** |
|
| 10 | + * An instance of the entity manager class. |
|
| 11 | + * @var \Analogue\ORM\System\Manager |
|
| 12 | + */ |
|
| 13 | + protected $manager; |
|
| 14 | + |
|
| 15 | + /** |
|
| 16 | + * The default mapper used to build entities with. |
|
| 17 | + * @var \Analogue\ORM\System\Mapper |
|
| 18 | + */ |
|
| 19 | + protected $defaultMapper; |
|
| 20 | + |
|
| 21 | + /** |
|
| 22 | + * Relations that will be eager loaded on this query |
|
| 23 | + * |
|
| 24 | + * @var array |
|
| 25 | + */ |
|
| 26 | + protected $eagerLoads; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * The Entity Map for the entity to build. |
|
| 30 | + * |
|
| 31 | + * @var \Analogue\ORM\EntityMap |
|
| 32 | + */ |
|
| 33 | + protected $entityMap; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * An array of builders used by this class to build necessary |
|
| 37 | + * entities for each result type. |
|
| 38 | + * |
|
| 39 | + * @var array |
|
| 40 | + */ |
|
| 41 | + protected $builders = []; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * @param Manager $manager |
|
| 45 | + * @param Mapper $defaultMapper |
|
| 46 | + * @param array $eagerLoads |
|
| 47 | + */ |
|
| 48 | + public function __construct(Manager $manager, Mapper $defaultMapper, array $eagerLoads) |
|
| 49 | + { |
|
| 50 | + $this->manager = $manager; |
|
| 51 | + |
|
| 52 | + $this->defaultMapper = $defaultMapper; |
|
| 53 | + |
|
| 54 | + $this->eagerLoads = $eagerLoads; |
|
| 55 | + |
|
| 56 | + $this->entityMap = $defaultMapper->getEntityMap(); |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * Convert a result set into an array of entities |
|
| 61 | + * |
|
| 62 | + * @param array $results |
|
| 63 | + * @return \Illuminate\Support\Collection |
|
| 64 | + */ |
|
| 65 | + public function build($results) |
|
| 66 | + { |
|
| 67 | + switch ($this->entityMap->getInheritanceType()) { |
|
| 68 | + case 'single_table': |
|
| 69 | + return $this->buildUsingSingleTableInheritance($results); |
|
| 70 | + break; |
|
| 71 | + |
|
| 72 | + default: |
|
| 73 | + return $this->buildWithDefaultMapper($results); |
|
| 74 | + break; |
|
| 75 | + } |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * Build an entity from results, using the default mapper on this builder. |
|
| 80 | + * This is the default build plan when no table inheritance is being used. |
|
| 81 | + * |
|
| 82 | + * @param array $results |
|
| 83 | + * @return Collection |
|
| 84 | + */ |
|
| 85 | + protected function buildWithDefaultMapper($results) |
|
| 86 | + { |
|
| 87 | + $builder = new EntityBuilder($this->defaultMapper, array_keys($this->eagerLoads)); |
|
| 88 | + |
|
| 89 | + return collect($results)->map(function($item, $key) use ($builder) { |
|
| 90 | + return $builder->build((array) $item); |
|
| 91 | + })->all(); |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * Build an entity from results, using single table inheritance. |
|
| 96 | + * |
|
| 97 | + * @param array $results |
|
| 98 | + * @return Collection |
|
| 99 | + */ |
|
| 100 | + protected function buildUsingSingleTableInheritance($results) |
|
| 101 | + { |
|
| 102 | + return collect($results)->map(function($item, $key) { |
|
| 103 | + $builder = $this->builderForResult((array) $item); |
|
| 104 | + |
|
| 105 | + return $builder->build((array) $item); |
|
| 106 | + })->all(); |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * Given a result array, return the entity builder needed to correctly |
|
| 111 | + * build the result into an entity. If no getDiscriminatorColumnMap property |
|
| 112 | + * has been defined on the EntityMap, we'll assume that the value stored in |
|
| 113 | + * the $type column is the fully qualified class name of the entity and |
|
| 114 | + * we'll use it instead. |
|
| 115 | + * |
|
| 116 | + * @param array $result |
|
| 117 | + * @return EntityBuilder |
|
| 118 | + */ |
|
| 119 | + protected function builderForResult(array $result) |
|
| 120 | + { |
|
| 121 | + $type = $result[$this->entityMap->getDiscriminatorColumn()]; |
|
| 122 | + |
|
| 123 | + $columnMap = $this->entityMap->getDiscriminatorColumnMap(); |
|
| 124 | + |
|
| 125 | + $class = isset($columnMap[$type]) ? $columnMap[$type]: $type; |
|
| 126 | + |
|
| 127 | + if (!isset($this->builders[$type])) { |
|
| 128 | + $this->builders[$type] = new EntityBuilder($this->manager->mapper($class), array_keys($this->eagerLoads)); |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + return $this->builders[$type]; |
|
| 132 | + } |
|
| 133 | 133 | } |
| 134 | 134 | \ No newline at end of file |