@@ -6,5 +6,5 @@ |
||
| 6 | 6 | |
| 7 | 7 | class EntityMapNotFoundException extends RuntimeException |
| 8 | 8 | { |
| 9 | - // |
|
| 9 | + // |
|
| 10 | 10 | } |
@@ -7,28 +7,28 @@ |
||
| 7 | 7 | */ |
| 8 | 8 | trait MagicSetters |
| 9 | 9 | { |
| 10 | - /** |
|
| 11 | - * Dynamically set attributes on the entity. |
|
| 12 | - * |
|
| 13 | - * @param string $key |
|
| 14 | - * @param mixed $value |
|
| 15 | - * |
|
| 16 | - * @return void |
|
| 17 | - */ |
|
| 18 | - public function __set($key, $value) |
|
| 19 | - { |
|
| 20 | - $this->attributes[$key] = $value; |
|
| 21 | - } |
|
| 10 | + /** |
|
| 11 | + * Dynamically set attributes on the entity. |
|
| 12 | + * |
|
| 13 | + * @param string $key |
|
| 14 | + * @param mixed $value |
|
| 15 | + * |
|
| 16 | + * @return void |
|
| 17 | + */ |
|
| 18 | + public function __set($key, $value) |
|
| 19 | + { |
|
| 20 | + $this->attributes[$key] = $value; |
|
| 21 | + } |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * Unset an attribute on the entity. |
|
| 25 | - * |
|
| 26 | - * @param string $key |
|
| 27 | - * |
|
| 28 | - * @return void |
|
| 29 | - */ |
|
| 30 | - public function __unset($key) |
|
| 31 | - { |
|
| 32 | - unset($this->attributes[$key]); |
|
| 33 | - } |
|
| 23 | + /** |
|
| 24 | + * Unset an attribute on the entity. |
|
| 25 | + * |
|
| 26 | + * @param string $key |
|
| 27 | + * |
|
| 28 | + * @return void |
|
| 29 | + */ |
|
| 30 | + public function __unset($key) |
|
| 31 | + { |
|
| 32 | + unset($this->attributes[$key]); |
|
| 33 | + } |
|
| 34 | 34 | } |
@@ -4,54 +4,54 @@ |
||
| 4 | 4 | |
| 5 | 5 | trait MagicGetters |
| 6 | 6 | { |
| 7 | - /** |
|
| 8 | - * Contains the entity's attributes. |
|
| 9 | - * |
|
| 10 | - * @var array |
|
| 11 | - */ |
|
| 12 | - protected $attributes = []; |
|
| 7 | + /** |
|
| 8 | + * Contains the entity's attributes. |
|
| 9 | + * |
|
| 10 | + * @var array |
|
| 11 | + */ |
|
| 12 | + protected $attributes = []; |
|
| 13 | 13 | |
| 14 | - /** |
|
| 15 | - * Dynamically retrieve attributes on the entity. |
|
| 16 | - * |
|
| 17 | - * @param string $key |
|
| 18 | - * |
|
| 19 | - * @return mixed |
|
| 20 | - */ |
|
| 21 | - public function __get($key) |
|
| 22 | - { |
|
| 23 | - // When using mixed mapping, we will check |
|
| 24 | - // for a class property corresponding to |
|
| 25 | - // the attribute's key first. |
|
| 26 | - // |
|
| 27 | - // Note : this may raise issues as we may grant |
|
| 28 | - // access to unwanted properties, like class dependencies. |
|
| 29 | - // |
|
| 30 | - // -> Solution would be to access the entityMap's $attributes, but we |
|
| 31 | - // have to do this in a very efficient way. |
|
| 32 | - // |
|
| 33 | - // Manager::getEntityMap(get_class($this))->hasProperty() |
|
| 34 | - // |
|
| 35 | - // We could do the casting to array / json the same way, and it would |
|
| 14 | + /** |
|
| 15 | + * Dynamically retrieve attributes on the entity. |
|
| 16 | + * |
|
| 17 | + * @param string $key |
|
| 18 | + * |
|
| 19 | + * @return mixed |
|
| 20 | + */ |
|
| 21 | + public function __get($key) |
|
| 22 | + { |
|
| 23 | + // When using mixed mapping, we will check |
|
| 24 | + // for a class property corresponding to |
|
| 25 | + // the attribute's key first. |
|
| 26 | + // |
|
| 27 | + // Note : this may raise issues as we may grant |
|
| 28 | + // access to unwanted properties, like class dependencies. |
|
| 29 | + // |
|
| 30 | + // -> Solution would be to access the entityMap's $attributes, but we |
|
| 31 | + // have to do this in a very efficient way. |
|
| 32 | + // |
|
| 33 | + // Manager::getEntityMap(get_class($this))->hasProperty() |
|
| 34 | + // |
|
| 35 | + // We could do the casting to array / json the same way, and it would |
|
| 36 | 36 | |
| 37 | - if (property_exists($this, $key)) { |
|
| 38 | - return $this->$key; |
|
| 39 | - } |
|
| 37 | + if (property_exists($this, $key)) { |
|
| 38 | + return $this->$key; |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - if (array_key_exists($key, $this->attributes)) { |
|
| 42 | - return $this->attributes[$key]; |
|
| 43 | - } |
|
| 44 | - } |
|
| 41 | + if (array_key_exists($key, $this->attributes)) { |
|
| 42 | + return $this->attributes[$key]; |
|
| 43 | + } |
|
| 44 | + } |
|
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * Determine if an attribute exists on the entity. |
|
| 48 | - * |
|
| 49 | - * @param string $key |
|
| 50 | - * |
|
| 51 | - * @return bool |
|
| 52 | - */ |
|
| 53 | - public function __isset($key) |
|
| 54 | - { |
|
| 55 | - return array_key_exists($key, $this->attributes) || property_exists($this, $key); |
|
| 56 | - } |
|
| 46 | + /** |
|
| 47 | + * Determine if an attribute exists on the entity. |
|
| 48 | + * |
|
| 49 | + * @param string $key |
|
| 50 | + * |
|
| 51 | + * @return bool |
|
| 52 | + */ |
|
| 53 | + public function __isset($key) |
|
| 54 | + { |
|
| 55 | + return array_key_exists($key, $this->attributes) || property_exists($this, $key); |
|
| 56 | + } |
|
| 57 | 57 | } |
@@ -6,36 +6,36 @@ |
||
| 6 | 6 | |
| 7 | 7 | class Restore extends Command |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * @throws \InvalidArgumentException |
|
| 11 | - * |
|
| 12 | - * @return false|mixed |
|
| 13 | - */ |
|
| 14 | - public function execute() |
|
| 15 | - { |
|
| 16 | - $aggregate = $this->aggregate; |
|
| 17 | - $entity = $aggregate->getEntityObject(); |
|
| 18 | - $mapper = $aggregate->getMapper(); |
|
| 19 | - $entityMap = $mapper->getEntityMap(); |
|
| 9 | + /** |
|
| 10 | + * @throws \InvalidArgumentException |
|
| 11 | + * |
|
| 12 | + * @return false|mixed |
|
| 13 | + */ |
|
| 14 | + public function execute() |
|
| 15 | + { |
|
| 16 | + $aggregate = $this->aggregate; |
|
| 17 | + $entity = $aggregate->getEntityObject(); |
|
| 18 | + $mapper = $aggregate->getMapper(); |
|
| 19 | + $entityMap = $mapper->getEntityMap(); |
|
| 20 | 20 | |
| 21 | - if ($mapper->fireEvent('restoring', $entity) === false) { |
|
| 22 | - return false; |
|
| 23 | - } |
|
| 21 | + if ($mapper->fireEvent('restoring', $entity) === false) { |
|
| 22 | + return false; |
|
| 23 | + } |
|
| 24 | 24 | |
| 25 | - $keyName = $entityMap->getKeyName(); |
|
| 25 | + $keyName = $entityMap->getKeyName(); |
|
| 26 | 26 | |
| 27 | - $query = $this->query->where($keyName, '=', $aggregate->getEntityAttribute($keyName)); |
|
| 27 | + $query = $this->query->where($keyName, '=', $aggregate->getEntityAttribute($keyName)); |
|
| 28 | 28 | |
| 29 | - $deletedAtColumn = $entityMap->getQualifiedDeletedAtColumn(); |
|
| 29 | + $deletedAtColumn = $entityMap->getQualifiedDeletedAtColumn(); |
|
| 30 | 30 | |
| 31 | - $query->update([$deletedAtColumn => null]); |
|
| 31 | + $query->update([$deletedAtColumn => null]); |
|
| 32 | 32 | |
| 33 | - $aggregate->setEntityAttribute($deletedAtColumn, null); |
|
| 33 | + $aggregate->setEntityAttribute($deletedAtColumn, null); |
|
| 34 | 34 | |
| 35 | - $mapper->fireEvent('restored', $entity, false); |
|
| 35 | + $mapper->fireEvent('restored', $entity, false); |
|
| 36 | 36 | |
| 37 | - $mapper->getEntityCache()->refresh($aggregate); |
|
| 37 | + $mapper->getEntityCache()->refresh($aggregate); |
|
| 38 | 38 | |
| 39 | - return $entity; |
|
| 40 | - } |
|
| 39 | + return $entity; |
|
| 40 | + } |
|
| 41 | 41 | } |
@@ -6,36 +6,36 @@ |
||
| 6 | 6 | |
| 7 | 7 | class EntityNotFoundException extends RuntimeException |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * Name of the affected Entity Map. |
|
| 11 | - * |
|
| 12 | - * @var string |
|
| 13 | - */ |
|
| 14 | - protected $entity; |
|
| 9 | + /** |
|
| 10 | + * Name of the affected Entity Map. |
|
| 11 | + * |
|
| 12 | + * @var string |
|
| 13 | + */ |
|
| 14 | + protected $entity; |
|
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * Set the affected Entity Map. |
|
| 18 | - * |
|
| 19 | - * @param string $entity |
|
| 20 | - * |
|
| 21 | - * @return $this |
|
| 22 | - */ |
|
| 23 | - public function setEntity($entity) |
|
| 24 | - { |
|
| 25 | - $this->entity = $entity; |
|
| 16 | + /** |
|
| 17 | + * Set the affected Entity Map. |
|
| 18 | + * |
|
| 19 | + * @param string $entity |
|
| 20 | + * |
|
| 21 | + * @return $this |
|
| 22 | + */ |
|
| 23 | + public function setEntity($entity) |
|
| 24 | + { |
|
| 25 | + $this->entity = $entity; |
|
| 26 | 26 | |
| 27 | - $this->message = "No query results for entity [{$entity}]."; |
|
| 27 | + $this->message = "No query results for entity [{$entity}]."; |
|
| 28 | 28 | |
| 29 | - return $this; |
|
| 30 | - } |
|
| 29 | + return $this; |
|
| 30 | + } |
|
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * Get the affected Entity. |
|
| 34 | - * |
|
| 35 | - * @return string |
|
| 36 | - */ |
|
| 37 | - public function getEntity() |
|
| 38 | - { |
|
| 39 | - return $this->entity; |
|
| 40 | - } |
|
| 32 | + /** |
|
| 33 | + * Get the affected Entity. |
|
| 34 | + * |
|
| 35 | + * @return string |
|
| 36 | + */ |
|
| 37 | + public function getEntity() |
|
| 38 | + { |
|
| 39 | + return $this->entity; |
|
| 40 | + } |
|
| 41 | 41 | } |
@@ -17,118 +17,118 @@ |
||
| 17 | 17 | */ |
| 18 | 18 | class Analogue |
| 19 | 19 | { |
| 20 | - /** |
|
| 21 | - * @var self |
|
| 22 | - */ |
|
| 23 | - protected static $instance; |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * @var Manager |
|
| 27 | - */ |
|
| 28 | - protected static $manager; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * @var Capsule |
|
| 32 | - */ |
|
| 33 | - protected static $capsule; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * @var bool |
|
| 37 | - */ |
|
| 38 | - protected static $booted = false; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * Analogue constructor. |
|
| 42 | - * |
|
| 43 | - * @param array $connection |
|
| 44 | - */ |
|
| 45 | - public function __construct(array $connection) |
|
| 46 | - { |
|
| 47 | - if (!static::$booted) { |
|
| 48 | - static::$capsule = new Capsule(); |
|
| 49 | - |
|
| 50 | - $this->addConnection($connection); |
|
| 51 | - |
|
| 52 | - $this->boot(); |
|
| 53 | - } |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * Boot Analogue. |
|
| 58 | - * |
|
| 59 | - * @return Analogue |
|
| 60 | - */ |
|
| 61 | - public function boot() |
|
| 62 | - { |
|
| 63 | - if (static::$booted) { |
|
| 64 | - return $this; |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - $dispatcher = new Dispatcher(); |
|
| 68 | - |
|
| 69 | - $connectionProvider = new CapsuleConnectionProvider(static::$capsule); |
|
| 70 | - |
|
| 71 | - $illuminate = new IlluminateDriver($connectionProvider); |
|
| 72 | - |
|
| 73 | - $driverManager = new DriverManager(); |
|
| 74 | - |
|
| 75 | - $driverManager->addDriver($illuminate); |
|
| 76 | - |
|
| 77 | - static::$manager = new Manager($driverManager, $dispatcher); |
|
| 78 | - |
|
| 79 | - static::$instance = $this; |
|
| 80 | - |
|
| 81 | - static::$booted = true; |
|
| 82 | - |
|
| 83 | - return $this; |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * Add a connection array to Capsule. |
|
| 88 | - * |
|
| 89 | - * @param array $config |
|
| 90 | - * @param string $name |
|
| 91 | - */ |
|
| 92 | - public function addConnection($config, $name = 'default') |
|
| 93 | - { |
|
| 94 | - static::$capsule->addConnection($config, $name); |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - /** |
|
| 98 | - * Get a Database connection object. |
|
| 99 | - * |
|
| 100 | - * @param $name |
|
| 101 | - * |
|
| 102 | - * @return \Illuminate\Database\Connection |
|
| 103 | - */ |
|
| 104 | - public function connection($name = null) |
|
| 105 | - { |
|
| 106 | - return static::$capsule->getConnection($name); |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * Dynamically handle static calls to the instance, Facade Style. |
|
| 111 | - * |
|
| 112 | - * @param string $method |
|
| 113 | - * @param array $parameters |
|
| 114 | - * |
|
| 115 | - * @return mixed |
|
| 116 | - */ |
|
| 117 | - public static function __callStatic($method, $parameters) |
|
| 118 | - { |
|
| 119 | - return call_user_func_array([static::$instance, $method], $parameters); |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * Dynamically handle calls to the Analogue Manager instance. |
|
| 124 | - * |
|
| 125 | - * @param string $method |
|
| 126 | - * @param array $parameters |
|
| 127 | - * |
|
| 128 | - * @return mixed |
|
| 129 | - */ |
|
| 130 | - public function __call($method, $parameters) |
|
| 131 | - { |
|
| 132 | - return call_user_func_array([static::$manager, $method], $parameters); |
|
| 133 | - } |
|
| 20 | + /** |
|
| 21 | + * @var self |
|
| 22 | + */ |
|
| 23 | + protected static $instance; |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * @var Manager |
|
| 27 | + */ |
|
| 28 | + protected static $manager; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * @var Capsule |
|
| 32 | + */ |
|
| 33 | + protected static $capsule; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * @var bool |
|
| 37 | + */ |
|
| 38 | + protected static $booted = false; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * Analogue constructor. |
|
| 42 | + * |
|
| 43 | + * @param array $connection |
|
| 44 | + */ |
|
| 45 | + public function __construct(array $connection) |
|
| 46 | + { |
|
| 47 | + if (!static::$booted) { |
|
| 48 | + static::$capsule = new Capsule(); |
|
| 49 | + |
|
| 50 | + $this->addConnection($connection); |
|
| 51 | + |
|
| 52 | + $this->boot(); |
|
| 53 | + } |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * Boot Analogue. |
|
| 58 | + * |
|
| 59 | + * @return Analogue |
|
| 60 | + */ |
|
| 61 | + public function boot() |
|
| 62 | + { |
|
| 63 | + if (static::$booted) { |
|
| 64 | + return $this; |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + $dispatcher = new Dispatcher(); |
|
| 68 | + |
|
| 69 | + $connectionProvider = new CapsuleConnectionProvider(static::$capsule); |
|
| 70 | + |
|
| 71 | + $illuminate = new IlluminateDriver($connectionProvider); |
|
| 72 | + |
|
| 73 | + $driverManager = new DriverManager(); |
|
| 74 | + |
|
| 75 | + $driverManager->addDriver($illuminate); |
|
| 76 | + |
|
| 77 | + static::$manager = new Manager($driverManager, $dispatcher); |
|
| 78 | + |
|
| 79 | + static::$instance = $this; |
|
| 80 | + |
|
| 81 | + static::$booted = true; |
|
| 82 | + |
|
| 83 | + return $this; |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * Add a connection array to Capsule. |
|
| 88 | + * |
|
| 89 | + * @param array $config |
|
| 90 | + * @param string $name |
|
| 91 | + */ |
|
| 92 | + public function addConnection($config, $name = 'default') |
|
| 93 | + { |
|
| 94 | + static::$capsule->addConnection($config, $name); |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + /** |
|
| 98 | + * Get a Database connection object. |
|
| 99 | + * |
|
| 100 | + * @param $name |
|
| 101 | + * |
|
| 102 | + * @return \Illuminate\Database\Connection |
|
| 103 | + */ |
|
| 104 | + public function connection($name = null) |
|
| 105 | + { |
|
| 106 | + return static::$capsule->getConnection($name); |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * Dynamically handle static calls to the instance, Facade Style. |
|
| 111 | + * |
|
| 112 | + * @param string $method |
|
| 113 | + * @param array $parameters |
|
| 114 | + * |
|
| 115 | + * @return mixed |
|
| 116 | + */ |
|
| 117 | + public static function __callStatic($method, $parameters) |
|
| 118 | + { |
|
| 119 | + return call_user_func_array([static::$instance, $method], $parameters); |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * Dynamically handle calls to the Analogue Manager instance. |
|
| 124 | + * |
|
| 125 | + * @param string $method |
|
| 126 | + * @param array $parameters |
|
| 127 | + * |
|
| 128 | + * @return mixed |
|
| 129 | + */ |
|
| 130 | + public function __call($method, $parameters) |
|
| 131 | + { |
|
| 132 | + return call_user_func_array([static::$manager, $method], $parameters); |
|
| 133 | + } |
|
| 134 | 134 | } |
@@ -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 | } |
@@ -4,21 +4,21 @@ |
||
| 4 | 4 | |
| 5 | 5 | interface ScopeInterface |
| 6 | 6 | { |
| 7 | - /** |
|
| 8 | - * Apply the scope to a given Query builder. |
|
| 9 | - * |
|
| 10 | - * @param \Analogue\ORM\System\Query $builder |
|
| 11 | - * |
|
| 12 | - * @return void |
|
| 13 | - */ |
|
| 14 | - public function apply(Query $builder); |
|
| 7 | + /** |
|
| 8 | + * Apply the scope to a given Query builder. |
|
| 9 | + * |
|
| 10 | + * @param \Analogue\ORM\System\Query $builder |
|
| 11 | + * |
|
| 12 | + * @return void |
|
| 13 | + */ |
|
| 14 | + public function apply(Query $builder); |
|
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * Remove the scope from the Query builder. |
|
| 18 | - * |
|
| 19 | - * @param \Analogue\ORM\System\Query $builder |
|
| 20 | - * |
|
| 21 | - * @return void |
|
| 22 | - */ |
|
| 23 | - public function remove(Query $builder); |
|
| 16 | + /** |
|
| 17 | + * Remove the scope from the Query builder. |
|
| 18 | + * |
|
| 19 | + * @param \Analogue\ORM\System\Query $builder |
|
| 20 | + * |
|
| 21 | + * @return void |
|
| 22 | + */ |
|
| 23 | + public function remove(Query $builder); |
|
| 24 | 24 | } |
@@ -6,110 +6,110 @@ |
||
| 6 | 6 | |
| 7 | 7 | class SingleTableInheritanceScope implements ScopeInterface |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * Discriminator column name. |
|
| 11 | - * |
|
| 12 | - * @var string |
|
| 13 | - */ |
|
| 14 | - protected $column; |
|
| 15 | - |
|
| 16 | - /** |
|
| 17 | - * Discriminator column allowed types. |
|
| 18 | - * |
|
| 19 | - * @var array |
|
| 20 | - */ |
|
| 21 | - protected $types = []; |
|
| 22 | - |
|
| 23 | - public function __construct(EntityMap $entityMap) |
|
| 24 | - { |
|
| 25 | - // Putting the heavy logic in here, so we won't have |
|
| 26 | - // to go through it each time we reach for a query |
|
| 27 | - // builder. |
|
| 28 | - |
|
| 29 | - $this->column = $entityMap->getDiscriminatorColumn(); |
|
| 30 | - |
|
| 31 | - // First we need to retrieve the base class & it's normalized |
|
| 32 | - // type string |
|
| 33 | - $class = $entityMap->getClass(); |
|
| 34 | - $this->types[] = $this->getTypeStringForEntity($class, $entityMap); |
|
| 35 | - |
|
| 36 | - // Then, we parse all registered entities for any potential |
|
| 37 | - // child class. |
|
| 38 | - $classes = Manager::getInstance()->getRegisteredEntities(); |
|
| 39 | - |
|
| 40 | - foreach ($classes as $otherClass => $entityMap) { |
|
| 41 | - if (is_subclass_of($otherClass, $class)) { |
|
| 42 | - $this->types[] = $this->getTypeStringForEntity($otherClass, $entityMap); |
|
| 43 | - } |
|
| 44 | - } |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * Get the normalized value to use for query on discriminator column. |
|
| 49 | - * |
|
| 50 | - * @param string $class |
|
| 51 | - * @param EntityMap $entityMap |
|
| 52 | - * |
|
| 53 | - * @return string |
|
| 54 | - */ |
|
| 55 | - protected function getTypeStringForEntity($class, EntityMap $entityMap) |
|
| 56 | - { |
|
| 57 | - $class = $entityMap->getClass(); |
|
| 58 | - |
|
| 59 | - $type = array_keys( |
|
| 60 | - $entityMap->getDiscriminatorColumnMap(), |
|
| 61 | - $class |
|
| 62 | - ); |
|
| 63 | - |
|
| 64 | - if (count($type) == 0) { |
|
| 65 | - return $class; |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - return $type[0]; |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * Apply the scope to a given Analogue query builder. |
|
| 73 | - * |
|
| 74 | - * @param \Analogue\ORM\System\Query $query |
|
| 75 | - * |
|
| 76 | - * @return void |
|
| 77 | - */ |
|
| 78 | - public function apply(Query $query) |
|
| 79 | - { |
|
| 80 | - $query->whereIn($this->column, $this->types); |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * Remove the scope from the given Analogue query builder. |
|
| 85 | - * |
|
| 86 | - * @param mixed $query |
|
| 87 | - * |
|
| 88 | - * @return void |
|
| 89 | - */ |
|
| 90 | - public function remove(Query $query) |
|
| 91 | - { |
|
| 92 | - $query = $query->getQuery(); |
|
| 93 | - |
|
| 94 | - foreach ((array) $query->wheres as $key => $where) { |
|
| 95 | - if ($this->isSingleTableConstraint($where, $this->column)) { |
|
| 96 | - unset($query->wheres[$key]); |
|
| 97 | - |
|
| 98 | - $query->wheres = array_values($query->wheres); |
|
| 99 | - } |
|
| 100 | - } |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * Determine if the given where clause is a single table inheritance constraint. |
|
| 105 | - * |
|
| 106 | - * @param array $where |
|
| 107 | - * @param string $column |
|
| 108 | - * |
|
| 109 | - * @return bool |
|
| 110 | - */ |
|
| 111 | - protected function isSingleTableConstraint(array $where, $column) |
|
| 112 | - { |
|
| 113 | - return $where['column'] == $column; |
|
| 114 | - } |
|
| 9 | + /** |
|
| 10 | + * Discriminator column name. |
|
| 11 | + * |
|
| 12 | + * @var string |
|
| 13 | + */ |
|
| 14 | + protected $column; |
|
| 15 | + |
|
| 16 | + /** |
|
| 17 | + * Discriminator column allowed types. |
|
| 18 | + * |
|
| 19 | + * @var array |
|
| 20 | + */ |
|
| 21 | + protected $types = []; |
|
| 22 | + |
|
| 23 | + public function __construct(EntityMap $entityMap) |
|
| 24 | + { |
|
| 25 | + // Putting the heavy logic in here, so we won't have |
|
| 26 | + // to go through it each time we reach for a query |
|
| 27 | + // builder. |
|
| 28 | + |
|
| 29 | + $this->column = $entityMap->getDiscriminatorColumn(); |
|
| 30 | + |
|
| 31 | + // First we need to retrieve the base class & it's normalized |
|
| 32 | + // type string |
|
| 33 | + $class = $entityMap->getClass(); |
|
| 34 | + $this->types[] = $this->getTypeStringForEntity($class, $entityMap); |
|
| 35 | + |
|
| 36 | + // Then, we parse all registered entities for any potential |
|
| 37 | + // child class. |
|
| 38 | + $classes = Manager::getInstance()->getRegisteredEntities(); |
|
| 39 | + |
|
| 40 | + foreach ($classes as $otherClass => $entityMap) { |
|
| 41 | + if (is_subclass_of($otherClass, $class)) { |
|
| 42 | + $this->types[] = $this->getTypeStringForEntity($otherClass, $entityMap); |
|
| 43 | + } |
|
| 44 | + } |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * Get the normalized value to use for query on discriminator column. |
|
| 49 | + * |
|
| 50 | + * @param string $class |
|
| 51 | + * @param EntityMap $entityMap |
|
| 52 | + * |
|
| 53 | + * @return string |
|
| 54 | + */ |
|
| 55 | + protected function getTypeStringForEntity($class, EntityMap $entityMap) |
|
| 56 | + { |
|
| 57 | + $class = $entityMap->getClass(); |
|
| 58 | + |
|
| 59 | + $type = array_keys( |
|
| 60 | + $entityMap->getDiscriminatorColumnMap(), |
|
| 61 | + $class |
|
| 62 | + ); |
|
| 63 | + |
|
| 64 | + if (count($type) == 0) { |
|
| 65 | + return $class; |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + return $type[0]; |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * Apply the scope to a given Analogue query builder. |
|
| 73 | + * |
|
| 74 | + * @param \Analogue\ORM\System\Query $query |
|
| 75 | + * |
|
| 76 | + * @return void |
|
| 77 | + */ |
|
| 78 | + public function apply(Query $query) |
|
| 79 | + { |
|
| 80 | + $query->whereIn($this->column, $this->types); |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * Remove the scope from the given Analogue query builder. |
|
| 85 | + * |
|
| 86 | + * @param mixed $query |
|
| 87 | + * |
|
| 88 | + * @return void |
|
| 89 | + */ |
|
| 90 | + public function remove(Query $query) |
|
| 91 | + { |
|
| 92 | + $query = $query->getQuery(); |
|
| 93 | + |
|
| 94 | + foreach ((array) $query->wheres as $key => $where) { |
|
| 95 | + if ($this->isSingleTableConstraint($where, $this->column)) { |
|
| 96 | + unset($query->wheres[$key]); |
|
| 97 | + |
|
| 98 | + $query->wheres = array_values($query->wheres); |
|
| 99 | + } |
|
| 100 | + } |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * Determine if the given where clause is a single table inheritance constraint. |
|
| 105 | + * |
|
| 106 | + * @param array $where |
|
| 107 | + * @param string $column |
|
| 108 | + * |
|
| 109 | + * @return bool |
|
| 110 | + */ |
|
| 111 | + protected function isSingleTableConstraint(array $where, $column) |
|
| 112 | + { |
|
| 113 | + return $where['column'] == $column; |
|
| 114 | + } |
|
| 115 | 115 | } |