@@ -4,44 +4,44 @@ |
||
| 4 | 4 | |
| 5 | 5 | class IlluminateDriver implements DriverInterface |
| 6 | 6 | { |
| 7 | - /** |
|
| 8 | - * The Illuminate Connection Provider. |
|
| 9 | - * |
|
| 10 | - * @var CapsuleConnectionProvider|IlluminateConnectionProvider |
|
| 11 | - */ |
|
| 12 | - protected $connectionProvider; |
|
| 7 | + /** |
|
| 8 | + * The Illuminate Connection Provider. |
|
| 9 | + * |
|
| 10 | + * @var CapsuleConnectionProvider|IlluminateConnectionProvider |
|
| 11 | + */ |
|
| 12 | + protected $connectionProvider; |
|
| 13 | 13 | |
| 14 | - /** |
|
| 15 | - * IlluminateDriver constructor. |
|
| 16 | - * |
|
| 17 | - * @param $connectionProvider |
|
| 18 | - */ |
|
| 19 | - public function __construct($connectionProvider) |
|
| 20 | - { |
|
| 21 | - $this->connectionProvider = $connectionProvider; |
|
| 22 | - } |
|
| 14 | + /** |
|
| 15 | + * IlluminateDriver constructor. |
|
| 16 | + * |
|
| 17 | + * @param $connectionProvider |
|
| 18 | + */ |
|
| 19 | + public function __construct($connectionProvider) |
|
| 20 | + { |
|
| 21 | + $this->connectionProvider = $connectionProvider; |
|
| 22 | + } |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * Return the name of the driver. |
|
| 26 | - * |
|
| 27 | - * @return string |
|
| 28 | - */ |
|
| 29 | - public function getName() |
|
| 30 | - { |
|
| 31 | - return 'illuminate'; |
|
| 32 | - } |
|
| 24 | + /** |
|
| 25 | + * Return the name of the driver. |
|
| 26 | + * |
|
| 27 | + * @return string |
|
| 28 | + */ |
|
| 29 | + public function getName() |
|
| 30 | + { |
|
| 31 | + return 'illuminate'; |
|
| 32 | + } |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * Get Analogue DBAdapter. |
|
| 36 | - * |
|
| 37 | - * @param string|null $connection |
|
| 38 | - * |
|
| 39 | - * @return IlluminateDBAdapter |
|
| 40 | - */ |
|
| 41 | - public function getAdapter($connection = null) |
|
| 42 | - { |
|
| 43 | - $connection = $this->connectionProvider->connection($connection); |
|
| 34 | + /** |
|
| 35 | + * Get Analogue DBAdapter. |
|
| 36 | + * |
|
| 37 | + * @param string|null $connection |
|
| 38 | + * |
|
| 39 | + * @return IlluminateDBAdapter |
|
| 40 | + */ |
|
| 41 | + public function getAdapter($connection = null) |
|
| 42 | + { |
|
| 43 | + $connection = $this->connectionProvider->connection($connection); |
|
| 44 | 44 | |
| 45 | - return new IlluminateDBAdapter($connection); |
|
| 46 | - } |
|
| 45 | + return new IlluminateDBAdapter($connection); |
|
| 46 | + } |
|
| 47 | 47 | } |
@@ -6,30 +6,30 @@ |
||
| 6 | 6 | |
| 7 | 7 | class CapsuleConnectionProvider |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * @var Capsule |
|
| 11 | - */ |
|
| 12 | - protected $capsule; |
|
| 9 | + /** |
|
| 10 | + * @var Capsule |
|
| 11 | + */ |
|
| 12 | + protected $capsule; |
|
| 13 | 13 | |
| 14 | - /** |
|
| 15 | - * CapsuleConnectionProvider constructor. |
|
| 16 | - * |
|
| 17 | - * @param Capsule $capsule |
|
| 18 | - */ |
|
| 19 | - public function __construct(Capsule $capsule) |
|
| 20 | - { |
|
| 21 | - $this->capsule = $capsule; |
|
| 22 | - } |
|
| 14 | + /** |
|
| 15 | + * CapsuleConnectionProvider constructor. |
|
| 16 | + * |
|
| 17 | + * @param Capsule $capsule |
|
| 18 | + */ |
|
| 19 | + public function __construct(Capsule $capsule) |
|
| 20 | + { |
|
| 21 | + $this->capsule = $capsule; |
|
| 22 | + } |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * Get a Database connection object. |
|
| 26 | - * |
|
| 27 | - * @param $name |
|
| 28 | - * |
|
| 29 | - * @return \Illuminate\Database\Connection |
|
| 30 | - */ |
|
| 31 | - public function connection($name = null) |
|
| 32 | - { |
|
| 33 | - return $this->capsule->getConnection($name); |
|
| 34 | - } |
|
| 24 | + /** |
|
| 25 | + * Get a Database connection object. |
|
| 26 | + * |
|
| 27 | + * @param $name |
|
| 28 | + * |
|
| 29 | + * @return \Illuminate\Database\Connection |
|
| 30 | + */ |
|
| 31 | + public function connection($name = null) |
|
| 32 | + { |
|
| 33 | + return $this->capsule->getConnection($name); |
|
| 34 | + } |
|
| 35 | 35 | } |
@@ -4,33 +4,33 @@ |
||
| 4 | 4 | |
| 5 | 5 | class Manager |
| 6 | 6 | { |
| 7 | - /** |
|
| 8 | - * @var DriverInterface[] |
|
| 9 | - */ |
|
| 10 | - protected $drivers = []; |
|
| 7 | + /** |
|
| 8 | + * @var DriverInterface[] |
|
| 9 | + */ |
|
| 10 | + protected $drivers = []; |
|
| 11 | 11 | |
| 12 | - /** |
|
| 13 | - * Add a Mapping Driver. |
|
| 14 | - * |
|
| 15 | - * @param DriverInterface $driver |
|
| 16 | - */ |
|
| 17 | - public function addDriver(DriverInterface $driver) |
|
| 18 | - { |
|
| 19 | - $this->drivers[$driver->getName()] = $driver; |
|
| 20 | - } |
|
| 12 | + /** |
|
| 13 | + * Add a Mapping Driver. |
|
| 14 | + * |
|
| 15 | + * @param DriverInterface $driver |
|
| 16 | + */ |
|
| 17 | + public function addDriver(DriverInterface $driver) |
|
| 18 | + { |
|
| 19 | + $this->drivers[$driver->getName()] = $driver; |
|
| 20 | + } |
|
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * Get the DBAdapter. |
|
| 24 | - * |
|
| 25 | - * @param string $driver |
|
| 26 | - * @param string $connection connection name for drivers supporting multiple connection. |
|
| 27 | - * |
|
| 28 | - * @return DriverInterface|void |
|
| 29 | - */ |
|
| 30 | - public function getAdapter($driver, $connection = null) |
|
| 31 | - { |
|
| 32 | - if (array_key_exists($driver, $this->drivers)) { |
|
| 33 | - return $this->drivers[$driver]->getAdapter($connection); |
|
| 34 | - } |
|
| 35 | - } |
|
| 22 | + /** |
|
| 23 | + * Get the DBAdapter. |
|
| 24 | + * |
|
| 25 | + * @param string $driver |
|
| 26 | + * @param string $connection connection name for drivers supporting multiple connection. |
|
| 27 | + * |
|
| 28 | + * @return DriverInterface|void |
|
| 29 | + */ |
|
| 30 | + public function getAdapter($driver, $connection = null) |
|
| 31 | + { |
|
| 32 | + if (array_key_exists($driver, $this->drivers)) { |
|
| 33 | + return $this->drivers[$driver]->getAdapter($connection); |
|
| 34 | + } |
|
| 35 | + } |
|
| 36 | 36 | } |
@@ -8,117 +8,117 @@ |
||
| 8 | 8 | |
| 9 | 9 | trait MagicCasting |
| 10 | 10 | { |
| 11 | - /** |
|
| 12 | - * Determine if the given attribute exists. |
|
| 13 | - * |
|
| 14 | - * @param mixed $offset |
|
| 15 | - * |
|
| 16 | - * @return bool |
|
| 17 | - */ |
|
| 18 | - public function offsetExists($offset) |
|
| 19 | - { |
|
| 20 | - return isset($this->$offset); |
|
| 21 | - } |
|
| 11 | + /** |
|
| 12 | + * Determine if the given attribute exists. |
|
| 13 | + * |
|
| 14 | + * @param mixed $offset |
|
| 15 | + * |
|
| 16 | + * @return bool |
|
| 17 | + */ |
|
| 18 | + public function offsetExists($offset) |
|
| 19 | + { |
|
| 20 | + return isset($this->$offset); |
|
| 21 | + } |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * Get the value for a given offset. |
|
| 25 | - * |
|
| 26 | - * @param mixed $offset |
|
| 27 | - * |
|
| 28 | - * @return mixed |
|
| 29 | - */ |
|
| 30 | - public function offsetGet($offset) |
|
| 31 | - { |
|
| 32 | - return $this->$offset; |
|
| 33 | - } |
|
| 23 | + /** |
|
| 24 | + * Get the value for a given offset. |
|
| 25 | + * |
|
| 26 | + * @param mixed $offset |
|
| 27 | + * |
|
| 28 | + * @return mixed |
|
| 29 | + */ |
|
| 30 | + public function offsetGet($offset) |
|
| 31 | + { |
|
| 32 | + return $this->$offset; |
|
| 33 | + } |
|
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * Set the value for a given offset. |
|
| 37 | - * |
|
| 38 | - * @param mixed $offset |
|
| 39 | - * @param mixed $value |
|
| 40 | - * |
|
| 41 | - * @return void |
|
| 42 | - */ |
|
| 43 | - public function offsetSet($offset, $value) |
|
| 44 | - { |
|
| 45 | - $this->$offset = $value; |
|
| 46 | - } |
|
| 35 | + /** |
|
| 36 | + * Set the value for a given offset. |
|
| 37 | + * |
|
| 38 | + * @param mixed $offset |
|
| 39 | + * @param mixed $value |
|
| 40 | + * |
|
| 41 | + * @return void |
|
| 42 | + */ |
|
| 43 | + public function offsetSet($offset, $value) |
|
| 44 | + { |
|
| 45 | + $this->$offset = $value; |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * Unset the value for a given offset. |
|
| 50 | - * |
|
| 51 | - * @param mixed $offset |
|
| 52 | - * |
|
| 53 | - * @return void |
|
| 54 | - */ |
|
| 55 | - public function offsetUnset($offset) |
|
| 56 | - { |
|
| 57 | - unset($this->$offset); |
|
| 58 | - } |
|
| 48 | + /** |
|
| 49 | + * Unset the value for a given offset. |
|
| 50 | + * |
|
| 51 | + * @param mixed $offset |
|
| 52 | + * |
|
| 53 | + * @return void |
|
| 54 | + */ |
|
| 55 | + public function offsetUnset($offset) |
|
| 56 | + { |
|
| 57 | + unset($this->$offset); |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | - /** |
|
| 61 | - * Convert the object into something JSON serializable. |
|
| 62 | - * |
|
| 63 | - * @return array |
|
| 64 | - */ |
|
| 65 | - public function jsonSerialize() |
|
| 66 | - { |
|
| 67 | - return $this->toArray(); |
|
| 68 | - } |
|
| 60 | + /** |
|
| 61 | + * Convert the object into something JSON serializable. |
|
| 62 | + * |
|
| 63 | + * @return array |
|
| 64 | + */ |
|
| 65 | + public function jsonSerialize() |
|
| 66 | + { |
|
| 67 | + return $this->toArray(); |
|
| 68 | + } |
|
| 69 | 69 | |
| 70 | - /** |
|
| 71 | - * Convert the entity instance to JSON. |
|
| 72 | - * |
|
| 73 | - * @param int $options |
|
| 74 | - * |
|
| 75 | - * @return string |
|
| 76 | - */ |
|
| 77 | - public function toJson($options = 0) |
|
| 78 | - { |
|
| 79 | - return json_encode($this->toArray(), $options); |
|
| 80 | - } |
|
| 70 | + /** |
|
| 71 | + * Convert the entity instance to JSON. |
|
| 72 | + * |
|
| 73 | + * @param int $options |
|
| 74 | + * |
|
| 75 | + * @return string |
|
| 76 | + */ |
|
| 77 | + public function toJson($options = 0) |
|
| 78 | + { |
|
| 79 | + return json_encode($this->toArray(), $options); |
|
| 80 | + } |
|
| 81 | 81 | |
| 82 | - /** |
|
| 83 | - * Convert Mappable object to array;. |
|
| 84 | - * |
|
| 85 | - * @return array |
|
| 86 | - */ |
|
| 87 | - public function toArray() |
|
| 88 | - { |
|
| 89 | - return $this->attributesToArray($this->attributes); |
|
| 90 | - } |
|
| 82 | + /** |
|
| 83 | + * Convert Mappable object to array;. |
|
| 84 | + * |
|
| 85 | + * @return array |
|
| 86 | + */ |
|
| 87 | + public function toArray() |
|
| 88 | + { |
|
| 89 | + return $this->attributesToArray($this->attributes); |
|
| 90 | + } |
|
| 91 | 91 | |
| 92 | - /** |
|
| 93 | - * Transform the Object to array/json,. |
|
| 94 | - * |
|
| 95 | - * @param array $sourceAttributes |
|
| 96 | - * |
|
| 97 | - * @return array |
|
| 98 | - */ |
|
| 99 | - protected function attributesToArray(array $sourceAttributes) |
|
| 100 | - { |
|
| 101 | - $attributes = []; |
|
| 92 | + /** |
|
| 93 | + * Transform the Object to array/json,. |
|
| 94 | + * |
|
| 95 | + * @param array $sourceAttributes |
|
| 96 | + * |
|
| 97 | + * @return array |
|
| 98 | + */ |
|
| 99 | + protected function attributesToArray(array $sourceAttributes) |
|
| 100 | + { |
|
| 101 | + $attributes = []; |
|
| 102 | 102 | |
| 103 | - foreach ($sourceAttributes as $key => $attribute) { |
|
| 104 | - // If the attribute is a proxy, and hasn't be loaded, we discard |
|
| 105 | - // it from the returned set. |
|
| 106 | - if ($attribute instanceof ProxyInterface && !$attribute->isProxyInitialized()) { |
|
| 107 | - continue; |
|
| 108 | - } |
|
| 103 | + foreach ($sourceAttributes as $key => $attribute) { |
|
| 104 | + // If the attribute is a proxy, and hasn't be loaded, we discard |
|
| 105 | + // it from the returned set. |
|
| 106 | + if ($attribute instanceof ProxyInterface && !$attribute->isProxyInitialized()) { |
|
| 107 | + continue; |
|
| 108 | + } |
|
| 109 | 109 | |
| 110 | - if ($attribute instanceof Carbon) { |
|
| 111 | - $attributes[$key] = $attribute->__toString(); |
|
| 112 | - continue; |
|
| 113 | - } |
|
| 110 | + if ($attribute instanceof Carbon) { |
|
| 111 | + $attributes[$key] = $attribute->__toString(); |
|
| 112 | + continue; |
|
| 113 | + } |
|
| 114 | 114 | |
| 115 | - if ($attribute instanceof Arrayable) { |
|
| 116 | - $attributes[$key] = $attribute->toArray(); |
|
| 117 | - } else { |
|
| 118 | - $attributes[$key] = $attribute; |
|
| 119 | - } |
|
| 120 | - } |
|
| 115 | + if ($attribute instanceof Arrayable) { |
|
| 116 | + $attributes[$key] = $attribute->toArray(); |
|
| 117 | + } else { |
|
| 118 | + $attributes[$key] = $attribute; |
|
| 119 | + } |
|
| 120 | + } |
|
| 121 | 121 | |
| 122 | - return $attributes; |
|
| 123 | - } |
|
| 122 | + return $attributes; |
|
| 123 | + } |
|
| 124 | 124 | } |
@@ -8,69 +8,69 @@ |
||
| 8 | 8 | */ |
| 9 | 9 | class CachedRelationship |
| 10 | 10 | { |
| 11 | - /** |
|
| 12 | - * The Hash of the related entity. |
|
| 13 | - * |
|
| 14 | - * @var string |
|
| 15 | - */ |
|
| 16 | - protected $hash; |
|
| 11 | + /** |
|
| 12 | + * The Hash of the related entity. |
|
| 13 | + * |
|
| 14 | + * @var string |
|
| 15 | + */ |
|
| 16 | + protected $hash; |
|
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * Pivot attributes, if any. |
|
| 20 | - * |
|
| 21 | - * @var array |
|
| 22 | - */ |
|
| 23 | - protected $pivotAttributes; |
|
| 18 | + /** |
|
| 19 | + * Pivot attributes, if any. |
|
| 20 | + * |
|
| 21 | + * @var array |
|
| 22 | + */ |
|
| 23 | + protected $pivotAttributes; |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * CachedRelationship constructor. |
|
| 27 | - * |
|
| 28 | - * @param $hash |
|
| 29 | - * @param array $pivotAttributes |
|
| 30 | - */ |
|
| 31 | - public function __construct($hash, $pivotAttributes = []) |
|
| 32 | - { |
|
| 33 | - $this->hash = $hash; |
|
| 34 | - $this->pivotAttributes = $pivotAttributes; |
|
| 35 | - } |
|
| 25 | + /** |
|
| 26 | + * CachedRelationship constructor. |
|
| 27 | + * |
|
| 28 | + * @param $hash |
|
| 29 | + * @param array $pivotAttributes |
|
| 30 | + */ |
|
| 31 | + public function __construct($hash, $pivotAttributes = []) |
|
| 32 | + { |
|
| 33 | + $this->hash = $hash; |
|
| 34 | + $this->pivotAttributes = $pivotAttributes; |
|
| 35 | + } |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * Return true if any pivot attributes are present. |
|
| 39 | - * |
|
| 40 | - * @return bool |
|
| 41 | - */ |
|
| 42 | - public function hasPivotAttributes() |
|
| 43 | - { |
|
| 44 | - return count($this->pivotAttributes) > 0; |
|
| 45 | - } |
|
| 37 | + /** |
|
| 38 | + * Return true if any pivot attributes are present. |
|
| 39 | + * |
|
| 40 | + * @return bool |
|
| 41 | + */ |
|
| 42 | + public function hasPivotAttributes() |
|
| 43 | + { |
|
| 44 | + return count($this->pivotAttributes) > 0; |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * Returns the hash of the related entity. |
|
| 49 | - * |
|
| 50 | - * @return string |
|
| 51 | - */ |
|
| 52 | - public function getHash() |
|
| 53 | - { |
|
| 54 | - return $this->hash; |
|
| 55 | - } |
|
| 47 | + /** |
|
| 48 | + * Returns the hash of the related entity. |
|
| 49 | + * |
|
| 50 | + * @return string |
|
| 51 | + */ |
|
| 52 | + public function getHash() |
|
| 53 | + { |
|
| 54 | + return $this->hash; |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - /** |
|
| 58 | - * Get the cached values for the pivot attributes. |
|
| 59 | - * |
|
| 60 | - * @return array |
|
| 61 | - */ |
|
| 62 | - public function getPivotAttributes() |
|
| 63 | - { |
|
| 64 | - return $this->pivotAttributes; |
|
| 65 | - } |
|
| 57 | + /** |
|
| 58 | + * Get the cached values for the pivot attributes. |
|
| 59 | + * |
|
| 60 | + * @return array |
|
| 61 | + */ |
|
| 62 | + public function getPivotAttributes() |
|
| 63 | + { |
|
| 64 | + return $this->pivotAttributes; |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | - /** |
|
| 68 | - * Access to the hash for fast cache comparison. |
|
| 69 | - * |
|
| 70 | - * @return string |
|
| 71 | - */ |
|
| 72 | - public function __toString() |
|
| 73 | - { |
|
| 74 | - return $this->hash; |
|
| 75 | - } |
|
| 67 | + /** |
|
| 68 | + * Access to the hash for fast cache comparison. |
|
| 69 | + * |
|
| 70 | + * @return string |
|
| 71 | + */ |
|
| 72 | + public function __toString() |
|
| 73 | + { |
|
| 74 | + return $this->hash; |
|
| 75 | + } |
|
| 76 | 76 | } |
@@ -9,165 +9,165 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | class EntityBuilder |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * The mapper for the entity to build. |
|
| 14 | - * |
|
| 15 | - * @var \Analogue\ORM\System\Mapper |
|
| 16 | - */ |
|
| 17 | - protected $mapper; |
|
| 18 | - |
|
| 19 | - /** |
|
| 20 | - * The Entity Map for the entity to build. |
|
| 21 | - * |
|
| 22 | - * @var \Analogue\ORM\EntityMap |
|
| 23 | - */ |
|
| 24 | - protected $entityMap; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * Relations that are eager loaded on this query. |
|
| 28 | - * |
|
| 29 | - * @var array |
|
| 30 | - */ |
|
| 31 | - protected $eagerLoads; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * Relations that will be lazy loaded on this query. |
|
| 35 | - * |
|
| 36 | - * @var array |
|
| 37 | - */ |
|
| 38 | - protected $lazyLoads; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * @var array |
|
| 42 | - */ |
|
| 43 | - protected $casts; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * Entity Wrapper Factory. |
|
| 47 | - * |
|
| 48 | - * @var \Analogue\ORM\System\Wrappers\Factory |
|
| 49 | - */ |
|
| 50 | - protected $factory; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * EntityBuilder constructor. |
|
| 54 | - * |
|
| 55 | - * @param Mapper $mapper |
|
| 56 | - * @param array $eagerLoads |
|
| 57 | - */ |
|
| 58 | - public function __construct(Mapper $mapper, array $eagerLoads) |
|
| 59 | - { |
|
| 60 | - $this->mapper = $mapper; |
|
| 61 | - |
|
| 62 | - $this->entityMap = $mapper->getEntityMap(); |
|
| 63 | - |
|
| 64 | - $this->eagerLoads = $eagerLoads; |
|
| 65 | - |
|
| 66 | - $this->lazyLoads = $this->getRelationshipsToProxy(); |
|
| 67 | - |
|
| 68 | - $this->factory = new Factory(); |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * Convert an array of values into an entity. |
|
| 73 | - * |
|
| 74 | - * @param array $result |
|
| 75 | - * |
|
| 76 | - * @return array |
|
| 77 | - */ |
|
| 78 | - public function build(array $result) |
|
| 79 | - { |
|
| 80 | - $instance = $this->getWrapperInstance(); |
|
| 81 | - |
|
| 82 | - // Hydrate any embedded Value Object |
|
| 83 | - // |
|
| 84 | - // TODO Move this to the result builder instead, |
|
| 85 | - // as we'll handle this the same way as they were |
|
| 86 | - // eager loaded relationships. |
|
| 87 | - $this->hydrateValueObjects($result); |
|
| 88 | - |
|
| 89 | - $instance->setEntityAttributes($result); |
|
| 90 | - |
|
| 91 | - // Hydrate relationship attributes with lazyloading proxies |
|
| 92 | - if (count($this->lazyLoads) > 0) { |
|
| 93 | - $instance->setProxies($this->lazyLoads); |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - // Hydrate and return the instance |
|
| 97 | - $instance->hydrate(); |
|
| 98 | - $entity = $instance->getObject(); |
|
| 99 | - |
|
| 100 | - return $entity; |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * Get the correct wrapper prototype corresponding to the object type. |
|
| 105 | - * |
|
| 106 | - * @throws \Analogue\ORM\Exceptions\MappingException |
|
| 107 | - * |
|
| 108 | - * @return InternallyMappable |
|
| 109 | - */ |
|
| 110 | - protected function getWrapperInstance() |
|
| 111 | - { |
|
| 112 | - return $this->factory->make($this->mapper->newInstance()); |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - /** |
|
| 116 | - * Hydrate value object embedded in this entity. |
|
| 117 | - * |
|
| 118 | - * @param array $attributes |
|
| 119 | - * |
|
| 120 | - * @throws \Analogue\ORM\Exceptions\MappingException |
|
| 121 | - * |
|
| 122 | - * @return void |
|
| 123 | - */ |
|
| 124 | - protected function hydrateValueObjects(&$attributes) |
|
| 125 | - { |
|
| 126 | - foreach ($this->entityMap->getEmbeddables() as $localKey => $valueClass) { |
|
| 127 | - $this->hydrateValueObject($attributes, $localKey, $valueClass); |
|
| 128 | - } |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * Hydrate a single value object. |
|
| 133 | - * |
|
| 134 | - * @param array $attributes |
|
| 135 | - * @param string $localKey |
|
| 136 | - * @param string $valueClass |
|
| 137 | - * |
|
| 138 | - * @throws \Analogue\ORM\Exceptions\MappingException |
|
| 139 | - * |
|
| 140 | - * @return void |
|
| 141 | - */ |
|
| 142 | - protected function hydrateValueObject(&$attributes, $localKey, $valueClass) |
|
| 143 | - { |
|
| 144 | - $map = $this->mapper->getManager()->getValueMap($valueClass); |
|
| 145 | - |
|
| 146 | - $embeddedAttributes = $map->getAttributes(); |
|
| 147 | - |
|
| 148 | - $valueObject = $this->mapper->getManager()->getValueObjectInstance($valueClass); |
|
| 149 | - $voWrapper = $this->factory->make($valueObject); |
|
| 150 | - |
|
| 151 | - foreach ($embeddedAttributes as $key) { |
|
| 152 | - $prefix = snake_case(class_basename($valueClass)).'_'; |
|
| 153 | - |
|
| 154 | - $voWrapper->setEntityAttribute($key, $attributes[$prefix.$key]); |
|
| 155 | - |
|
| 156 | - unset($attributes[$prefix.$key]); |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - $attributes[$localKey] = $voWrapper->getObject(); |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * Deduce the relationships for which we will build lazy loading proxies. |
|
| 164 | - * |
|
| 165 | - * @return array |
|
| 166 | - */ |
|
| 167 | - protected function getRelationshipsToProxy() |
|
| 168 | - { |
|
| 169 | - $relations = $this->entityMap->getRelationships(); |
|
| 170 | - |
|
| 171 | - return array_diff($relations, $this->eagerLoads); |
|
| 172 | - } |
|
| 12 | + /** |
|
| 13 | + * The mapper for the entity to build. |
|
| 14 | + * |
|
| 15 | + * @var \Analogue\ORM\System\Mapper |
|
| 16 | + */ |
|
| 17 | + protected $mapper; |
|
| 18 | + |
|
| 19 | + /** |
|
| 20 | + * The Entity Map for the entity to build. |
|
| 21 | + * |
|
| 22 | + * @var \Analogue\ORM\EntityMap |
|
| 23 | + */ |
|
| 24 | + protected $entityMap; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * Relations that are eager loaded on this query. |
|
| 28 | + * |
|
| 29 | + * @var array |
|
| 30 | + */ |
|
| 31 | + protected $eagerLoads; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * Relations that will be lazy loaded on this query. |
|
| 35 | + * |
|
| 36 | + * @var array |
|
| 37 | + */ |
|
| 38 | + protected $lazyLoads; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * @var array |
|
| 42 | + */ |
|
| 43 | + protected $casts; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * Entity Wrapper Factory. |
|
| 47 | + * |
|
| 48 | + * @var \Analogue\ORM\System\Wrappers\Factory |
|
| 49 | + */ |
|
| 50 | + protected $factory; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * EntityBuilder constructor. |
|
| 54 | + * |
|
| 55 | + * @param Mapper $mapper |
|
| 56 | + * @param array $eagerLoads |
|
| 57 | + */ |
|
| 58 | + public function __construct(Mapper $mapper, array $eagerLoads) |
|
| 59 | + { |
|
| 60 | + $this->mapper = $mapper; |
|
| 61 | + |
|
| 62 | + $this->entityMap = $mapper->getEntityMap(); |
|
| 63 | + |
|
| 64 | + $this->eagerLoads = $eagerLoads; |
|
| 65 | + |
|
| 66 | + $this->lazyLoads = $this->getRelationshipsToProxy(); |
|
| 67 | + |
|
| 68 | + $this->factory = new Factory(); |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * Convert an array of values into an entity. |
|
| 73 | + * |
|
| 74 | + * @param array $result |
|
| 75 | + * |
|
| 76 | + * @return array |
|
| 77 | + */ |
|
| 78 | + public function build(array $result) |
|
| 79 | + { |
|
| 80 | + $instance = $this->getWrapperInstance(); |
|
| 81 | + |
|
| 82 | + // Hydrate any embedded Value Object |
|
| 83 | + // |
|
| 84 | + // TODO Move this to the result builder instead, |
|
| 85 | + // as we'll handle this the same way as they were |
|
| 86 | + // eager loaded relationships. |
|
| 87 | + $this->hydrateValueObjects($result); |
|
| 88 | + |
|
| 89 | + $instance->setEntityAttributes($result); |
|
| 90 | + |
|
| 91 | + // Hydrate relationship attributes with lazyloading proxies |
|
| 92 | + if (count($this->lazyLoads) > 0) { |
|
| 93 | + $instance->setProxies($this->lazyLoads); |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + // Hydrate and return the instance |
|
| 97 | + $instance->hydrate(); |
|
| 98 | + $entity = $instance->getObject(); |
|
| 99 | + |
|
| 100 | + return $entity; |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * Get the correct wrapper prototype corresponding to the object type. |
|
| 105 | + * |
|
| 106 | + * @throws \Analogue\ORM\Exceptions\MappingException |
|
| 107 | + * |
|
| 108 | + * @return InternallyMappable |
|
| 109 | + */ |
|
| 110 | + protected function getWrapperInstance() |
|
| 111 | + { |
|
| 112 | + return $this->factory->make($this->mapper->newInstance()); |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + /** |
|
| 116 | + * Hydrate value object embedded in this entity. |
|
| 117 | + * |
|
| 118 | + * @param array $attributes |
|
| 119 | + * |
|
| 120 | + * @throws \Analogue\ORM\Exceptions\MappingException |
|
| 121 | + * |
|
| 122 | + * @return void |
|
| 123 | + */ |
|
| 124 | + protected function hydrateValueObjects(&$attributes) |
|
| 125 | + { |
|
| 126 | + foreach ($this->entityMap->getEmbeddables() as $localKey => $valueClass) { |
|
| 127 | + $this->hydrateValueObject($attributes, $localKey, $valueClass); |
|
| 128 | + } |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * Hydrate a single value object. |
|
| 133 | + * |
|
| 134 | + * @param array $attributes |
|
| 135 | + * @param string $localKey |
|
| 136 | + * @param string $valueClass |
|
| 137 | + * |
|
| 138 | + * @throws \Analogue\ORM\Exceptions\MappingException |
|
| 139 | + * |
|
| 140 | + * @return void |
|
| 141 | + */ |
|
| 142 | + protected function hydrateValueObject(&$attributes, $localKey, $valueClass) |
|
| 143 | + { |
|
| 144 | + $map = $this->mapper->getManager()->getValueMap($valueClass); |
|
| 145 | + |
|
| 146 | + $embeddedAttributes = $map->getAttributes(); |
|
| 147 | + |
|
| 148 | + $valueObject = $this->mapper->getManager()->getValueObjectInstance($valueClass); |
|
| 149 | + $voWrapper = $this->factory->make($valueObject); |
|
| 150 | + |
|
| 151 | + foreach ($embeddedAttributes as $key) { |
|
| 152 | + $prefix = snake_case(class_basename($valueClass)).'_'; |
|
| 153 | + |
|
| 154 | + $voWrapper->setEntityAttribute($key, $attributes[$prefix.$key]); |
|
| 155 | + |
|
| 156 | + unset($attributes[$prefix.$key]); |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + $attributes[$localKey] = $voWrapper->getObject(); |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * Deduce the relationships for which we will build lazy loading proxies. |
|
| 164 | + * |
|
| 165 | + * @return array |
|
| 166 | + */ |
|
| 167 | + protected function getRelationshipsToProxy() |
|
| 168 | + { |
|
| 169 | + $relations = $this->entityMap->getRelationships(); |
|
| 170 | + |
|
| 171 | + return array_diff($relations, $this->eagerLoads); |
|
| 172 | + } |
|
| 173 | 173 | } |
@@ -10,208 +10,208 @@ |
||
| 10 | 10 | */ |
| 11 | 11 | abstract class Wrapper implements InternallyMappable |
| 12 | 12 | { |
| 13 | - /** |
|
| 14 | - * Original Entity Object. |
|
| 15 | - * |
|
| 16 | - * @var mixed |
|
| 17 | - */ |
|
| 18 | - protected $entity; |
|
| 19 | - |
|
| 20 | - /** |
|
| 21 | - * Corresponding EntityMap. |
|
| 22 | - * |
|
| 23 | - * @var \Analogue\ORM\EntityMap |
|
| 24 | - */ |
|
| 25 | - protected $entityMap; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * @var \Analogue\ORM\System\Proxirs\ProxyFactory |
|
| 29 | - */ |
|
| 30 | - protected $proxyFactory; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * Wrapper constructor. |
|
| 34 | - * |
|
| 35 | - * @param $entity |
|
| 36 | - * @param $entityMap |
|
| 37 | - */ |
|
| 38 | - public function __construct($entity, $entityMap) |
|
| 39 | - { |
|
| 40 | - $this->entity = $entity; |
|
| 41 | - $this->entityMap = $entityMap; |
|
| 42 | - $this->proxyFactory = new ProxyFactory(); |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * Return the wrapped entity class. |
|
| 47 | - * |
|
| 48 | - * @return mixed |
|
| 49 | - */ |
|
| 50 | - public function getEntityClass() |
|
| 51 | - { |
|
| 52 | - return get_class($this->entity); |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * Return the entity's primary key valuye. |
|
| 57 | - * |
|
| 58 | - * @return string |
|
| 59 | - */ |
|
| 60 | - public function getEntityKey() |
|
| 61 | - { |
|
| 62 | - return $this->getEntityAttribute($this->entityMap->getKeyName()); |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * Return the Entity class/primary key couple, |
|
| 67 | - * which is used for internall operations. |
|
| 68 | - * |
|
| 69 | - * @return string |
|
| 70 | - */ |
|
| 71 | - public function getEntityHash() |
|
| 72 | - { |
|
| 73 | - return $this->getEntityClass().'.'.$this->getEntityKey(); |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * Returns the wrapped entity. |
|
| 78 | - * |
|
| 79 | - * @return mixed |
|
| 80 | - */ |
|
| 81 | - public function getObject() |
|
| 82 | - { |
|
| 83 | - return $this->entity; |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * Returns the wrapped entity's map. |
|
| 88 | - * |
|
| 89 | - * @return mixed |
|
| 90 | - */ |
|
| 91 | - public function getMap() |
|
| 92 | - { |
|
| 93 | - return $this->entityMap; |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * Set the lazyloading proxies on the wrapped entity objet. |
|
| 98 | - * |
|
| 99 | - * @param array $relations list of relations to be lazy loaded |
|
| 100 | - * |
|
| 101 | - * @return void |
|
| 102 | - */ |
|
| 103 | - public function setProxies(array $relations = null) |
|
| 104 | - { |
|
| 105 | - $attributes = $this->getEntityAttributes(); |
|
| 106 | - $proxies = []; |
|
| 107 | - |
|
| 108 | - if (is_null($relations)) { |
|
| 109 | - $relations = $this->getRelationsToProxy(); |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - foreach ($relations as $relation) { |
|
| 113 | - // We first look if we need to build the proxy on the relationship. |
|
| 114 | - // If the key is handled locally and we know it not to be set, |
|
| 115 | - // we'll set the relationship to null |
|
| 116 | - if (!$this->relationNeedsProxy($relation, $attributes)) { |
|
| 117 | - $proxies[$relation] = null; |
|
| 118 | - } else { |
|
| 119 | - $targetClass = $this->entityMap->getTargettedClass($relation); |
|
| 120 | - $proxies[$relation] = $this->proxyFactory->make($this->getObject(), $relation, $targetClass); |
|
| 121 | - } |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - foreach ($proxies as $key => $value) { |
|
| 125 | - $this->setEntityAttribute($key, $value); |
|
| 126 | - } |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * Determine which relations we have to build proxy for, by parsing |
|
| 131 | - * attributes and finding methods that aren't set. |
|
| 132 | - * |
|
| 133 | - * @return array |
|
| 134 | - */ |
|
| 135 | - protected function getRelationsToProxy() |
|
| 136 | - { |
|
| 137 | - $proxies = []; |
|
| 138 | - $attributes = $this->getEntityAttributes(); |
|
| 139 | - |
|
| 140 | - foreach ($this->entityMap->getRelationships() as $relation) { |
|
| 141 | - if (!array_key_exists($relation, $attributes)) { |
|
| 142 | - $proxies[] = $relation; |
|
| 143 | - } |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - return $proxies; |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - /** |
|
| 150 | - * Determine if the relation needs a proxy or not. |
|
| 151 | - * |
|
| 152 | - * @param string $relation |
|
| 153 | - * @param array $attributes |
|
| 154 | - * |
|
| 155 | - * @return bool |
|
| 156 | - */ |
|
| 157 | - protected function relationNeedsProxy($relation, $attributes) |
|
| 158 | - { |
|
| 159 | - if (in_array($relation, $this->entityMap->getRelationshipsWithoutProxy())) { |
|
| 160 | - return false; |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - $localKey = $this->entityMap->getLocalKeys($relation); |
|
| 164 | - |
|
| 165 | - if (is_null($localKey)) { |
|
| 166 | - return true; |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - if (is_array($localKey)) { |
|
| 170 | - $localKey = $localKey['id']; |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - if (!isset($attributes[$localKey])) { |
|
| 174 | - return false; |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - if (is_null($attributes[$localKey])) { |
|
| 178 | - return false; |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - return true; |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - /** |
|
| 185 | - * @param string $key |
|
| 186 | - * @param string $value |
|
| 187 | - * |
|
| 188 | - * @return mixed |
|
| 189 | - */ |
|
| 190 | - abstract public function setEntityAttribute($key, $value); |
|
| 191 | - |
|
| 192 | - /** |
|
| 193 | - * @param string $key |
|
| 194 | - * |
|
| 195 | - * @return mixed |
|
| 196 | - */ |
|
| 197 | - abstract public function getEntityAttribute($key); |
|
| 198 | - |
|
| 199 | - /** |
|
| 200 | - * @param array $attributes |
|
| 201 | - * |
|
| 202 | - * @return mixed |
|
| 203 | - */ |
|
| 204 | - abstract public function setEntityAttributes(array $attributes); |
|
| 205 | - |
|
| 206 | - /** |
|
| 207 | - * @return mixed |
|
| 208 | - */ |
|
| 209 | - abstract public function getEntityAttributes(); |
|
| 210 | - |
|
| 211 | - /** |
|
| 212 | - * @param string $key |
|
| 213 | - * |
|
| 214 | - * @return mixed |
|
| 215 | - */ |
|
| 216 | - abstract public function hasAttribute($key); |
|
| 13 | + /** |
|
| 14 | + * Original Entity Object. |
|
| 15 | + * |
|
| 16 | + * @var mixed |
|
| 17 | + */ |
|
| 18 | + protected $entity; |
|
| 19 | + |
|
| 20 | + /** |
|
| 21 | + * Corresponding EntityMap. |
|
| 22 | + * |
|
| 23 | + * @var \Analogue\ORM\EntityMap |
|
| 24 | + */ |
|
| 25 | + protected $entityMap; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * @var \Analogue\ORM\System\Proxirs\ProxyFactory |
|
| 29 | + */ |
|
| 30 | + protected $proxyFactory; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * Wrapper constructor. |
|
| 34 | + * |
|
| 35 | + * @param $entity |
|
| 36 | + * @param $entityMap |
|
| 37 | + */ |
|
| 38 | + public function __construct($entity, $entityMap) |
|
| 39 | + { |
|
| 40 | + $this->entity = $entity; |
|
| 41 | + $this->entityMap = $entityMap; |
|
| 42 | + $this->proxyFactory = new ProxyFactory(); |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * Return the wrapped entity class. |
|
| 47 | + * |
|
| 48 | + * @return mixed |
|
| 49 | + */ |
|
| 50 | + public function getEntityClass() |
|
| 51 | + { |
|
| 52 | + return get_class($this->entity); |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * Return the entity's primary key valuye. |
|
| 57 | + * |
|
| 58 | + * @return string |
|
| 59 | + */ |
|
| 60 | + public function getEntityKey() |
|
| 61 | + { |
|
| 62 | + return $this->getEntityAttribute($this->entityMap->getKeyName()); |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * Return the Entity class/primary key couple, |
|
| 67 | + * which is used for internall operations. |
|
| 68 | + * |
|
| 69 | + * @return string |
|
| 70 | + */ |
|
| 71 | + public function getEntityHash() |
|
| 72 | + { |
|
| 73 | + return $this->getEntityClass().'.'.$this->getEntityKey(); |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * Returns the wrapped entity. |
|
| 78 | + * |
|
| 79 | + * @return mixed |
|
| 80 | + */ |
|
| 81 | + public function getObject() |
|
| 82 | + { |
|
| 83 | + return $this->entity; |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * Returns the wrapped entity's map. |
|
| 88 | + * |
|
| 89 | + * @return mixed |
|
| 90 | + */ |
|
| 91 | + public function getMap() |
|
| 92 | + { |
|
| 93 | + return $this->entityMap; |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * Set the lazyloading proxies on the wrapped entity objet. |
|
| 98 | + * |
|
| 99 | + * @param array $relations list of relations to be lazy loaded |
|
| 100 | + * |
|
| 101 | + * @return void |
|
| 102 | + */ |
|
| 103 | + public function setProxies(array $relations = null) |
|
| 104 | + { |
|
| 105 | + $attributes = $this->getEntityAttributes(); |
|
| 106 | + $proxies = []; |
|
| 107 | + |
|
| 108 | + if (is_null($relations)) { |
|
| 109 | + $relations = $this->getRelationsToProxy(); |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + foreach ($relations as $relation) { |
|
| 113 | + // We first look if we need to build the proxy on the relationship. |
|
| 114 | + // If the key is handled locally and we know it not to be set, |
|
| 115 | + // we'll set the relationship to null |
|
| 116 | + if (!$this->relationNeedsProxy($relation, $attributes)) { |
|
| 117 | + $proxies[$relation] = null; |
|
| 118 | + } else { |
|
| 119 | + $targetClass = $this->entityMap->getTargettedClass($relation); |
|
| 120 | + $proxies[$relation] = $this->proxyFactory->make($this->getObject(), $relation, $targetClass); |
|
| 121 | + } |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + foreach ($proxies as $key => $value) { |
|
| 125 | + $this->setEntityAttribute($key, $value); |
|
| 126 | + } |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * Determine which relations we have to build proxy for, by parsing |
|
| 131 | + * attributes and finding methods that aren't set. |
|
| 132 | + * |
|
| 133 | + * @return array |
|
| 134 | + */ |
|
| 135 | + protected function getRelationsToProxy() |
|
| 136 | + { |
|
| 137 | + $proxies = []; |
|
| 138 | + $attributes = $this->getEntityAttributes(); |
|
| 139 | + |
|
| 140 | + foreach ($this->entityMap->getRelationships() as $relation) { |
|
| 141 | + if (!array_key_exists($relation, $attributes)) { |
|
| 142 | + $proxies[] = $relation; |
|
| 143 | + } |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + return $proxies; |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + /** |
|
| 150 | + * Determine if the relation needs a proxy or not. |
|
| 151 | + * |
|
| 152 | + * @param string $relation |
|
| 153 | + * @param array $attributes |
|
| 154 | + * |
|
| 155 | + * @return bool |
|
| 156 | + */ |
|
| 157 | + protected function relationNeedsProxy($relation, $attributes) |
|
| 158 | + { |
|
| 159 | + if (in_array($relation, $this->entityMap->getRelationshipsWithoutProxy())) { |
|
| 160 | + return false; |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + $localKey = $this->entityMap->getLocalKeys($relation); |
|
| 164 | + |
|
| 165 | + if (is_null($localKey)) { |
|
| 166 | + return true; |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + if (is_array($localKey)) { |
|
| 170 | + $localKey = $localKey['id']; |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + if (!isset($attributes[$localKey])) { |
|
| 174 | + return false; |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + if (is_null($attributes[$localKey])) { |
|
| 178 | + return false; |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + return true; |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + /** |
|
| 185 | + * @param string $key |
|
| 186 | + * @param string $value |
|
| 187 | + * |
|
| 188 | + * @return mixed |
|
| 189 | + */ |
|
| 190 | + abstract public function setEntityAttribute($key, $value); |
|
| 191 | + |
|
| 192 | + /** |
|
| 193 | + * @param string $key |
|
| 194 | + * |
|
| 195 | + * @return mixed |
|
| 196 | + */ |
|
| 197 | + abstract public function getEntityAttribute($key); |
|
| 198 | + |
|
| 199 | + /** |
|
| 200 | + * @param array $attributes |
|
| 201 | + * |
|
| 202 | + * @return mixed |
|
| 203 | + */ |
|
| 204 | + abstract public function setEntityAttributes(array $attributes); |
|
| 205 | + |
|
| 206 | + /** |
|
| 207 | + * @return mixed |
|
| 208 | + */ |
|
| 209 | + abstract public function getEntityAttributes(); |
|
| 210 | + |
|
| 211 | + /** |
|
| 212 | + * @param string $key |
|
| 213 | + * |
|
| 214 | + * @return mixed |
|
| 215 | + */ |
|
| 216 | + abstract public function hasAttribute($key); |
|
| 217 | 217 | } |
@@ -45,7 +45,7 @@ discard block |
||
| 45 | 45 | /** |
| 46 | 46 | * Return the wrapped entity class. |
| 47 | 47 | * |
| 48 | - * @return mixed |
|
| 48 | + * @return string |
|
| 49 | 49 | */ |
| 50 | 50 | public function getEntityClass() |
| 51 | 51 | { |
@@ -86,7 +86,7 @@ discard block |
||
| 86 | 86 | /** |
| 87 | 87 | * Returns the wrapped entity's map. |
| 88 | 88 | * |
| 89 | - * @return mixed |
|
| 89 | + * @return \Analogue\ORM\EntityMap |
|
| 90 | 90 | */ |
| 91 | 91 | public function getMap() |
| 92 | 92 | { |
@@ -7,33 +7,33 @@ |
||
| 7 | 7 | |
| 8 | 8 | class Factory |
| 9 | 9 | { |
| 10 | - /** |
|
| 11 | - * Build the wrapper corresponding to the object's type. |
|
| 12 | - * |
|
| 13 | - * @param mixed $object |
|
| 14 | - * |
|
| 15 | - * @throws \Analogue\ORM\Exceptions\MappingException |
|
| 16 | - * |
|
| 17 | - * @return Wrapper |
|
| 18 | - */ |
|
| 19 | - public function make($object) |
|
| 20 | - { |
|
| 21 | - $manager = Manager::getInstance(); |
|
| 10 | + /** |
|
| 11 | + * Build the wrapper corresponding to the object's type. |
|
| 12 | + * |
|
| 13 | + * @param mixed $object |
|
| 14 | + * |
|
| 15 | + * @throws \Analogue\ORM\Exceptions\MappingException |
|
| 16 | + * |
|
| 17 | + * @return Wrapper |
|
| 18 | + */ |
|
| 19 | + public function make($object) |
|
| 20 | + { |
|
| 21 | + $manager = Manager::getInstance(); |
|
| 22 | 22 | |
| 23 | - // Instantiate hydrator. We'll need to optimize this and allow pre-generation |
|
| 24 | - // of these hydrator, and get it, ideally, from the entityMap or the Mapper class, |
|
| 25 | - // so it's only instantiated once |
|
| 26 | - $config = new Configuration(get_class($object)); |
|
| 27 | - $hydratorClass = $config->createFactory()->getHydratorClass(); |
|
| 28 | - $hydrator = new $hydratorClass(); |
|
| 23 | + // Instantiate hydrator. We'll need to optimize this and allow pre-generation |
|
| 24 | + // of these hydrator, and get it, ideally, from the entityMap or the Mapper class, |
|
| 25 | + // so it's only instantiated once |
|
| 26 | + $config = new Configuration(get_class($object)); |
|
| 27 | + $hydratorClass = $config->createFactory()->getHydratorClass(); |
|
| 28 | + $hydrator = new $hydratorClass(); |
|
| 29 | 29 | |
| 30 | - if ($manager->isValueObject($object)) { |
|
| 31 | - $entityMap = $manager->getValueMap($object); |
|
| 32 | - } else { |
|
| 33 | - $entityMap = $manager->mapper($object)->getEntityMap(); |
|
| 34 | - } |
|
| 30 | + if ($manager->isValueObject($object)) { |
|
| 31 | + $entityMap = $manager->getValueMap($object); |
|
| 32 | + } else { |
|
| 33 | + $entityMap = $manager->mapper($object)->getEntityMap(); |
|
| 34 | + } |
|
| 35 | 35 | |
| 36 | - // Build Wrapper |
|
| 37 | - return new ObjectWrapper($object, $entityMap, $hydrator); |
|
| 38 | - } |
|
| 36 | + // Build Wrapper |
|
| 37 | + return new ObjectWrapper($object, $entityMap, $hydrator); |
|
| 38 | + } |
|
| 39 | 39 | } |
@@ -13,78 +13,78 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class MapperFactory |
| 15 | 15 | { |
| 16 | - /** |
|
| 17 | - * Manager instance. |
|
| 18 | - * |
|
| 19 | - * @var \Analogue\ORM\System\Manager |
|
| 20 | - */ |
|
| 21 | - protected $manager; |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * DriverManager instance. |
|
| 25 | - * |
|
| 26 | - * @var \Analogue\ORM\Drivers\Manager |
|
| 27 | - */ |
|
| 28 | - protected $drivers; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * Event dispatcher instance. |
|
| 32 | - * |
|
| 33 | - * @var \Illuminate\Contracts\Events\Dispatcher |
|
| 34 | - */ |
|
| 35 | - protected $dispatcher; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * MapperFactory constructor. |
|
| 39 | - * |
|
| 40 | - * @param DriverManager $drivers |
|
| 41 | - * @param Dispatcher $dispatcher |
|
| 42 | - * @param Manager $manager |
|
| 43 | - */ |
|
| 44 | - public function __construct(DriverManager $drivers, Dispatcher $dispatcher, Manager $manager) |
|
| 45 | - { |
|
| 46 | - $this->drivers = $drivers; |
|
| 47 | - |
|
| 48 | - $this->dispatcher = $dispatcher; |
|
| 49 | - |
|
| 50 | - $this->manager = $manager; |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * Return a new Mapper instance. |
|
| 55 | - * |
|
| 56 | - * @param string $entityClass |
|
| 57 | - * @param EntityMap $entityMap |
|
| 58 | - * |
|
| 59 | - * @return Mapper |
|
| 60 | - */ |
|
| 61 | - public function make($entityClass, EntityMap $entityMap) |
|
| 62 | - { |
|
| 63 | - $driver = $entityMap->getDriver(); |
|
| 64 | - |
|
| 65 | - $connection = $entityMap->getConnection(); |
|
| 66 | - |
|
| 67 | - $adapter = $this->drivers->getAdapter($driver, $connection); |
|
| 68 | - |
|
| 69 | - $entityMap->setDateFormat($adapter->getDateFormat()); |
|
| 70 | - |
|
| 71 | - $mapper = new Mapper($entityMap, $adapter, $this->dispatcher, $this->manager); |
|
| 72 | - |
|
| 73 | - // Fire Initializing Event |
|
| 74 | - $mapper->fireEvent('initializing', $mapper); |
|
| 75 | - |
|
| 76 | - // Proceed necessary parsing on the EntityMap object |
|
| 77 | - $entityMap->initialize(); |
|
| 78 | - |
|
| 79 | - // Apply Inheritance scope, if necessary |
|
| 80 | - if ($entityMap->getInheritanceType() == 'single_table') { |
|
| 81 | - $scope = new SingleTableInheritanceScope($entityMap); |
|
| 82 | - $mapper->addGlobalScope($scope); |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - // Fire Initialized Event |
|
| 86 | - $mapper->fireEvent('initialized', $mapper); |
|
| 87 | - |
|
| 88 | - return $mapper; |
|
| 89 | - } |
|
| 16 | + /** |
|
| 17 | + * Manager instance. |
|
| 18 | + * |
|
| 19 | + * @var \Analogue\ORM\System\Manager |
|
| 20 | + */ |
|
| 21 | + protected $manager; |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * DriverManager instance. |
|
| 25 | + * |
|
| 26 | + * @var \Analogue\ORM\Drivers\Manager |
|
| 27 | + */ |
|
| 28 | + protected $drivers; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * Event dispatcher instance. |
|
| 32 | + * |
|
| 33 | + * @var \Illuminate\Contracts\Events\Dispatcher |
|
| 34 | + */ |
|
| 35 | + protected $dispatcher; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * MapperFactory constructor. |
|
| 39 | + * |
|
| 40 | + * @param DriverManager $drivers |
|
| 41 | + * @param Dispatcher $dispatcher |
|
| 42 | + * @param Manager $manager |
|
| 43 | + */ |
|
| 44 | + public function __construct(DriverManager $drivers, Dispatcher $dispatcher, Manager $manager) |
|
| 45 | + { |
|
| 46 | + $this->drivers = $drivers; |
|
| 47 | + |
|
| 48 | + $this->dispatcher = $dispatcher; |
|
| 49 | + |
|
| 50 | + $this->manager = $manager; |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * Return a new Mapper instance. |
|
| 55 | + * |
|
| 56 | + * @param string $entityClass |
|
| 57 | + * @param EntityMap $entityMap |
|
| 58 | + * |
|
| 59 | + * @return Mapper |
|
| 60 | + */ |
|
| 61 | + public function make($entityClass, EntityMap $entityMap) |
|
| 62 | + { |
|
| 63 | + $driver = $entityMap->getDriver(); |
|
| 64 | + |
|
| 65 | + $connection = $entityMap->getConnection(); |
|
| 66 | + |
|
| 67 | + $adapter = $this->drivers->getAdapter($driver, $connection); |
|
| 68 | + |
|
| 69 | + $entityMap->setDateFormat($adapter->getDateFormat()); |
|
| 70 | + |
|
| 71 | + $mapper = new Mapper($entityMap, $adapter, $this->dispatcher, $this->manager); |
|
| 72 | + |
|
| 73 | + // Fire Initializing Event |
|
| 74 | + $mapper->fireEvent('initializing', $mapper); |
|
| 75 | + |
|
| 76 | + // Proceed necessary parsing on the EntityMap object |
|
| 77 | + $entityMap->initialize(); |
|
| 78 | + |
|
| 79 | + // Apply Inheritance scope, if necessary |
|
| 80 | + if ($entityMap->getInheritanceType() == 'single_table') { |
|
| 81 | + $scope = new SingleTableInheritanceScope($entityMap); |
|
| 82 | + $mapper->addGlobalScope($scope); |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + // Fire Initialized Event |
|
| 86 | + $mapper->fireEvent('initialized', $mapper); |
|
| 87 | + |
|
| 88 | + return $mapper; |
|
| 89 | + } |
|
| 90 | 90 | } |