diff --git a/composer.json b/composer.json index af759ef..82804a2 100755 --- a/composer.json +++ b/composer.json @@ -1,8 +1,14 @@ { "name": "analogue/orm", "description": "An intuitive Data Mapper ORM for PHP and Laravel", - "keywords": ["orm", "datamapper", "laravel", - "entity", "repository", "mapper"], + "keywords": [ + "orm", + "datamapper", + "laravel", + "entity", + "repository", + "mapper" + ], "homepage": "http://github.com/analogueorm/analogue", "license": "MIT", "authors": [ @@ -29,5 +35,5 @@ } }, "minimum-stability": "stable", - "prefer-stable":true + "prefer-stable": true } diff --git a/src/Analogue.php b/src/Analogue.php index 818acfb..5aa56b8 100755 --- a/src/Analogue.php +++ b/src/Analogue.php @@ -1,4 +1,6 @@ -addConnection($connection); @@ -66,12 +85,12 @@ public function boot() /** * Add a connection array to Capsule * - * @param array $config - * @param string $name + * @param array $config + * @param string $name */ public function addConnection($config, $name = 'default') { - return static::$capsule->addConnection($config, $name); + static::$capsule->addConnection($config, $name); } /** @@ -88,24 +107,24 @@ public function connection($name = null) /** * Dynamically handle static calls to the instance, Facade Style. * - * @param string $method - * @param array $parameters + * @param string $method + * @param array $parameters * @return mixed */ public static function __callStatic($method, $parameters) { - return call_user_func_array(array(static::$instance, $method), $parameters); + return call_user_func_array([static::$instance, $method], $parameters); } /** * Dynamically handle calls to the Analogue Manager instance. * - * @param string $method - * @param array $parameters + * @param string $method + * @param array $parameters * @return mixed */ public function __call($method, $parameters) { - return call_user_func_array(array(static::$manager, $method), $parameters); + return call_user_func_array([static::$manager, $method], $parameters); } } diff --git a/src/AnalogueFacade.php b/src/AnalogueFacade.php index e1459de..012fecf 100755 --- a/src/AnalogueFacade.php +++ b/src/AnalogueFacade.php @@ -1,4 +1,6 @@ -aggregate = $aggregate; diff --git a/src/Commands/Delete.php b/src/Commands/Delete.php index 0ceaae6..ec78304 100755 --- a/src/Commands/Delete.php +++ b/src/Commands/Delete.php @@ -1,14 +1,17 @@ -aggregate->getEntityId(); if (is_null($id)) { - throw new MappingException("Executed a delete command on an entity with 'null' as primary key"); + throw new MappingException('Executed a delete command on an entity with "null" as primary key'); } $this->query->where($keyName, '=', $id)->delete(); diff --git a/src/Commands/Store.php b/src/Commands/Store.php index 5f5128f..09e3ae1 100755 --- a/src/Commands/Store.php +++ b/src/Commands/Store.php @@ -3,15 +3,9 @@ namespace Analogue\ORM\Commands; use Analogue\ORM\Mappable; -use Analogue\ORM\System\Mapper; -use Analogue\ORM\System\Manager; use Analogue\ORM\EntityCollection; -use Analogue\ORM\Drivers\QueryAdapter; use Analogue\ORM\System\Aggregate; -use Analogue\ORM\System\InternallyMappable; use Analogue\ORM\System\Proxies\EntityProxy; -use Analogue\ORM\Exceptions\MappingException; -use Analogue\ORM\System\Proxies\ProxyInterface; use Analogue\ORM\System\Proxies\CollectionProxy; /** @@ -20,11 +14,11 @@ */ class Store extends Command { - /** * Persist the entity in the database * - * @return void + * @throws \InvalidArgumentException + * @return false|mixed */ public function execute() { @@ -42,11 +36,11 @@ public function execute() * We will test the entity for existence * and run a creation if it doesn't exists */ - if (! $this->aggregate->exists()) { + if (!$this->aggregate->exists()) { if ($mapper->fireEvent('creating', $entity) === false) { return false; } - + $this->insert(); $mapper->fireEvent('created', $entity, false); @@ -70,20 +64,20 @@ public function execute() $mapper->fireEvent('stored', $entity, false); return $entity; - ; } /** * Run all operations that have to occur before actually * storing the entity * + * @throws \InvalidArgumentException * @return void */ protected function preStoreProcess() { // Create any related object that doesn't exist in the database. $localRelationships = $this->aggregate->getEntityMap()->getLocalRelationships(); - + $this->createRelatedEntities($localRelationships); } @@ -91,12 +85,13 @@ protected function preStoreProcess() * Check for existence and create non-existing related entities * * @param array + * @throws \InvalidArgumentException * @return void */ protected function createRelatedEntities($relations) { $entitiesToCreate = $this->aggregate->getNonExistingRelated($relations); - + foreach ($entitiesToCreate as $aggregate) { $this->createStoreCommand($aggregate)->execute(); } @@ -106,7 +101,7 @@ protected function createRelatedEntities($relations) * Create a new store command * * @param Aggregate $aggregate - * @return void + * @return Store */ protected function createStoreCommand(Aggregate $aggregate) { @@ -120,6 +115,7 @@ protected function createStoreCommand(Aggregate $aggregate) * Run all operations that have to occur after the entity * is stored. * + * @throws \InvalidArgumentException * @return void */ protected function postStoreProcess() @@ -147,7 +143,7 @@ protected function postStoreProcess() // This should be move to the wrapper class // so it's the same code for the entity builder $aggregate->setProxies(); - + // Update Entity Cache $aggregate->getMapper()->getEntityCache()->refresh($aggregate); } @@ -164,7 +160,7 @@ protected function updateDirtyRelated() $attributes = $this->getAttributes(); foreach ($relations as $relation) { - if (! array_key_exists($relation, $attributes)) { + if (!array_key_exists($relation, $attributes)) { continue; } @@ -181,7 +177,7 @@ protected function updateDirtyRelated() if ($value instanceof CollectionProxy && $value->isLoaded()) { $value = $value->getUnderlyingCollection(); } - if ($value instanceof CollectionProxy && ! $value->isLoaded()) { + if ($value instanceof CollectionProxy && !$value->isLoaded()) { foreach ($value->getAddedItems() as $entity) { $this->updateEntityIfDirty($entity); } @@ -190,7 +186,7 @@ protected function updateDirtyRelated() if ($value instanceof EntityCollection) { foreach ($value as $entity) { - if (! $this->createEntityIfNotExists($entity)) { + if (!$this->createEntityIfNotExists($entity)) { $this->updateEntityIfDirty($entity); } } @@ -231,6 +227,8 @@ protected function insert() /** * Run an update statement on the entity * + * @throws \InvalidArgumentException + * * @return void */ protected function update() @@ -242,7 +240,7 @@ protected function update() $query = $query->where($keyName, '=', $this->aggregate->getEntityId()); $dirtyAttributes = $this->aggregate->getDirtyRawAttributes(); - + if (count($dirtyAttributes) > 0) { $query->update($dirtyAttributes); } diff --git a/src/Drivers/CapsuleConnectionProvider.php b/src/Drivers/CapsuleConnectionProvider.php index 7370735..f2b58f0 100755 --- a/src/Drivers/CapsuleConnectionProvider.php +++ b/src/Drivers/CapsuleConnectionProvider.php @@ -1,12 +1,20 @@ -capsule = $capsule; diff --git a/src/Drivers/DBAdapter.php b/src/Drivers/DBAdapter.php index 2b01b56..2445046 100755 --- a/src/Drivers/DBAdapter.php +++ b/src/Drivers/DBAdapter.php @@ -1,15 +1,15 @@ -db = $db; diff --git a/src/Drivers/IlluminateDBAdapter.php b/src/Drivers/IlluminateDBAdapter.php index 1b7f803..b2fee07 100755 --- a/src/Drivers/IlluminateDBAdapter.php +++ b/src/Drivers/IlluminateDBAdapter.php @@ -1,4 +1,6 @@ -connection = $connection; @@ -67,6 +75,6 @@ public function commit() */ public function rollback() { - $this->connection->rollback(); + $this->connection->rollBack(); } } diff --git a/src/Drivers/IlluminateDriver.php b/src/Drivers/IlluminateDriver.php index b34e0db..40dd650 100755 --- a/src/Drivers/IlluminateDriver.php +++ b/src/Drivers/IlluminateDriver.php @@ -1,15 +1,20 @@ -connectionProvider = $connectionProvider; @@ -28,8 +33,8 @@ public function getName() /** * Get Analogue DBAdapter * - * @param string $connection - * @return \Analogue\ORM\DBAdater + * @param string|null $connection + * @return IlluminateDBAdapter */ public function getAdapter($connection = null) { diff --git a/src/Drivers/IlluminateQueryAdapter.php b/src/Drivers/IlluminateQueryAdapter.php index 40e05f3..2d2870d 100755 --- a/src/Drivers/IlluminateQueryAdapter.php +++ b/src/Drivers/IlluminateQueryAdapter.php @@ -1,19 +1,38 @@ -query = $query; } + /** + * @param $method + * @param $parameters + * @return mixed + */ public function __call($method, $parameters) { - return call_user_func_array(array($this->query, $method), $parameters); + return call_user_func_array([$this->query, $method], $parameters); } } diff --git a/src/Drivers/Manager.php b/src/Drivers/Manager.php index 5a6f24c..b14d50b 100755 --- a/src/Drivers/Manager.php +++ b/src/Drivers/Manager.php @@ -1,14 +1,18 @@ -hasGetMutator($key)) { - $method = 'get'.$this->getMutatorMethod($key); + $method = 'get' . $this->getMutatorMethod($key); $attribute = null; @@ -36,7 +33,7 @@ public function __get($key) return $this->$method($attribute); } - if (! array_key_exists($key, $this->attributes)) { + if (!array_key_exists($key, $this->attributes)) { return null; } if ($this->attributes[$key] instanceof EntityProxy) { @@ -48,14 +45,14 @@ public function __get($key) /** * Dynamically set attributes on the entity. * - * @param string $key - * @param mixed $value + * @param string $key + * @param mixed $value * @return void */ public function __set($key, $value) { if ($this->hasSetMutator($key)) { - $method = 'set'.$this->getMutatorMethod($key); + $method = 'set' . $this->getMutatorMethod($key); $this->$method($value); } else { @@ -66,28 +63,32 @@ public function __set($key, $value) /** * Is a getter method defined ? * - * @param string $key + * @param string $key * @return boolean */ protected function hasGetMutator($key) { - return method_exists($this, 'get'.$this->getMutatorMethod($key)) ? true : false; + return method_exists($this, 'get' . $this->getMutatorMethod($key)) ? true : false; } /** * Is a setter method defined ? * - * @param string $key + * @param string $key * @return boolean */ protected function hasSetMutator($key) { - return method_exists($this, 'set'.$this->getMutatorMethod($key)) ? true : false; + return method_exists($this, 'set' . $this->getMutatorMethod($key)) ? true : false; } + /** + * @param $key + * @return string + */ protected function getMutatorMethod($key) { - return ucfirst($key).'Attribute'; + return ucfirst($key) . 'Attribute'; } /** @@ -107,7 +108,7 @@ public function toArray() continue; } if ($this->hasGetMutator($key)) { - $method = 'get'.$this->getMutatorMethod($key); + $method = 'get' . $this->getMutatorMethod($key); $attributes[$key] = $this->$method($attribute); } } diff --git a/src/EntityCollection.php b/src/EntityCollection.php index 63d4fa3..5b2ac7a 100755 --- a/src/EntityCollection.php +++ b/src/EntityCollection.php @@ -2,8 +2,7 @@ namespace Analogue\ORM; -use InvalidArgumentException; -use Analogue\ORM\Mappable; +use Analogue\ORM\Exceptions\MappingException; use Analogue\ORM\System\Manager; use Analogue\ORM\System\Wrappers\Factory; use Illuminate\Support\Collection; @@ -11,7 +10,6 @@ class EntityCollection extends Collection { - /** * Wrapper Factory * @@ -19,6 +17,10 @@ class EntityCollection extends Collection */ protected $factory; + /** + * EntityCollection constructor. + * @param array|null $entities + */ public function __construct(array $entities = null) { $this->factory = new Factory; @@ -29,9 +31,9 @@ public function __construct(array $entities = null) /** * Find an entity in the collection by key. * - * @param mixed $key - * @param mixed $default - * + * @param mixed $key + * @param mixed $default + * @throws MappingException * @return \Analogue\ORM\Entity */ public function find($key, $default = null) @@ -48,7 +50,7 @@ public function find($key, $default = null) /** * Add an entity to the collection. * - * @param Mappable $entity + * @param Mappable $entity * @return $this */ public function add($entity) @@ -60,6 +62,10 @@ public function add($entity) /** * Remove an entity from the collection + * + * @param $entity + * @throws MappingException + * @return mixed */ public function remove($entity) { @@ -71,7 +77,7 @@ public function remove($entity) /** * Push an item onto the end of the collection. * - * @param mixed $value + * @param mixed $value * @return void */ public function push($value) @@ -82,8 +88,8 @@ public function push($value) /** * Put an item in the collection by key. * - * @param mixed $key - * @param mixed $value + * @param mixed $key + * @param mixed $value * @return void */ public function put($key, $value) @@ -94,8 +100,8 @@ public function put($key, $value) /** * Set the item at a given offset. * - * @param mixed $key - * @param mixed $value + * @param mixed $key + * @param mixed $value * @return void */ public function offsetSet($key, $value) @@ -110,19 +116,20 @@ public function offsetSet($key, $value) /** * Determine if a key exists in the collection. * - * @param mixed $key + * @param mixed $key + * @param mixed|null $value * @return bool */ public function contains($key, $value = null) { - return ! is_null($this->find($key)); + return !is_null($this->find($key)); } /** * Fetch a nested element of the collection. * - * @param string $key - * @return static + * @param string $key + * @return self */ public function fetch($key) { @@ -132,18 +139,19 @@ public function fetch($key) /** * Generic function for returning class.key value pairs * + * @throws MappingException * @return string */ public function getEntityHashes() { - return array_map(function ($entity) { + return array_map(function($entity) { $class = get_class($entity); $mapper = Manager::getMapper($class); $keyName = $mapper->getEntityMap()->getKeyName(); - return $class.'.'.$entity->getEntityAttribute($keyName); + return $class . '.' . $entity->getEntityAttribute($keyName); }, $this->items); } @@ -151,8 +159,9 @@ public function getEntityHashes() /** * Get a subset of the collection from entity hashes * - * @param array $hashes - * @return + * @param array $hashes + * @throws MappingException + * @return array */ public function getSubsetByHashes(array $hashes) { @@ -165,7 +174,7 @@ public function getSubsetByHashes(array $hashes) $keyName = $mapper->getEntityMap()->getKeyName(); - if (in_array($class.'.'.$item->$keyName, $hashes)) { + if (in_array($class . '.' . $item->$keyName, $hashes)) { $subset[] = $item; } } @@ -176,8 +185,9 @@ public function getSubsetByHashes(array $hashes) /** * Merge the collection with the given items. * - * @param array $items - * @return static + * @param array $items + * @throws MappingException + * @return self */ public function merge($items) { @@ -193,8 +203,8 @@ public function merge($items) /** * Diff the collection with the given items. * - * @param \ArrayAccess|array $items - * @return static + * @param \ArrayAccess|array $items + * @return self */ public function diff($items) { @@ -203,7 +213,7 @@ public function diff($items) $dictionary = $this->getDictionary($items); foreach ($this->items as $item) { - if (! isset($dictionary[$this->getEntityKey($item)])) { + if (!isset($dictionary[$this->getEntityKey($item)])) { $diff->add($item); } } @@ -214,8 +224,9 @@ public function diff($items) /** * Intersect the collection with the given items. * - * @param \ArrayAccess|array $items - * @return static + * @param \ArrayAccess|array $items + * @throws MappingException + * @return self */ public function intersect($items) { @@ -235,8 +246,8 @@ public function intersect($items) /** * Returns only the models from the collection with the specified keys. * - * @param mixed $keys - * @return static + * @param mixed $keys + * @return self */ public function only($keys) { @@ -248,8 +259,8 @@ public function only($keys) /** * Returns all models in the collection except the models with specified keys. * - * @param mixed $keys - * @return static + * @param mixed $keys + * @return self */ public function except($keys) { @@ -261,14 +272,15 @@ public function except($keys) /** * Get a dictionary keyed by primary keys. * - * @param \ArrayAccess|array $items + * @param \ArrayAccess|array $items + * @throws MappingException * @return array */ public function getDictionary($items = null) { $items = is_null($items) ? $this->items : $items; - $dictionary = array(); + $dictionary = []; foreach ($items as $value) { $dictionary[$this->getEntityKey($value)] = $value; @@ -277,11 +289,20 @@ public function getDictionary($items = null) return $dictionary; } + /** + * @throws MappingException + * @return array + */ public function getEntityKeys() { return array_keys($this->getDictionary()); } + /** + * @param $entity + * @throws MappingException + * @return mixed + */ protected function getEntityKey($entity) { $keyName = Manager::getMapper($entity)->getEntityMap()->getKeyName(); @@ -294,12 +315,13 @@ protected function getEntityKey($entity) /** * Get the max value of a given key. * - * @param string $key + * @param string|null $key + * @throws MappingException * @return mixed */ public function max($key = null) { - return $this->reduce(function ($result, $item) use ($key) { + return $this->reduce(function($result, $item) use ($key) { $wrapper = $this->factory->make($item); return (is_null($result) || $wrapper->getEntityAttribute($key) > $result) ? @@ -310,12 +332,13 @@ public function max($key = null) /** * Get the min value of a given key. * - * @param string $key + * @param string|null $key + * @throws MappingException * @return mixed */ public function min($key = null) { - return $this->reduce(function ($result, $item) use ($key) { + return $this->reduce(function($result, $item) use ($key) { $wrapper = $this->factory->make($item); return (is_null($result) || $wrapper->getEntityAttribute($key) < $result) @@ -326,9 +349,9 @@ public function min($key = null) /** * Get an array with the values of a given key. * - * @param string $value - * @param string $key - * @return static + * @param string $value + * @param string|null $key + * @return self */ public function pluck($value, $key = null) { @@ -338,9 +361,9 @@ public function pluck($value, $key = null) /** * Alias for the "pluck" method. * - * @param string $value - * @param string $key - * @return static + * @param string $value + * @param string|null $key + * @return self */ public function lists($value, $key = null) { @@ -350,7 +373,9 @@ public function lists($value, $key = null) /** * Return only unique items from the collection. * - * @return static + * @param string|null $key + * @throws MappingException + * @return self */ public function unique($key = null) { diff --git a/src/EntityMap.php b/src/EntityMap.php index bcf2a13..e73a931 100755 --- a/src/EntityMap.php +++ b/src/EntityMap.php @@ -2,10 +2,10 @@ namespace Analogue\ORM; +use Analogue\ORM\Exceptions\MappingException; use Exception; use ReflectionClass; use Analogue\ORM\System\Manager; -use Analogue\ORM\System\Mapper; use Analogue\ORM\System\Wrappers\Factory; use Analogue\ORM\Relationships\BelongsTo; use Analogue\ORM\Relationships\BelongsToMany; @@ -23,7 +23,6 @@ */ class EntityMap { - /** * The mapping driver to use with this entity */ @@ -39,7 +38,7 @@ class EntityMap /** * The table associated with the entity. * - * @var string + * @var string|null */ protected $table = null; @@ -61,7 +60,7 @@ class EntityMap /** * The Custom Domain Class to use with this mapping * - * @var string + * @var string|null */ protected $class = null; @@ -147,7 +146,7 @@ class EntityMap * Sequence name, to be used with postgreSql * defaults to %table_name%_id_seq * - * @var string + * @var string|null */ protected $sequence = null; @@ -199,8 +198,7 @@ class EntityMap * * @var array */ - protected static $manyClasses = ['BelongsToMany', 'HasMany', 'HasManyThrough', - 'MorphMany', 'MorphToMany']; + protected static $manyClasses = ['BelongsToMany', 'HasMany', 'HasManyThrough', 'MorphMany', 'MorphToMany']; /** * The 'Single' relationships classes, which related Entity attribute should be @@ -208,7 +206,7 @@ class EntityMap * * @var array */ - protected static $singleClasses = ['BelongsTo', 'HasOne', 'MorphOne','MorphTo']; + protected static $singleClasses = ['BelongsTo', 'HasOne', 'MorphOne', 'MorphTo']; /** * Relationships with a pivot record @@ -229,8 +227,15 @@ class EntityMap * * @var array */ - protected static $foreignClasses = ['BelongsToMany', 'HasMany', 'HasManyThrough', - 'MorphMany', 'MorphToMany', 'HasOne', 'MorphOne']; + protected static $foreignClasses = [ + 'BelongsToMany', + 'HasMany', + 'HasManyThrough', + 'MorphMany', + 'MorphToMany', + 'HasOne', + 'MorphOne', + ]; /** * The date format to use with the current database connection @@ -279,12 +284,12 @@ public function setAttributes(array $attributeNames) /** * Get all the attribute names for the class, including relationships, embeddables and primary key. * - * @return [type] [description] + * @return array */ public function getCompiledAttributes() { $key = $this->getKeyName(); - + $embeddables = array_keys($this->getEmbeddables()); $relationships = $this->getRelationships(); @@ -337,7 +342,7 @@ public function getDriver() /** * Set the db connection to use on the table * - * @param [type] $connection [description] + * @param $connection */ public function setConnection($connection) { @@ -361,7 +366,7 @@ public function getConnection() */ public function getTable() { - if (! is_null($this->table)) { + if (!is_null($this->table)) { return $this->table; } @@ -385,10 +390,10 @@ public function setTable($table) */ public function getSequence() { - if (! is_null($this->sequence)) { + if (!is_null($this->sequence)) { return $this->sequence; } else { - return $this->getTable().'_id_seq'; + return $this->getTable() . '_id_seq'; } } @@ -405,12 +410,12 @@ public function getClass() /** * Set the custom entity class * - * @param string namespaced class name + * @param string $class namespaced class name */ public function setClass($class) { // Throw exception if class not exists - + $this->class = $class; } @@ -500,11 +505,11 @@ public function getPivotRelationships() * by hooking the 'initializing' event, before entityMap is initialized. * * @param string $name Relation name - * @param Closure $relationship + * @param \Closure $relationship * * @return void */ - public function addRelationshipMethod($name, Closure $relationship) + public function addRelationshipMethod($name, \Closure $relationship) { $this->dynamicRelationships[$name] = $relationship; } @@ -543,6 +548,7 @@ public function getKeyName() /** * Set the primary key for the entity. * + * @param $key * @return void */ public function setKeyName($key) @@ -557,7 +563,7 @@ public function setKeyName($key) */ public function getQualifiedKeyName() { - return $this->getTable().'.'.$this->getKeyName(); + return $this->getTable() . '.' . $this->getKeyName(); } /** @@ -573,7 +579,7 @@ public function getPerPage() /** * Set the number of models to return per page. * - * @param int $perPage + * @param int $perPage * @return void */ public function setPerPage($perPage) @@ -638,16 +644,17 @@ public function getQualifiedDeletedAtColumn() */ public function getForeignKey() { - return snake_case(class_basename($this->getClass())).'_id'; + return snake_case(class_basename($this->getClass())) . '_id'; } /** * Define a one-to-one relationship. * - * @param $entity - * @param string $related entity class - * @param string $foreignKey - * @param string $localKey + * @param $entity + * @param string $relatedClass entity class + * @param string $foreignKey + * @param string $localKey + * @throws MappingException * @return \Analogue\ORM\Relationships\HasOne */ public function hasOne($entity, $relatedClass, $foreignKey = null, $localKey = null) @@ -660,17 +667,19 @@ public function hasOne($entity, $relatedClass, $foreignKey = null, $localKey = n $localKey = $localKey ?: $this->getKeyName(); - return new HasOne($relatedMapper, $entity, $relatedMap->getTable().'.'.$foreignKey, $localKey); + return new HasOne($relatedMapper, $entity, $relatedMap->getTable() . '.' . $foreignKey, $localKey); } /** * Define a polymorphic one-to-one relationship. * - * @param string $related - * @param string $name - * @param string $type - * @param string $id - * @param string $localKey + * @param mixed $entity + * @param string $related + * @param string $name + * @param string|null $type + * @param string|null $id + * @param string|null $localKey + * @throws MappingException * @return \Analogue\ORM\Relationships\MorphOne */ public function morphOne($entity, $related, $name, $type = null, $id = null, $localKey = null) @@ -683,16 +692,18 @@ public function morphOne($entity, $related, $name, $type = null, $id = null, $lo $table = $relatedMapper->getEntityMap()->getTable(); - return new MorphOne($relatedMapper, $entity, $table.'.'.$type, $table.'.'.$id, $localKey); + return new MorphOne($relatedMapper, $entity, $table . '.' . $type, $table . '.' . $id, $localKey); } /** * Define an inverse one-to-one or many relationship. * - * @param string $related - * @param string $foreignKey - * @param string $otherKey - * @param string $relation + * @param mixed $entity + * @param string $related + * @param string|null $foreignKey + * @param string|null $otherKey + * @param string|null $relation + * @throws MappingException * @return \Analogue\ORM\Relationships\BelongsTo */ public function belongsTo($entity, $related, $foreignKey = null, $otherKey = null, $relation = null) @@ -710,7 +721,7 @@ public function belongsTo($entity, $related, $foreignKey = null, $otherKey = nul // foreign key name by using the name of the relationship function, which // when combined with an "_id" should conventionally match the columns. if (is_null($foreignKey)) { - $foreignKey = snake_case($relation).'_id'; + $foreignKey = snake_case($relation) . '_id'; } $relatedMapper = $this->manager->mapper($related); @@ -723,9 +734,11 @@ public function belongsTo($entity, $related, $foreignKey = null, $otherKey = nul /** * Define a polymorphic, inverse one-to-one or many relationship. * - * @param string $name - * @param string $type - * @param string $id + * @param mixed $entity + * @param string|null $name + * @param string|null $type + * @param string|null $id + * @throws MappingException * @return \Analogue\ORM\Relationships\MorphTo */ public function morphTo($entity, $name = null, $type = null, $id = null) @@ -772,9 +785,11 @@ public function morphTo($entity, $name = null, $type = null, $id = null) /** * Define a one-to-many relationship. * - * @param string $related - * @param string $foreignKey - * @param string $localKey + * @param mixed $entity + * @param string $related + * @param string|null $foreignKey + * @param string|null $localKey + * @throws MappingException * @return \Analogue\ORM\Relationships\HasMany */ public function hasMany($entity, $related, $foreignKey = null, $localKey = null) @@ -783,7 +798,7 @@ public function hasMany($entity, $related, $foreignKey = null, $localKey = null) $relatedMapper = $this->manager->mapper($related); - $table = $relatedMapper->getEntityMap()->getTable().'.'.$foreignKey; + $table = $relatedMapper->getEntityMap()->getTable() . '.' . $foreignKey; $localKey = $localKey ?: $this->getKeyName(); @@ -793,10 +808,12 @@ public function hasMany($entity, $related, $foreignKey = null, $localKey = null) /** * Define a has-many-through relationship. * - * @param string $related - * @param string $through - * @param string|null $firstKey - * @param string|null $secondKey + * @param mixed $entity + * @param string $related + * @param string $through + * @param string|null $firstKey + * @param string|null $secondKey + * @throws MappingException * @return \Analogue\ORM\Relationships\HasManyThrough */ public function hasManyThrough($entity, $related, $through, $firstKey = null, $secondKey = null) @@ -818,11 +835,12 @@ public function hasManyThrough($entity, $related, $through, $firstKey = null, $s /** * Define a polymorphic one-to-many relationship. * - * @param string $related - * @param string $name - * @param string $type - * @param string $id - * @param string $localKey + * @param mixed $entity + * @param string $related + * @param string $name + * @param string|null $type + * @param string|null $id + * @param string|null $localKey * @return \Analogue\ORM\Relationships\MorphMany */ public function morphMany($entity, $related, $name, $type = null, $id = null, $localKey = null) @@ -838,17 +856,19 @@ public function morphMany($entity, $related, $name, $type = null, $id = null, $l $localKey = $localKey ?: $this->getKeyName(); - return new MorphMany($relatedMapper, $entity, $table.'.'.$type, $table.'.'.$id, $localKey); + return new MorphMany($relatedMapper, $entity, $table . '.' . $type, $table . '.' . $id, $localKey); } /** * Define a many-to-many relationship. * - * @param string $related - * @param string $table - * @param string $foreignKey - * @param string $otherKey - * @param string $relation + * @param mixed $entity + * @param string $related + * @param string|null $table + * @param string|null $foreignKey + * @param string|null $otherKey + * @param string|null $relation + * @throws MappingException * @return \Analogue\ORM\Relationships\BelongsToMany */ public function belongsToMany($entity, $related, $table = null, $foreignKey = null, $otherKey = null, $relation = null) @@ -884,12 +904,14 @@ public function belongsToMany($entity, $related, $table = null, $foreignKey = nu /** * Define a polymorphic many-to-many relationship. * - * @param string $related - * @param string $name - * @param string $table - * @param string $foreignKey - * @param string $otherKey - * @param bool $inverse + * @param mixed $entity + * @param string $related + * @param string $name + * @param string|null $table + * @param string|null $foreignKey + * @param string|null $otherKey + * @param bool $inverse + * @throws MappingException * @return \Analogue\ORM\Relationships\MorphToMany */ public function morphToMany($entity, $related, $name, $table = null, $foreignKey = null, $otherKey = null, $inverse = false) @@ -899,7 +921,7 @@ public function morphToMany($entity, $related, $name, $table = null, $foreignKey // First, we will need to determine the foreign key and "other key" for the // relationship. Once we have determined the keys we will make the query // instances, as well as the relationship instances we need for these. - $foreignKey = $foreignKey ?: $name.'_id'; + $foreignKey = $foreignKey ?: $name . '_id'; $relatedMapper = $this->manager->mapper($related); @@ -907,20 +929,19 @@ public function morphToMany($entity, $related, $name, $table = null, $foreignKey $table = $table ?: str_plural($name); - return new MorphToMany( - $relatedMapper, $entity, $name, $table, $foreignKey, - $otherKey, $caller, $inverse - ); + return new MorphToMany($relatedMapper, $entity, $name, $table, $foreignKey, $otherKey, $caller, $inverse); } /** * Define a polymorphic, inverse many-to-many relationship. * - * @param string $related - * @param string $name - * @param string $table - * @param string $foreignKey - * @param string $otherKey + * @param mixed $entity + * @param string $related + * @param string $name + * @param string|null $table + * @param string|null $foreignKey + * @param string|null $otherKey + * @throws MappingException * @return \Analogue\ORM\Relationships\MorphToMany */ public function morphedByMany($entity, $related, $name, $table = null, $foreignKey = null, $otherKey = null) @@ -930,7 +951,7 @@ public function morphedByMany($entity, $related, $name, $table = null, $foreignK // For the inverse of the polymorphic many-to-many relations, we will change // the way we determine the foreign and other keys, as it is the opposite // of the morph-to-many method since we're figuring out these inverses. - $otherKey = $otherKey ?: $name.'_id'; + $otherKey = $otherKey ?: $name . '_id'; return $this->morphToMany($entity, $related, $name, $table, $foreignKey, $otherKey, true); } @@ -938,7 +959,7 @@ public function morphedByMany($entity, $related, $name, $table = null, $foreignK /** * Get the relationship name of the belongs to many. * - * @return string + * @return string */ protected function getBelongsToManyCaller() { @@ -947,16 +968,16 @@ protected function getBelongsToManyCaller() $caller = array_first(debug_backtrace(false), function ($key, $trace) use ($self) { $caller = $trace['function']; - return (! in_array($caller, EntityMap::$manyMethods) && $caller != $self); + return (!in_array($caller, EntityMap::$manyMethods) && $caller != $self); }); - return ! is_null($caller) ? $caller['function'] : null; + return !is_null($caller) ? $caller['function'] : null; } /** * Get the joining table name for a many-to-many relation. * - * @param string $related + * @param EntityMap $relatedMap * @return string */ public function joiningTable($relatedMap) @@ -968,7 +989,7 @@ public function joiningTable($relatedMap) $related = $relatedMap->getTable(); - $tables = array($related, $base); + $tables = [$related, $base]; // Now that we have the model names in an array we can just sort them and // use the implode function to join them together with an underscores, @@ -981,18 +1002,18 @@ public function joiningTable($relatedMap) /** * Get the polymorphic relationship columns. * - * @param string $name - * @param string $type - * @param string $id - * @return array + * @param string $name + * @param string $type + * @param string $id + * @return string[] */ protected function getMorphs($name, $type, $id) { - $type = $type ?: $name.'_type'; + $type = $type ?: $name . '_type'; - $id = $id ?: $name.'_id'; + $id = $id ?: $name . '_id'; - return array($type, $id); + return [$type, $id]; } /** @@ -1008,10 +1029,10 @@ public function getMorphClass() /** * Create a new Entity Collection instance. * - * @param array $entities + * @param array $entities * @return \Analogue\ORM\EntityCollection */ - public function newCollection(array $entities = array()) + public function newCollection(array $entities = []) { return new EntityCollection($entities, $this); } @@ -1066,8 +1087,8 @@ protected function getCustomMethods() /** * Parse user's class methods for relationships * - * @param array $customMethods - * @return + * @param array $customMethods + * @return array */ protected function parseMethodsForRelationship(array $customMethods) { @@ -1137,7 +1158,7 @@ protected function sortRelationshipsByType() /** * Override this method for custom entity instantiation * - * @return mixed + * @return null */ public function activator() { @@ -1147,19 +1168,20 @@ public function activator() /** * Call dynamic relationship, if it exists * - * @param string $method - * @param array $parameters + * @param string $method + * @param array $parameters + * @throws Exception * @return mixed */ public function __call($method, $parameters) { - if (! array_key_exists($method, $this->dynamicRelationships)) { - throw new Exception(get_class($this)." has no method $method"); + if (!array_key_exists($method, $this->dynamicRelationships)) { + throw new Exception(get_class($this) . " has no method $method"); } // Add $this to parameters so the closure can call relationship method on the map. $parameters[] = $this; - return call_user_func_array(array($this->dynamicRelationships[$method], $parameters)); + return call_user_func_array([$this->dynamicRelationships[$method], $parameters]); } } diff --git a/src/Exceptions/EntityNotFoundException.php b/src/Exceptions/EntityNotFoundException.php index 028a7ae..21dc367 100755 --- a/src/Exceptions/EntityNotFoundException.php +++ b/src/Exceptions/EntityNotFoundException.php @@ -1,10 +1,11 @@ -entity; } diff --git a/src/Exceptions/MappingException.php b/src/Exceptions/MappingException.php index c1ccb52..3a81690 100755 --- a/src/Exceptions/MappingException.php +++ b/src/Exceptions/MappingException.php @@ -1,4 +1,6 @@ -attributes)) - { + if (array_key_exists($key, $this->attributes)) { return $this->attributes[$key]; + } else { + return null; } - else return null; } } diff --git a/src/Plugins/AnaloguePlugin.php b/src/Plugins/AnaloguePlugin.php index 4da59ac..0b15f29 100755 --- a/src/Plugins/AnaloguePlugin.php +++ b/src/Plugins/AnaloguePlugin.php @@ -1,10 +1,11 @@ -manager = $manager; diff --git a/src/Plugins/AnaloguePluginInterface.php b/src/Plugins/AnaloguePluginInterface.php index cab7491..a46af43 100755 --- a/src/Plugins/AnaloguePluginInterface.php +++ b/src/Plugins/AnaloguePluginInterface.php @@ -1,8 +1,9 @@ -aggregate; diff --git a/src/Plugins/SoftDeletes/SoftDeletesPlugin.php b/src/Plugins/SoftDeletes/SoftDeletesPlugin.php index 20b7e16..b0c9866 100755 --- a/src/Plugins/SoftDeletes/SoftDeletesPlugin.php +++ b/src/Plugins/SoftDeletes/SoftDeletesPlugin.php @@ -3,7 +3,6 @@ namespace Analogue\ORM\Plugins\SoftDeletes; use Carbon\Carbon; -use Analogue\ORM\System\Manager; use Analogue\ORM\System\Mapper; use Analogue\ORM\Plugins\AnaloguePlugin; use Analogue\ORM\System\Wrappers\Factory; @@ -14,10 +13,10 @@ */ class SoftDeletesPlugin extends AnaloguePlugin { - /** * Register the plugin * + * @throws \Exception * @return void */ public function register() @@ -25,7 +24,7 @@ public function register() $host = $this; // Hook any mapper init and check the mapping include soft deletes. - $this->manager->registerGlobalEvent('initialized', function ($mapper) use ($host) { + $this->manager->registerGlobalEvent('initialized', function (Mapper $mapper) use ($host) { $entityMap = $mapper->getEntityMap(); if ($entityMap->usesSoftDeletes()) { @@ -39,8 +38,9 @@ public function register() * By hooking to the mapper initialization event, we can extend it * with the softDelete capacity. * - * @param \Analogue\ORM\System\Mapper $mapper - * @return void + * @param \Analogue\ORM\System\Mapper $mapper + * @throws \Analogue\ORM\Exceptions\MappingException + * @return bool|void */ protected function registerSoftDelete(Mapper $mapper) { @@ -52,7 +52,7 @@ protected function registerSoftDelete(Mapper $mapper) $host = $this; // Register 'deleting' events - $mapper->registerEvent('deleting', function ($entity) use ($entityMap,$host) { + $mapper->registerEvent('deleting', function ($entity) use ($entityMap, $host) { // Convert Entity into an EntityWrapper $factory = new Factory; @@ -61,10 +61,10 @@ protected function registerSoftDelete(Mapper $mapper) $deletedAtField = $entityMap->getQualifiedDeletedAtColumn(); - if (! is_null($wrappedEntity->getEntityAttribute($deletedAtField))) { + if (!is_null($wrappedEntity->getEntityAttribute($deletedAtField))) { return true; } else { - $time= new Carbon; + $time = new Carbon; $wrappedEntity->setEntityAttribute($deletedAtField, $time); @@ -83,7 +83,7 @@ protected function registerSoftDelete(Mapper $mapper) /** * Get custom events provided by the plugin * - * @return array + * @return string[] */ public function getCustomEvents() { diff --git a/src/Plugins/SoftDeletes/SoftDeletingScope.php b/src/Plugins/SoftDeletes/SoftDeletingScope.php index c0d221e..27108fc 100755 --- a/src/Plugins/SoftDeletes/SoftDeletingScope.php +++ b/src/Plugins/SoftDeletes/SoftDeletingScope.php @@ -1,11 +1,12 @@ -manager->registerGlobalEvent('initialized', function ($mapper) { + $this->manager->registerGlobalEvent('initialized', function (Mapper $mapper) { $entityMap = $mapper->getEntityMap(); if ($entityMap->usesTimestamps()) { $mapper->registerEvent('creating', function ($entity) use ($entityMap) { - + $factory = new Factory; $wrappedEntity = $factory->make($entity); $createdAtField = $entityMap->getCreatedAtColumn(); $updatedAtField = $entityMap->getUpdatedAtColumn(); - $time= new Carbon; + $time = new Carbon; $wrappedEntity->setEntityAttribute($createdAtField, $time); $wrappedEntity->setEntityAttribute($updatedAtField, $time); @@ -46,7 +46,7 @@ public function register() $updatedAtField = $entityMap->getUpdatedAtColumn(); - $time= new Carbon; + $time = new Carbon; $wrappedEntity->setEntityAttribute($updatedAtField, $time); }); diff --git a/src/Relationships/BelongsTo.php b/src/Relationships/BelongsTo.php index 5299a1e..fc046c7 100755 --- a/src/Relationships/BelongsTo.php +++ b/src/Relationships/BelongsTo.php @@ -2,6 +2,7 @@ namespace Analogue\ORM\Relationships; +use Analogue\ORM\Mappable; use Analogue\ORM\System\Query; use Analogue\ORM\System\Mapper; use Analogue\ORM\EntityCollection; @@ -9,7 +10,6 @@ class BelongsTo extends Relationship { - /** * The foreign key of the parent model. * @@ -41,11 +41,11 @@ class BelongsTo extends Relationship /** * Create a new belongs to relationship instance. * - * @param \Analogue\ORM\System\Mapper $mapper - * @param Mappable $parent - * @param string $foreignKey - * @param string $otherKey - * @param string $relation + * @param Mapper $mapper + * @param Mappable $parent + * @param string $foreignKey + * @param string $otherKey + * @param string $relation */ public function __construct(Mapper $mapper, $parent, $foreignKey, $otherKey, $relation) { @@ -55,21 +55,31 @@ public function __construct(Mapper $mapper, $parent, $foreignKey, $otherKey, $re parent::__construct($mapper, $parent); } - + + /** + * @param $related + * @return mixed + */ public function attachTo($related) { - return $this->associate($related); + $this->associate($related); } + /** + * @param $related + * @return Mappable + */ public function detachFrom($related) { - return $this->dissociate($related); + return $this->dissociate($related); //todo } - + /** * Get the results of the relationship. * - * @return mixed + * @param $relation + * + * @return \Analogue\ORM\Entity */ public function getResults($relation) { @@ -93,22 +103,22 @@ public function addConstraints() // of the related models matching on the foreign key that's on a parent. $table = $this->relatedMap->getTable(); - $this->query->where($table.'.'.$this->otherKey, '=', $this->parent->getEntityAttribute($this->foreignKey)); + $this->query->where($table . '.' . $this->otherKey, '=', $this->parent->getEntityAttribute($this->foreignKey)); } } /** * Add the constraints for a relationship count query. * - * @param \Analogue\ORM\System\Query $query - * @param \Analogue\ORM\System\Query $parent - * @return \Analogue\ORM\System\Query + * @param Query $query + * @param Query $parent + * @return Query */ public function getRelationCountQuery(Query $query, Query $parent) { $query->select(new Expression('count(*)')); - $otherKey = $this->wrap($query->getTable().'.'.$this->otherKey); + $otherKey = $this->wrap($query->getTable() . '.' . $this->otherKey); return $query->where($this->getQualifiedForeignKey(), '=', new Expression($otherKey)); } @@ -116,7 +126,7 @@ public function getRelationCountQuery(Query $query, Query $parent) /** * Set the constraints for an eager load of the relation. * - * @param array $entities + * @param array $entities * @return void */ public function addEagerConstraints(array $entities) @@ -124,7 +134,7 @@ public function addEagerConstraints(array $entities) // We'll grab the primary key name of the related models since it could be set to // a non-standard name and not "id". We will then construct the constraint for // our eagerly loading query so it returns the proper models from execution. - $key = $this->relatedMap->getTable().'.'.$this->otherKey; + $key = $this->relatedMap->getTable() . '.' . $this->otherKey; $this->query->whereIn($key, $this->getEagerModelKeys($entities)); } @@ -132,12 +142,12 @@ public function addEagerConstraints(array $entities) /** * Gather the keys from an array of related models. * - * @param array $entities + * @param array $entities * @return array */ protected function getEagerModelKeys(array $entities) { - $keys = array(); + $keys = []; // First we need to gather all of the keys from the parent models so we know what // to query for via the eager loading query. We will add them to an array then @@ -145,7 +155,7 @@ protected function getEagerModelKeys(array $entities) foreach ($entities as $entity) { $entity = $this->factory->make($entity); - if (! is_null($value = $entity->getEntityAttribute($this->foreignKey))) { + if (!is_null($value = $entity->getEntityAttribute($this->foreignKey))) { $keys[] = $value; } } @@ -154,7 +164,7 @@ protected function getEagerModelKeys(array $entities) // it so the query doesn't fail, but will not return any results, which should // be what this developer is expecting in a case where this happens to them. if (count($keys) == 0) { - return array(0); + return [0]; } return array_values(array_unique($keys)); @@ -163,8 +173,8 @@ protected function getEagerModelKeys(array $entities) /** * Initialize the relation on a set of models. * - * @param array $entities - * @param string $relation + * @param array $entities + * @param string $relation * @return array */ public function initRelation(array $entities, $relation) @@ -180,9 +190,9 @@ public function initRelation(array $entities, $relation) /** * Match the eagerly loaded results to their parents. * - * @param array $entities - * @param \Analogue\ORM\EntityCollection $results - * @param string $relation + * @param array $entities + * @param EntityCollection $results + * @param string $relation * @return array */ public function match(array $entities, EntityCollection $results, $relation) @@ -229,7 +239,7 @@ public function associate($entity) //$this->parent->setEntityAttribute($this->foreignKey, $entity->getEntityAttribute($this->otherKey)); // // Instead, we'll just add the object to the Entity's attribute - + $this->parent->setEntityAttribute($this->relation, $entity); } @@ -244,7 +254,7 @@ public function dissociate() // the foreign key attribute inside the parent Entity. // //$this->parent->setEntityAttribute($this->foreignKey, null); - + $this->parent->setEntityAttribute($this->relation, null); } @@ -261,7 +271,7 @@ public function getForeignKey() /** * Get the foreign key value pair for a related object * - * @var mixed $related + * @param mixed $related * * @return array */ @@ -287,7 +297,7 @@ public function getForeignKeyValuePair($related) */ public function getQualifiedForeignKey() { - return $this->parentMap->getTable().'.'.$this->foreignKey; + return $this->parentMap->getTable() . '.' . $this->foreignKey; } /** @@ -307,6 +317,6 @@ public function getOtherKey() */ public function getQualifiedOtherKeyName() { - return $this->relatedMap->getTable().'.'.$this->otherKey; + return $this->relatedMap->getTable() . '.' . $this->otherKey; } } diff --git a/src/Relationships/BelongsToMany.php b/src/Relationships/BelongsToMany.php index e959d69..f23bb86 100755 --- a/src/Relationships/BelongsToMany.php +++ b/src/Relationships/BelongsToMany.php @@ -2,6 +2,7 @@ namespace Analogue\ORM\Relationships; +use Analogue\ORM\Mappable; use Analogue\ORM\System\Query; use Analogue\ORM\System\Mapper; use Analogue\ORM\EntityCollection; @@ -9,13 +10,8 @@ use Analogue\ORM\System\InternallyMappable; use Analogue\ORM\Exceptions\EntityNotFoundException; -/** - * @method where - * @method take - */ class BelongsToMany extends Relationship { - /** * The intermediate table for the relation. * @@ -49,7 +45,7 @@ class BelongsToMany extends Relationship * * @var array */ - protected $pivotColumns = array(); + protected $pivotColumns = []; /** * This relationship has pivot attributes @@ -61,12 +57,12 @@ class BelongsToMany extends Relationship /** * Create a new has many relationship instance. * - * @param Mapper $mapper - * @param Mappable $parent - * @param string $table - * @param string $foreignKey - * @param string $otherKey - * @param string $relationName + * @param Mapper $mapper + * @param Mappable $parent + * @param string $table + * @param string $foreignKey + * @param string $otherKey + * @param string $relationName */ public function __construct(Mapper $mapper, $parent, $table, $foreignKey, $otherKey, $relationName = null) { @@ -78,11 +74,18 @@ public function __construct(Mapper $mapper, $parent, $table, $foreignKey, $other parent::__construct($mapper, $parent); } - + /** + * @param $related + * @return mixed + */ public function attachTo($related) { } + /** + * @param $related + * @return mixed + */ public function detachFrom($related) { $ids = $this->getIdsFromHashes([$related]); @@ -90,6 +93,9 @@ public function detachFrom($related) $this->detach($ids); } + /** + * @param $related + */ public function detachMany($related) { $ids = $this->getIdsFromHashes($related); @@ -97,6 +103,10 @@ public function detachMany($related) $this->detach($ids); } + /** + * @param array $hashes + * @return array + */ protected function getIdsFromHashes(array $hashes) { $ids = []; @@ -111,7 +121,9 @@ protected function getIdsFromHashes(array $hashes) /** * Get the results of the relationship. * - * @return mixed + * @param $relation + * + * @return EntityCollection */ public function getResults($relation) { @@ -125,24 +137,24 @@ public function getResults($relation) /** * Set a where clause for a pivot table column. * - * @param string $column - * @param string $operator - * @param mixed $value - * @param string $boolean - * @return \Analogue\ORM\Relationships\BelongsToMany + * @param string $column + * @param string $operator + * @param mixed $value + * @param string $boolean + * @return self */ public function wherePivot($column, $operator = null, $value = null, $boolean = 'and') { - return $this->where($this->table.'.'.$column, $operator, $value, $boolean); + return $this->where($this->table . '.' . $column, $operator, $value, $boolean); } /** * Set an or where clause for a pivot table column. * - * @param string $column - * @param string $operator - * @param mixed $value - * @return \Analogue\ORM\Relationships\BelongsToMany + * @param string $column + * @param string $operator + * @param mixed $value + * @return self */ public function orWherePivot($column, $operator = null, $value = null) { @@ -162,10 +174,10 @@ public function getPivotAttributes() /** * Execute the query and get the first result. * - * @param array $columns + * @param array $columns * @return mixed */ - public function first($columns = array('*')) + public function first($columns = ['*']) { $results = $this->take(1)->get($columns); @@ -175,14 +187,15 @@ public function first($columns = array('*')) /** * Execute the query and get the first result or throw an exception. * - * @param array $columns - * @return Mappable|static + * @param array $columns + * + * @throws EntityNotFoundException * - * @throws Mappable|EntityNotFoundException + * @return Mappable|self */ - public function firstOrFail($columns = array('*')) + public function firstOrFail($columns = ['*']) { - if (! is_null($entity = $this->first($columns))) { + if (!is_null($entity = $this->first($columns))) { return $entity; } @@ -192,15 +205,15 @@ public function firstOrFail($columns = array('*')) /** * Execute the query as a "select" statement. * - * @param array $columns + * @param array $columns * @return \Analogue\ORM\EntityCollection */ - public function get($columns = array('*')) + public function get($columns = ['*']) { // First we'll add the proper select columns onto the query so it is run with // the proper columns. Then, we will get the results and hydrate out pivot // models with the result of those columns as a separate model relation. - $columns = $this->query->getQuery()->columns ? array() : $columns; + $columns = $this->query->getQuery()->columns ? [] : $columns; $select = $this->getSelectColumns($columns); @@ -221,7 +234,7 @@ public function get($columns = array('*')) /** * Hydrate the pivot table relationship on the models. * - * @param array $entities + * @param array $entities * @return void */ protected function hydratePivotRelation(array $entities) @@ -229,7 +242,7 @@ protected function hydratePivotRelation(array $entities) // To hydrate the pivot relationship, we will just gather the pivot attributes // and create a new Pivot model, which is basically a dynamic model that we // will set the attributes, table, and connections on so it they be used. - + foreach ($entities as $entity) { $entityWrapper = $this->factory->make($entity); @@ -247,7 +260,7 @@ protected function hydratePivotRelation(array $entities) */ protected function cleanPivotAttributes(InternallyMappable $entity) { - $values = array(); + $values = []; $attributes = $entity->getEntityAttributes(); @@ -285,9 +298,9 @@ public function addConstraints() /** * Add the constraints for a relationship count query. * - * @param \Analogue\ORM\Query $query - * @param \Analogue\ORM\Query $parent - * @return \Analogue\ORM\Query + * @param Query $query + * @param Query $parent + * @return Query */ public function getRelationCountQuery(Query $query, Query $parent) { @@ -303,9 +316,9 @@ public function getRelationCountQuery(Query $query, Query $parent) /** * Add the constraints for a relationship count query on the same table. * - * @param \Analogue\ORM\Query $query - * @param \Analogue\ORM\Query $parent - * @return \Analogue\ORM\Query + * @param Query $query + * @param Query $parent + * @return Query */ public function getRelationCountQueryForSelfJoin(Query $query, Query $parent) { @@ -313,11 +326,11 @@ public function getRelationCountQueryForSelfJoin(Query $query, Query $parent) $tablePrefix = $this->query->getQuery()->getConnection()->getTablePrefix(); - $query->from($this->table.' as '.$tablePrefix.$hash = $this->getRelationCountHash()); + $query->from($this->table . ' as ' . $tablePrefix . $hash = $this->getRelationCountHash()); $key = $this->wrap($this->getQualifiedParentKeyName()); - return $query->where($hash.'.'.$this->foreignKey, '=', new Expression($key)); + return $query->where($hash . '.' . $this->foreignKey, '=', new Expression($key)); } /** @@ -327,19 +340,19 @@ public function getRelationCountQueryForSelfJoin(Query $query, Query $parent) */ public function getRelationCountHash() { - return 'self_'.md5(microtime(true)); + return 'self_' . md5(microtime(true)); } /** * Set the select clause for the relation query. * - * @param array $columns + * @param array $columns * @return \Analogue\ORM\Relationships\BelongsToMany */ - protected function getSelectColumns(array $columns = array('*')) + protected function getSelectColumns(array $columns = ['*']) { - if ($columns == array('*')) { - $columns = array($this->relatedMap->getTable().'.*'); + if ($columns == ['*']) { + $columns = [$this->relatedMap->getTable() . '.*']; } return array_merge($columns, $this->getAliasedPivotColumns()); @@ -352,15 +365,15 @@ protected function getSelectColumns(array $columns = array('*')) */ protected function getAliasedPivotColumns() { - $defaults = array($this->foreignKey, $this->otherKey); + $defaults = [$this->foreignKey, $this->otherKey]; // We need to alias all of the pivot columns with the "pivot_" prefix so we // can easily extract them out of the models and put them into the pivot // relationships when they are retrieved and hydrated into the models. - $columns = array(); + $columns = []; foreach (array_merge($defaults, $this->pivotColumns) as $column) { - $columns[] = $this->table.'.'.$column.' as pivot_'.$column; + $columns[] = $this->table . '.' . $column . ' as pivot_' . $column; } return array_unique($columns); @@ -381,7 +394,7 @@ protected function setJoin($query = null) // model instance. Then we can set the "where" for the parent models. $baseTable = $this->relatedMap->getTable(); - $key = $baseTable.'.'.$this->relatedMap->getKeyName(); + $key = $baseTable . '.' . $this->relatedMap->getKeyName(); $query->join($this->table, $key, '=', $this->getOtherKey()); @@ -407,7 +420,7 @@ protected function setWhere() /** * Set the constraints for an eager load of the relation. * - * @param array $entities + * @param array $entities * @return void */ public function addEagerConstraints(array $entities) @@ -418,8 +431,8 @@ public function addEagerConstraints(array $entities) /** * Initialize the relation on a set of eneities. * - * @param array $entities - * @param string $relation + * @param array $entities + * @param string $relation * @return array */ public function initRelation(array $entities, $relation) @@ -436,9 +449,9 @@ public function initRelation(array $entities, $relation) /** * Match the eagerly loaded results to their parents. * - * @param array $entities - * @param \Analogue\ORM\EntityCollection $results - * @param string $relation + * @param array $entities + * @param EntityCollection $results + * @param string $relation * @return array */ public function match(array $entities, EntityCollection $results, $relation) @@ -470,7 +483,7 @@ public function match(array $entities, EntityCollection $results, $relation) /** * Build model dictionary keyed by the relation's foreign key. * - * @param \Analogue\ORM\EntityCollection $results + * @param EntityCollection $results * @return array */ protected function buildDictionary(EntityCollection $results) @@ -480,7 +493,7 @@ protected function buildDictionary(EntityCollection $results) // First we will build a dictionary of child models keyed by the foreign key // of the relation so that we will easily and quickly match them to their // parents without having a possibly slow inner loops for every models. - $dictionary = array(); + $dictionary = []; foreach ($results as $entity) { $wrapper = $this->factory->make($entity); @@ -506,7 +519,7 @@ public function getRelatedIds() /** * Update Pivot * - * @param $entity + * @param \Analogue\ORM\Entity $entity * @return void */ public function updatePivot($entity) @@ -514,8 +527,8 @@ public function updatePivot($entity) $keyName = $this->relatedMap->getKeyName(); $this->updateExistingPivot( - $entity->getEntityAttribute($keyName), - $entity->getEntityAttribute('pivot')->getEntityAttributes() + $entity->getEntityAttribute($keyName), + $entity->getEntityAttribute('pivot')->getEntityAttributes() ); } @@ -535,7 +548,7 @@ public function updatePivots($relatedEntities) /** * Create Pivot Records * - * @param $relatedEntities + * @param \Analogue\ORM\Entity[] $relatedEntities * @return void */ public function createPivots($relatedEntities) @@ -547,7 +560,6 @@ public function createPivots($relatedEntities) foreach ($relatedEntities as $entity) { $keys[] = $entity->getEntityAttribute($keyName); - //$attributes = $entity->getPivotEntity()->getEntityAttributes(); } $records = $this->createAttachRecords($keys, $attributes); @@ -558,9 +570,10 @@ public function createPivots($relatedEntities) /** * Update an existing pivot record on the table. * - * @param mixed $id - * @param array $attributes - * @return void + * @param mixed $id + * @param array $attributes + * @throws \InvalidArgumentException + * @return integer */ public function updateExistingPivot($id, array $attributes) { @@ -568,19 +581,17 @@ public function updateExistingPivot($id, array $attributes) $attributes = $this->setTimestampsOnAttach($attributes, true); } - $updated = $this->newPivotStatementForId($id)->update($attributes); - - return $updated; + return $this->newPivotStatementForId($id)->update($attributes); } /** * Attach a model to the parent. * - * @param mixed $id - * @param array $attributes + * @param mixed $id + * @param array $attributes * @return void */ - public function attach($id, array $attributes = array()) + public function attach($id, array $attributes = []) { $query = $this->newPivotStatement(); @@ -588,10 +599,9 @@ public function attach($id, array $attributes = array()) } /** - * Synchronize Relationship + * @param array $entities * - * @param mixed $entities - * @return [type] [description] + * @throws \InvalidArgumentException */ public function sync(array $entities) { @@ -602,9 +612,12 @@ public function sync(array $entities) * Detach related entities that are not in $id * * @param array $entities + * + * @throws \InvalidArgumentException + * * @return void */ - protected function detachExcept(array $entities = array()) + protected function detachExcept(array $entities = []) { $query = $this->newPivotQuery(); @@ -626,13 +639,13 @@ protected function detachExcept(array $entities = array()) /** * Create an array of records to insert into the pivot table. * - * @param array $ids - * @param array $attributes + * @param array $ids + * @param array $attributes * @return array */ protected function createAttachRecords($ids, array $attributes) { - $records = array(); + $records = []; $timed = in_array($this->createdAt(), $this->pivotColumns); @@ -649,10 +662,10 @@ protected function createAttachRecords($ids, array $attributes) /** * Create a full attachment record payload. * - * @param int $key - * @param mixed $value - * @param array $attributes - * @param bool $timed + * @param int $key + * @param mixed $value + * @param array $attributes + * @param bool $timed * @return array */ protected function attacher($key, $value, $attributes, $timed) @@ -670,25 +683,25 @@ protected function attacher($key, $value, $attributes, $timed) /** * Get the attach record ID and extra attributes. * - * @param mixed $key - * @param mixed $value - * @param array $attributes + * @param int $key + * @param mixed $value + * @param array $attributes * @return array */ protected function getAttachId($key, $value, array $attributes) { if (is_array($value)) { - return array($key, array_merge($value, $attributes)); + return [$key, array_merge($value, $attributes)]; } - return array($value, $attributes); + return [$value, $attributes]; } /** * Create a new pivot attachment record. * - * @param int $id - * @param bool $timed + * @param int $id + * @param bool $timed * @return array */ protected function createAttachRecord($id, $timed) @@ -714,15 +727,15 @@ protected function createAttachRecord($id, $timed) /** * Set the creation and update timestamps on an attach record. * - * @param array $record - * @param bool $exists + * @param array $record + * @param bool $exists * @return array */ protected function setTimestampsOnAttach(array $record, $exists = false) { $fresh = $this->freshTimestamp(); - if (! $exists) { + if (!$exists) { $record[$this->createdAt()] = $fresh; } @@ -731,20 +744,27 @@ protected function setTimestampsOnAttach(array $record, $exists = false) return $record; } + /** + * @param EntityCollection $entities + * @return array + */ protected function getModelKeysFromCollection(EntityCollection $entities) { $keyName = $this->relatedMap->getKeyName(); - return array_map(function ($m) use ($keyName) { return $m->$keyName; }, $entities); + return array_map(function ($m) use ($keyName) { + return $m->$keyName; + }, $entities); } /** * Detach models from the relationship. * - * @param int|array $ids + * @param int|array $ids + * @throws \InvalidArgumentException * @return int */ - public function detach($ids = array()) + public function detach($ids = []) { if ($ids instanceof EntityCollection) { $ids = (array) $ids->modelKeys(); @@ -764,14 +784,14 @@ public function detach($ids = array()) // Once we have all of the conditions set on the statement, we are ready // to run the delete on the pivot table. Then, if the touch parameter // is true, we will go ahead and touch all related models to sync. - $results = $query->delete(); - - return $results; + return $query->delete(); } /** * Create a new query builder for the pivot table. * + * @throws \InvalidArgumentException + * * @return \Illuminate\Database\Query\Builder */ protected function newPivotQuery() @@ -796,7 +816,10 @@ public function newPivotStatement() /** * Get a new pivot statement for a given "other" ID. * - * @param mixed $id + * @param mixed $id + * + * @throws \InvalidArgumentException + * * @return \Illuminate\Database\Query\Builder */ public function newPivotStatementForId($id) @@ -813,11 +836,11 @@ public function newPivotStatementForId($id) /** * Create a new pivot model instance. * - * @param array $attributes - * @param bool $exists + * @param array $attributes + * @param bool $exists * @return \Analogue\ORM\Relationships\Pivot */ - public function newPivot(array $attributes = array(), $exists = false) + public function newPivot(array $attributes = [], $exists = false) { $pivot = new Pivot($this->parent, $this->parentMap, $attributes, $this->table, $exists); @@ -827,10 +850,10 @@ public function newPivot(array $attributes = array(), $exists = false) /** * Create a new existing pivot model instance. * - * @param array $attributes + * @param array $attributes * @return \Analogue\ORM\Relationships\Pivot */ - public function newExistingPivot(array $attributes = array()) + public function newExistingPivot(array $attributes = []) { return $this->newPivot($attributes, true); } @@ -838,7 +861,7 @@ public function newExistingPivot(array $attributes = array()) /** * Set the columns on the pivot table to retrieve. * - * @param array $columns + * @param array $columns * @return $this */ public function withPivot($columns) @@ -853,8 +876,8 @@ public function withPivot($columns) /** * Specify that the pivot table has creation and update timestamps. * - * @param mixed $createdAt - * @param mixed $updatedAt + * @param mixed $createdAt + * @param mixed $updatedAt * @return \Analogue\ORM\Relationships\BelongsToMany */ public function withTimestamps($createdAt = null, $updatedAt = null) @@ -879,7 +902,7 @@ public function getHasCompareKey() */ public function getForeignKey() { - return $this->table.'.'.$this->foreignKey; + return $this->table . '.' . $this->foreignKey; } /** @@ -889,7 +912,7 @@ public function getForeignKey() */ public function getOtherKey() { - return $this->table.'.'.$this->otherKey; + return $this->table . '.' . $this->otherKey; } /** diff --git a/src/Relationships/HasMany.php b/src/Relationships/HasMany.php index f114dd9..59500c4 100755 --- a/src/Relationships/HasMany.php +++ b/src/Relationships/HasMany.php @@ -1,13 +1,16 @@ -farParentMap->getKeyName(); - $this->query->where($parentTable.'.'.$this->firstKey, - '=', $this->farParent->getEntityAttribute($farParentKeyName)); + $this->query->where( + $parentTable . '.' . $this->firstKey, + '=', + $this->farParent->getEntityAttribute($farParentKeyName) + ); } } /** * Add the constraints for a relationship count query. * - * @param \Analogue\ORM\Query $query - * @param \Analogue\ORM\Query $parent - * @return \Analogue\ORM\Query + * @param Query $query + * @param Query $parent + * @return Query */ public function getRelationCountQuery(Query $query, Query $parent) { @@ -103,7 +115,7 @@ public function getRelationCountQuery(Query $query, Query $parent) $query->select(new Expression('count(*)')); - $key = $this->wrap($parentTable.'.'.$this->firstKey); + $key = $this->wrap($parentTable . '.' . $this->firstKey); return $query->where($this->getHasCompareKey(), '=', new Expression($key)); } @@ -111,14 +123,14 @@ public function getRelationCountQuery(Query $query, Query $parent) /** * Set the join clause on the query. * - * @param \Analogue\ORM\Query|null $query + * @param null|Query $query * @return void */ protected function setJoin(Query $query = null) { $query = $query ?: $this->query; - $foreignKey = $this->relatedMap->getTable().'.'.$this->secondKey; + $foreignKey = $this->relatedMap->getTable() . '.' . $this->secondKey; $query->join($this->parentMap->getTable(), $this->getQualifiedParentKeyName(), '=', $foreignKey); } @@ -126,22 +138,22 @@ protected function setJoin(Query $query = null) /** * Set the constraints for an eager load of the relation. * - * @param array $entities + * @param array $entities * @return void */ public function addEagerConstraints(array $entities) { $table = $this->parentMap->getTable(); - $this->query->whereIn($table.'.'.$this->firstKey, $this->getKeys($entities)); + $this->query->whereIn($table . '.' . $this->firstKey, $this->getKeys($entities)); } /** * Initialize the relation on a set of entities. * - * @param array $entities - * @param string $relation - * @return array + * @param \Analogue\ORM\Entity[] $entities + * @param string $relation + * @return \Analogue\ORM\Entity[] */ public function initRelation(array $entities, $relation) { @@ -155,10 +167,10 @@ public function initRelation(array $entities, $relation) /** * Match the eagerly loaded results to their parents. * - * @param array $entities - * @param \Analogue\ORM\EntityCollection $results - * @param string $relation - * @return array + * @param \Analogue\ORM\Entity[] $entities + * @param EntityCollection $results + * @param string $relation + * @return \Analogue\ORM\Entity[] */ public function match(array $entities, EntityCollection $results, $relation) { @@ -186,15 +198,15 @@ public function match(array $entities, EntityCollection $results, $relation) return $entities; } - /* + /** * Build model dictionary keyed by the relation's foreign key. * - * @param \Analogue\ORM\EntityCollection $results + * @param EntityCollection $results * @return array */ protected function buildDictionary(EntityCollection $results) { - $dictionary = array(); + $dictionary = []; $foreign = $this->firstKey; @@ -211,7 +223,8 @@ protected function buildDictionary(EntityCollection $results) /** * Get the results of the relationship. * - * @return mixed + * @param $relation + * @return EntityCollection */ public function getResults($relation) { @@ -225,10 +238,10 @@ public function getResults($relation) /** * Execute the query as a "select" statement. * - * @param array $columns - * @return \Analogue\ORM\EntityCollection + * @param array $columns + * @return EntityCollection */ - public function get($columns = array('*')) + public function get($columns = ['*']) { // First we'll add the proper select columns onto the query so it is run with // the proper columns. Then, we will get the results and hydrate out pivot @@ -250,32 +263,30 @@ public function get($columns = array('*')) /** * Set the select clause for the relation query. * - * @param array $columns - * @return \Analogue\ORM\Relationships\BelongsToMany + * @param array $columns + * @return BelongsToMany */ - protected function getSelectColumns(array $columns = array('*')) + protected function getSelectColumns(array $columns = ['*']) { - if ($columns == array('*')) { - $columns = array($this->relatedMap->getTable().'.*'); + if ($columns == ['*']) { + $columns = [$this->relatedMap->getTable() . '.*']; } - return array_merge($columns, array($this->parentMap->getTable().'.'.$this->firstKey)); + return array_merge($columns, [$this->parentMap->getTable() . '.' . $this->firstKey]); } /** * Get a paginator for the "select" statement. * - * @param int $perPage - * @param array $columns - * @return \Illuminate\Pagination\Paginator + * @param int $perPage + * @param array $columns + * @return \Illuminate\Pagination\LengthAwarePaginator */ - public function paginate($perPage = null, $columns = array('*')) + public function paginate($perPage = null, $columns = ['*']) { $this->query->addSelect($this->getSelectColumns($columns)); - $pager = $this->query->paginate($perPage, $columns); - - return $pager; + return $this->query->paginate($perPage, $columns); } /** diff --git a/src/Relationships/HasOne.php b/src/Relationships/HasOne.php index 3b006d3..6043d7b 100755 --- a/src/Relationships/HasOne.php +++ b/src/Relationships/HasOne.php @@ -1,13 +1,15 @@ -attachMany($entity); + $this->attachMany($entity); } - return $this->attachOne($entity); + $this->attachOne($entity); } + /** + * @param $entityHash + * @return void + */ public function detachFrom($entityHash) { if (is_array($entityHash)) { - return $this->detachMany($entityHash); + $this->detachMany($entityHash); + return; } - return $this->detachMany([$entityHash]); + $this->detachMany([$entityHash]); } + /** + * @param \Analogue\ORM\Entity $entity + */ public function attachOne($entity) { $wrapper = $this->factory->make($entity); @@ -64,6 +76,9 @@ public function attachOne($entity) $wrapper->setEntityAttribute($this->getPlainForeignKey(), $this->getParentKey()); } + /** + * @param EntityCollection $entities + */ public function attachMany(EntityCollection $entities) { foreach ($entities as $entity) { @@ -71,6 +86,9 @@ public function attachMany(EntityCollection $entities) } } + /** + * @param $entityHash + */ protected function detachOne($entityHash) { $this->detachMany([$entityHash]); @@ -78,7 +96,8 @@ protected function detachOne($entityHash) /** * Attach ids that are passed as arguments, and detach any other - * @param mixed $entities + * @param mixed $entities + * @throws \InvalidArgumentException * @return void */ public function sync(array $entities) @@ -86,6 +105,10 @@ public function sync(array $entities) $this->detachExcept($entities); } + /** + * @param $entities + * @throws \InvalidArgumentException + */ protected function detachExcept($entities) { $query = $this->query->getQuery()->from($this->relatedMap->getTable()); @@ -101,7 +124,9 @@ protected function detachExcept($entities) ->update([$this->getPlainForeignKey() => null]); } - + /** + * @param array $entityHashes + */ public function detachMany(array $entityHashes) { $keys = []; @@ -132,7 +157,7 @@ public function addConstraints() /** * Set the constraints for an eager load of the relation. * - * @param array $entities + * @param array $entities * @return void */ public function addEagerConstraints(array $entities) @@ -143,9 +168,9 @@ public function addEagerConstraints(array $entities) /** * Match the eagerly loaded results to their single parents. * - * @param array $entities - * @param \Analogue\ORM\EntityCollection $results - * @param string $relation + * @param array $entities + * @param EntityCollection $results + * @param string $relation * @return array */ public function matchOne(array $entities, EntityCollection $results, $relation) @@ -156,9 +181,9 @@ public function matchOne(array $entities, EntityCollection $results, $relation) /** * Match the eagerly loaded results to their many parents. * - * @param array $entities - * @param \Analogue\ORM\EntityCollection $results - * @param string $relation + * @param array $entities + * @param EntityCollection $results + * @param string $relation * @return array */ public function matchMany(array $entities, EntityCollection $results, $relation) @@ -169,10 +194,10 @@ public function matchMany(array $entities, EntityCollection $results, $relation) /** * Match the eagerly loaded results to their many parents. * - * @param array $entities - * @param \Analogue\ORM\EntityCollection $results - * @param string $relation - * @param string $type + * @param array $entities + * @param EntityCollection $results + * @param string $relation + * @param string $type * @return array */ protected function matchOneOrMany(array $entities, EntityCollection $results, $relation, $type) @@ -181,10 +206,6 @@ protected function matchOneOrMany(array $entities, EntityCollection $results, $r $cache = $this->parentMapper->getEntityCache(); - // As our cache will hold polymorphic relations, we'll key - // them by entity.key as a standard. - $foreignKey = $this->relatedMap->getKeyName(); - // Once we have the dictionary we can simply spin through the parent models to // link them up with their children using the keyed dictionary to make the // matching very convenient and easy work. Then we'll just return them. @@ -208,9 +229,9 @@ protected function matchOneOrMany(array $entities, EntityCollection $results, $r /** * Get the value of a relationship by one or many type. * - * @param array $dictionary - * @param string $key - * @param string $type + * @param array $dictionary + * @param string $key + * @param string $type * @return mixed */ protected function getRelationValue(array $dictionary, $key, $type) @@ -223,12 +244,12 @@ protected function getRelationValue(array $dictionary, $key, $type) /** * Build model dictionary keyed by the relation's foreign key. * - * @param \Analogue\ORM\EntityCollection $results + * @param EntityCollection $results * @return array */ protected function buildDictionary(EntityCollection $results) { - $dictionary = array(); + $dictionary = []; $foreign = $this->getPlainForeignKey(); @@ -241,7 +262,7 @@ protected function buildDictionary(EntityCollection $results) return $dictionary; } - + /** * Get the key for comparing against the parent key in "has" query. * @@ -291,7 +312,7 @@ public function getParentKey() */ public function getQualifiedParentKeyName() { - return $this->parentMap->getTable().'.'.$this->localKey; + return $this->parentMap->getTable() . '.' . $this->localKey; } /** diff --git a/src/Relationships/MorphMany.php b/src/Relationships/MorphMany.php index a32c381..41aea63 100755 --- a/src/Relationships/MorphMany.php +++ b/src/Relationships/MorphMany.php @@ -1,13 +1,15 @@ -query->first(); - + $this->cacheRelation($result, $relation); return $result; @@ -22,8 +24,8 @@ public function getResults($relation) /** * Initialize the relation on a set of models. * - * @param array $entities - * @param string $relation + * @param array $entities + * @param string $relation * @return array */ public function initRelation(array $entities, $relation) @@ -40,9 +42,9 @@ public function initRelation(array $entities, $relation) /** * Match the eagerly loaded results to their parents. * - * @param array $entities - * @param \Analogue\ORM\Collection $results - * @param string $relation + * @param array $entities + * @param EntityCollection $results + * @param string $relation * @return array */ public function match(array $entities, EntityCollection $results, $relation) diff --git a/src/Relationships/MorphOneOrMany.php b/src/Relationships/MorphOneOrMany.php index bf3be97..dae623d 100755 --- a/src/Relationships/MorphOneOrMany.php +++ b/src/Relationships/MorphOneOrMany.php @@ -1,11 +1,12 @@ -setEntityAttribute($this->getPlainMorphType(), get_class($this->parent)); - $entity->setEntityAttribute($this->getPlainForeignKey(), $this->getParentKey()); - }*/ - /** * Set the base constraints on the relation query. * @@ -61,9 +56,9 @@ public function addConstraints() /** * Get the relationship count query. * - * @param \Analogue\ORM\System\Query $query - * @param \Analogue\ORM\System\Query $parent - * @return \Analogue\ORM\System\Query + * @param Query $query + * @param Query $parent + * @return Query */ public function getRelationCountQuery(Query $query, Query $parent) { @@ -75,7 +70,7 @@ public function getRelationCountQuery(Query $query, Query $parent) /** * Set the constraints for an eager load of the relation. * - * @param array $entities + * @param array $entities * @return void */ public function addEagerConstraints(array $entities) diff --git a/src/Relationships/MorphPivot.php b/src/Relationships/MorphPivot.php index 55e66ce..c0a5eb5 100755 --- a/src/Relationships/MorphPivot.php +++ b/src/Relationships/MorphPivot.php @@ -1,10 +1,11 @@ -relatedMapper->getManager()->mapper($type); - $map = $mapper->getEntityMap(); - - $key = $map->getKeyName(); + $key = $mapper->getEntityMap()->getKeyName(); $query = $mapper->getQuery(); - //$query = $this->useWithTrashed($query); - return $query->whereIn($key, $this->gatherKeysByType($type)->all())->get(); } /** * Gather all of the foreign keys for a given type. * - * @param string $type - * @return array + * @param string $type + * @return BaseCollection */ protected function gatherKeysByType($type) { @@ -169,16 +166,6 @@ protected function gatherKeysByType($type) })->unique(); } - /** - * Get the foreign key of the relationship. - * - * @return string - */ - /*public function getForeignKey() - { - return $this->foreignKey; - }*/ - /** * Associate the model instance to the given parent. * @@ -193,7 +180,7 @@ public function associate($entity) //$this->parent->setEntityAttribute($this->foreignKey, $entity->getEntityAttribute($this->otherKey)); // // Instead, we'll just add the object to the Entity's attribute - + $this->parent->setEntityAttribute($this->relation, $entity); } diff --git a/src/Relationships/MorphToMany.php b/src/Relationships/MorphToMany.php index 329900c..74e88a0 100755 --- a/src/Relationships/MorphToMany.php +++ b/src/Relationships/MorphToMany.php @@ -1,11 +1,12 @@ -inverse = $inverse; - $this->morphType = $name.'_type'; + $this->morphType = $name . '_type'; $this->morphClass = $inverse ? $mapper->getEntityMap()->getClass() : get_class($parent); @@ -58,13 +58,13 @@ public function __construct(Mapper $mapper, $parent, $name, $table, $foreignKey, /** * Set the where clause for the relation query. * - * @return $this + * @return self */ protected function setWhere() { parent::setWhere(); - $this->query->where($this->table.'.'.$this->morphType, $this->morphClass); + $this->query->where($this->table . '.' . $this->morphType, $this->morphClass); return $this; } @@ -72,35 +72,35 @@ protected function setWhere() /** * Add the constraints for a relationship count query. * - * @param \Analogue\ORM\Query $query - * @param \Analogue\ORM\Query $parent - * @return \Analogue\ORM\Query + * @param Query $query + * @param Query $parent + * @return Query */ public function getRelationCountQuery(Query $query, Query $parent) { $query = parent::getRelationCountQuery($query, $parent); - return $query->where($this->table.'.'.$this->morphType, $this->morphClass); + return $query->where($this->table . '.' . $this->morphType, $this->morphClass); } /** * Set the constraints for an eager load of the relation. * - * @param array $entities + * @param array $entities * @return void */ public function addEagerConstraints(array $entities) { parent::addEagerConstraints($entities); - $this->query->where($this->table.'.'.$this->morphType, $this->morphClass); + $this->query->where($this->table . '.' . $this->morphType, $this->morphClass); } /** * Create a new pivot attachment record. * - * @param int $id - * @param bool $timed + * @param int $id + * @param bool $timed * @return array */ protected function createAttachRecord($id, $timed) @@ -113,6 +113,7 @@ protected function createAttachRecord($id, $timed) /** * Create a new query builder for the pivot table. * + * @throws \InvalidArgumentException * @return \Illuminate\Database\Query\Builder */ protected function newPivotQuery() @@ -125,17 +126,17 @@ protected function newPivotQuery() /** * Create a new pivot model instance. * - * @param array $attributes - * @param bool $exists - * @return \Analogue\ORM\Relationships\Pivot + * @param array $attributes + * @param bool $exists + * @return Pivot */ - public function newPivot(array $attributes = array(), $exists = false) + public function newPivot(array $attributes = [], $exists = false) { $pivot = new MorphPivot($this->parent, $this->parentMap, $attributes, $this->table, $exists); $pivot->setPivotKeys($this->foreignKey, $this->otherKey) - ->setMorphType($this->morphType) - ->setMorphClass($this->morphClass); + ->setMorphType($this->morphType) + ->setMorphClass($this->morphClass); return $pivot; } diff --git a/src/Relationships/Pivot.php b/src/Relationships/Pivot.php index ca01bc7..8b1007d 100755 --- a/src/Relationships/Pivot.php +++ b/src/Relationships/Pivot.php @@ -1,10 +1,14 @@ -factory->make($value); } @@ -267,7 +270,7 @@ protected function getKeys(array $entities, $key = null) /** * Get the underlying query for the relation. * - * @return \Analogue\ORM\System\Query + * @return Query */ public function getQuery() { @@ -277,7 +280,7 @@ public function getQuery() /** * Get the base query builder * - * @return \Illuminate\Database\Query\Builder + * @return \Analogue\ORM\Drivers\QueryAdapter */ public function getBaseQuery() { @@ -287,7 +290,7 @@ public function getBaseQuery() /** * Get the parent model of the relation. * - * @return Mappable + * @return InternallyMappable */ public function getParent() { @@ -317,7 +320,7 @@ public function getRelated() /** * Get the related mapper for the relation * - * @return \Analogue\ORM\System\Mapper + * @return Mapper */ public function getRelatedMapper() { @@ -358,7 +361,7 @@ public function relatedUpdatedAt() /** * Wrap the given value with the parent query's grammar. * - * @param string $value + * @param string $value * @return string */ public function wrap($value) @@ -369,7 +372,7 @@ public function wrap($value) /** * Get a fresh timestamp * - * @return \Carbon\Carbon + * @return Carbon */ protected function freshTimestamp() { @@ -380,9 +383,8 @@ protected function freshTimestamp() * Cache the link between parent and related * into the mapper's Entity Cache. * - * @param EntityCollection|Mappable $results result of the relation query - * @param string $relation Name of the relation method on the parent entity - * + * @param EntityCollection|Mappable $results result of the relation query + * @param string $relation name of the relation method on the parent entity * @return void */ protected function cacheRelation($results, $relation) @@ -411,19 +413,17 @@ public function getPivotAttributes() protected function getEntityHash(Mappable $entity) { $class = get_class($entity); - + $keyName = Mapper::getMapper($class)->getEntityMap()->getKeyName(); - - $hash = $class.'.'.$entity->getEntityAttribute($keyName); - return $hash; + return $class . '.' . $entity->getEntityAttribute($keyName); } /** * Run synchronization content if needed by the * relation type. * - * @param array $actualContent + * @param array $actualContent * @return void */ public function sync(array $actualContent) @@ -434,13 +434,13 @@ public function sync(array $actualContent) /** * Handle dynamic method calls to the relationship. * - * @param string $method - * @param array $parameters + * @param string $method + * @param array $parameters * @return mixed */ public function __call($method, $parameters) { - $result = call_user_func_array(array($this->query, $method), $parameters); + $result = call_user_func_array([$this->query, $method], $parameters); if ($result === $this->query) { return $this; diff --git a/src/Repository.php b/src/Repository.php index 904c3e9..cd5ca0d 100755 --- a/src/Repository.php +++ b/src/Repository.php @@ -1,15 +1,20 @@ -mapper = $mapper; } else { - new InvalidArgumentException('Repository class constuctor need a valid Mapper or Mappable object.'); + new InvalidArgumentException('Repository class constructor need a valid Mapper or Mappable object.'); } } @@ -85,8 +90,8 @@ public function allMatching(array $attributes) /** * Return a paginator instance on the EntityCollection * - * @param int $perPage number of item per page (fallback on default setup in entity map) - * @return + * @param int|null $perPage number of item per page (fallback on default setup in entity map) + * @return \Illuminate\Pagination\LengthAwarePaginator */ public function paginate($perPage = null) { @@ -96,8 +101,10 @@ public function paginate($perPage = null) /** * Delete an entity or an entity collection from the database * - * @param Mappable|Collection $entity - * @return null + * @param Mappable|EntityCollection $entity + * @throws MappingException + * @throws \InvalidArgumentException + * @return \Illuminate\Support\Collection|null */ public function delete($entity) { @@ -107,8 +114,10 @@ public function delete($entity) /** * Persist an entity or an entity collection in the database. * - * @param Mappable|Collection|array $entity - * @return Mappable|Collection|array + * @param Mappable|EntityCollection|array $entity + * @throws MappingException + * @throws \InvalidArgumentException + * @return Mappable|EntityCollection|array */ public function store($entity) { @@ -118,16 +127,17 @@ public function store($entity) /** * Make custom mapper custom commands available in repository * - * @param string $method - * @param array $parameters + * @param string $method + * @param array $parameters + * @throws Exception * @return mixed */ public function __call($method, $parameters) { if ($this->mapper->hasCustomCommand($method)) { - call_user_func_array(array($this->mapper, $method), $parameters); + call_user_func_array([$this->mapper, $method], $parameters); } else { - throw new Exception("No method $method on ".get_class($this)); + throw new Exception("No method $method on " . get_class($this)); } } } diff --git a/src/System/Aggregate.php b/src/System/Aggregate.php index a4673c2..aadb328 100755 --- a/src/System/Aggregate.php +++ b/src/System/Aggregate.php @@ -1,8 +1,10 @@ -hasAttribute($relation)) { + if (!$this->hasAttribute($relation)) { // If no attribute exists for this relationships // we'll make it a simple empty array. This will // save us from constantly checking for the attributes @@ -151,11 +155,12 @@ protected function parseForCommonValues($relation) * Parse a 'single' relationship * * @param string $relation - * @return void|boolean + * @throws MappingException + * @return boolean */ protected function parseSingleRelationship($relation) { - if (! $value = $this->parseForCommonValues($relation)) { + if (!$value = $this->parseForCommonValues($relation)) { return true; } @@ -163,7 +168,7 @@ protected function parseSingleRelationship($relation) throw new MappingException("Entity's attribute $relation should not be array, or collection"); } - if ($value instanceof EntityProxy && ! $value->isLoaded()) { + if ($value instanceof EntityProxy && !$value->isLoaded()) { $this->relationships[$relation] = []; return true; } @@ -182,7 +187,7 @@ protected function parseSingleRelationship($relation) // At this point, we can assume the attribute is an Entity instance // so we'll treat it as such. $subAggregate = $this->createSubAggregate($value, $relation); - + // Even if it's a single entity, we'll store it as an array // just for consistency with other relationships $this->relationships[$relation] = [$subAggregate]; @@ -194,19 +199,18 @@ protected function parseSingleRelationship($relation) * Check if value isn't parent or root in the aggregate * * @param mixed - * @return boolean + * @return boolean|null */ protected function isParentOrRoot($value) { - if (! is_null($this->root)) { + if (!is_null($this->root)) { $rootClass = get_class($this->root->getEntityObject()); if ($rootClass == get_class($value)) { return true; } } - - if (! is_null($this->parent)) { + if (!is_null($this->parent)) { $parentClass = get_class($this->parent->getEntityObject()); if ($parentClass == get_class($value)) { return true; @@ -218,11 +222,12 @@ protected function isParentOrRoot($value) * Parse a 'many' relationship * * @param string $relation + * @throws MappingException * @return boolean */ protected function parseManyRelationship($relation) { - if (! $value = $this->parseForCommonValues($relation)) { + if (!$value = $this->parseForCommonValues($relation)) { return true; } @@ -237,13 +242,13 @@ protected function parseManyRelationship($relation) $value = $value->getUnderlyingCollection(); } - if ($value instanceof CollectionProxy && ! $value->isLoaded()) { + if ($value instanceof CollectionProxy && !$value->isLoaded()) { $value = $value->getAddedItems(); } // At this point $value should be either an array or an instance // of a collection class. - if (! is_array($value) && ! $value instanceof Collection) { + if (!is_array($value) && !$value instanceof Collection) { throw new MappingException("'$relation' attribute should be array() or Collection"); } @@ -256,8 +261,8 @@ protected function parseManyRelationship($relation) * Return Entity's relationship attribute * * @param string $relation + * @throws MappingException * @return mixed - * @throws \Analogue\ORM\Exceptions\MappingException */ protected function getRelationshipValue($relation) { @@ -274,7 +279,8 @@ protected function getRelationshipValue($relation) * Create a child, aggregated entity * * @param mixed $entities - * @return + * @param string $relation + * @return array */ protected function createSubAggregates($entities, $relation) { @@ -291,7 +297,9 @@ protected function createSubAggregates($entities, $relation) * Create a related subAggregate * * @param mixed $entity - * @return \Analogue\ORM\System\RootAggregate; + * @param string $relation + * @throws MappingException + * @return self */ protected function createSubAggregate($entity, $relation) { @@ -302,9 +310,7 @@ protected function createSubAggregate($entity, $relation) $root = $this->root; } - $aggregate = new Aggregate($entity, $this, $relation, $root); - - return $aggregate; + return new self($entity, $this, $relation, $root); } /** @@ -344,13 +350,13 @@ public function getEntityMap() */ public function getEntityHash() { - return $this->getEntityClass().'.'.$this->getEntityId(); + return $this->getEntityClass() . '.' . $this->getEntityId(); } /** * Get wrapped entity class * - * @return + * @return string */ public function getEntityClass() { @@ -395,7 +401,7 @@ public function getPivotAttributes() /** * Get Non existing related entities from several relationships * - * @param array $relationships + * @param array $relationships * @return array */ public function getNonExistingRelated(array $relationships) @@ -422,7 +428,7 @@ protected function getNonExistingFromRelation($relation) $nonExisting = []; foreach ($this->relationships[$relation] as $aggregate) { - if (! $aggregate->exists()) { + if (!$aggregate->exists()) { $nonExisting[] = $aggregate; } } @@ -432,8 +438,6 @@ protected function getNonExistingFromRelation($relation) /** * Synchronize relationships if needed - * - * @return */ public function syncRelationships() { @@ -447,7 +451,7 @@ public function syncRelationships() /** * Synchronize a relationship attribute * - * @return void + * @param $relation */ protected function synchronize($relation) { @@ -467,11 +471,11 @@ public function getMissingEntities($relation) { $cachedRelations = $this->getCachedAttribute($relation); - if (! is_null($cachedRelations)) { + if (!is_null($cachedRelations)) { $missing = []; foreach ($cachedRelations as $hash) { - if (! $this->getRelatedAggregateFromHash($hash, $relation)) { + if (!$this->getRelatedAggregateFromHash($hash, $relation)) { $missing[] = $hash; } } @@ -493,7 +497,7 @@ public function getDirtyRelationships() foreach ($this->relationships as $relation) { foreach ($relation as $aggregate) { - if (! $aggregate->exists() || $aggregate->isDirty() || count($aggregate->getDirtyRelationships() > 0)) { + if (!$aggregate->exists() || $aggregate->isDirty() || count($aggregate->getDirtyRelationships() > 0)) { $dirtyAggregates[] = $aggregate; } } @@ -562,7 +566,7 @@ protected function flattenEmbeddables($attributes) $prefix = snake_case(class_basename($embed)); foreach ($valueObjectAttributes as $key=>$value) { - $valueObjectAttributes[$prefix.'_'.$key] = $value; + $valueObjectAttributes[$prefix . '_' . $key] = $value; unset($valueObjectAttributes[$key]); } @@ -576,6 +580,7 @@ protected function flattenEmbeddables($attributes) * Return's entity raw attributes in the state they were at last * query. * + * @param array|null $columns * @return array */ protected function getCachedRawAttributes(array $columns = null) @@ -598,7 +603,7 @@ protected function getCachedAttribute($key) { $cachedAttributes = $this->getCache()->get($this->getEntityId()); - if (! array_key_exists($key, $cachedAttributes)) { + if (!array_key_exists($key, $cachedAttributes)) { return null; } else { return $cachedAttributes[$key]; @@ -622,7 +627,7 @@ protected function getForeignKeyAttributes() } } - if (! is_null($this->parent)) { + if (!is_null($this->parent)) { $foreignKeys = $foreignKeys + $this->getForeignKeyAttributesFromParent(); } @@ -656,7 +661,6 @@ protected function getForeignKeyAttributesFromRelation($relation) * Get foreign key attribute(s) from a parent entity in this * aggregate context * - * @param string $relation * @return array */ protected function getForeignKeyAttributesFromParent() @@ -669,7 +673,8 @@ protected function getForeignKeyAttributesFromParent() $parentRelation = $this->parentRelationship; if (in_array($parentRelation, $parentForeignRelations) - && ! in_array($parentRelation, $parentPivotRelations)) { + && !in_array($parentRelation, $parentPivotRelations) + ) { $parentObject = $this->parent->getEntityObject(); // Call Relationship's method on parent map @@ -722,8 +727,6 @@ protected function updatePivotRelation($relation) } if (count($new) > 0) { - $relatedCollection = $this->getEntityAttribute($relation); - $pivots = $this->getRelatedAggregatesFromHashes($new, $relation); $this->entityMap->$relation($this->getEntityObject())->createPivots($pivots); @@ -756,7 +759,7 @@ protected function updatePivotIfDirty($pivotHash, $relation) $actualPivotAttributes = array_only($pivot, array_keys($cachedPivotAttributes)); $dirty = $this->getDirtyAttributes($actualPivotAttributes, $cachedPivotAttributes); - + if (count($dirty) > 0) { $id = $aggregate->getEntityId(); @@ -768,8 +771,8 @@ protected function updatePivotIfDirty($pivotHash, $relation) /** * Compare two attributes array and return dirty attributes * - * @param array $actual - * @param array $cached + * @param array $actual + * @param array $cached * @return array */ protected function getDirtyAttributes(array $actual, array $cached) @@ -777,7 +780,7 @@ protected function getDirtyAttributes(array $actual, array $cached) $dirty = []; foreach ($actual as $key => $value) { - if (! $this->originalIsNumericallyEquivalent($value, $cached[$key])) { + if (!$this->originalIsNumericallyEquivalent($value, $cached[$key])) { $dirty[$key] = $actual[$key]; } } @@ -818,7 +821,7 @@ protected function getRelatedAggregatesFromHashes(array $hashes, $relation) foreach ($hashes as $hash) { $aggregate = $this->getRelatedAggregateFromHash($hash, $relation); - if (! is_null($aggregate)) { + if (!is_null($aggregate)) { $related[] = $aggregate; } } @@ -831,7 +834,7 @@ protected function getRelatedAggregatesFromHashes(array $hashes, $relation) * * @param string $hash * @param string $relation - * @return \Analogue\ORM\System\Aggregate | null + * @return \Analogue\ORM\System\Aggregate|null */ protected function getRelatedAggregateFromHash($hash, $relation) { @@ -896,10 +899,10 @@ public function getDirtyRawAttributes() continue; } - if (! array_key_exists($key, $cachedAttributes) && ! $value instanceof Pivot) { + if (!array_key_exists($key, $cachedAttributes) && !$value instanceof Pivot) { $dirty[$key] = $value; } elseif ($value !== $cachedAttributes[$key] && - ! $this->originalIsNumericallyEquivalent($value, $cachedAttributes[$key])) { + !$this->originalIsNumericallyEquivalent($value, $cachedAttributes[$key])) { $dirty[$key] = $value; } } @@ -907,6 +910,10 @@ public function getDirtyRawAttributes() return $dirty; } + /** + * @param $key + * @return bool + */ protected function isRelation($key) { return in_array($key, $this->entityMap->getRelationships()); @@ -915,6 +922,8 @@ protected function isRelation($key) /** * Determine if the new and old values for a given key are numerically equivalent. * + * @param $current + * @param $original * @return boolean */ protected function originalIsNumericallyEquivalent($current, $original) @@ -1006,11 +1015,9 @@ public function hasAttribute($key) /** * Set the lazyloading proxies on the wrapped entity - * - * @return void */ public function setProxies() { - return $this->wrappedEntity->setProxies(); + $this->wrappedEntity->setProxies(); } } diff --git a/src/System/CachedRelationship.php b/src/System/CachedRelationship.php index c0267f7..548f932 100755 --- a/src/System/CachedRelationship.php +++ b/src/System/CachedRelationship.php @@ -1,4 +1,6 @@ -hash = $hash; @@ -34,7 +40,7 @@ public function __construct($hash, $pivotAttributes = []) */ public function hasPivotAttributes() { - return count($this->pivotAttributes) > 0 ? true : false; + return count($this->pivotAttributes) > 0; } /** diff --git a/src/System/EntityBuilder.php b/src/System/EntityBuilder.php index 3382730..cbfa990 100755 --- a/src/System/EntityBuilder.php +++ b/src/System/EntityBuilder.php @@ -1,6 +1,7 @@ -mapper = $mapper; @@ -62,13 +67,12 @@ public function __construct(Mapper $mapper, array $eagerLoads) /** * Convert a result set into an array of entities * - * @param array $results - * + * @param array $results * @return array */ public function build(array $results) { - $entities = array(); + $entities = []; //$prototype = $this->getWrapperPrototype(); //$prototype = $this->mapper->newInstance(); @@ -83,7 +87,7 @@ public function build(array $results) $resultArray = (array) $result; - $tmpCache[$resultArray[$keyName] ] = $resultArray; + $tmpCache[$resultArray[$keyName]] = $resultArray; // Hydrate any embedded Value Object $this->hydrateValueObjects($resultArray); @@ -108,7 +112,8 @@ public function build(array $results) /** * Get the correct wrapper prototype corresponding to the object type * - * @return mixed + * @throws \Analogue\ORM\Exceptions\MappingException + * @return InternallyMappable */ protected function getWrapperInstance() { @@ -119,6 +124,7 @@ protected function getWrapperInstance() * Hydrate value object embedded in this entity * * @param array $attributes + * @throws \Analogue\ORM\Exceptions\MappingException * @return void */ protected function hydrateValueObjects(& $attributes) @@ -131,9 +137,10 @@ protected function hydrateValueObjects(& $attributes) /** * Hydrate a single value object * - * @param array $attributes + * @param array $attributes * @param string $localKey * @param string $valueClass + * @throws \Analogue\ORM\Exceptions\MappingException * @return void */ protected function hydrateValueObject(& $attributes, $localKey, $valueClass) @@ -142,18 +149,16 @@ protected function hydrateValueObject(& $attributes, $localKey, $valueClass) $embeddedAttributes = $map->getAttributes(); - //$nestedValueObjects = $map->getEmbeddables(); - $valueObject = $this->mapper->getManager()->getValueObjectInstance($valueClass); foreach ($embeddedAttributes as $key) { - $prefix = snake_case(class_basename($valueClass)).'_'; + $prefix = snake_case(class_basename($valueClass)) . '_'; $voWrapper = $this->factory->make($valueObject); - $voWrapper->setEntityAttribute($key, $attributes[$prefix.$key]); + $voWrapper->setEntityAttribute($key, $attributes[$prefix . $key]); - unset($attributes[$prefix.$key]); + unset($attributes[$prefix . $key]); } $attributes[$localKey] = $valueObject; @@ -174,7 +179,7 @@ protected function prepareLazyLoading() /** * Build lazy loading proxies for the current entity * - * @param \Analogue\ORM\Mappable $entity + * @param InternallyMappable $entity * * @return array */ diff --git a/src/System/EntityCache.php b/src/System/EntityCache.php index 1ce104c..9ad2a15 100755 --- a/src/System/EntityCache.php +++ b/src/System/EntityCache.php @@ -6,10 +6,8 @@ use Analogue\ORM\EntityMap; use Analogue\ORM\EntityCollection; use Analogue\ORM\System\Wrappers\Factory; -use Analogue\ORM\System\InternallyMappable; use Analogue\ORM\Relationships\Relationship; use Analogue\ORM\Exceptions\MappingException; -use Analogue\ORM\System\Proxies\ProxyInterface; /** * The EntityCache class is responsible for tracking entity's attribute states @@ -17,7 +15,6 @@ */ class EntityCache { - /** * Entity's raw attributes/relationships * @@ -46,6 +43,10 @@ class EntityCache */ protected $pivotAttributes = []; + /** + * EntityCache constructor. + * @param EntityMap $entityMap + */ public function __construct(EntityMap $entityMap) { $this->entityMap = $entityMap; @@ -104,33 +105,25 @@ public function has($id) protected function mergeCacheResults($entities) { foreach ($entities as $key => $entity) { - if (array_key_exists($key, $this->cache)) { - /* - $existingValue = $this->cache[$key]; - - $this->cache[$key] = $entity + $existingValue;*/ - - $this->cache[$key] = $entity; - } else { - $this->cache[$key] = $entity; - } + $this->cache[$key] = $entity; } } /** * Cache Relation's query result for an entity * - * @param mixed $parent - * @param string $relation name of the relation - * @param mixed $results results of the relationship's query - * + * @param mixed $parent + * @param string $relation name of the relation + * @param mixed $results results of the relationship's query + * @param Relationship $relationship + * @throws MappingException * @return void */ public function cacheLoadedRelationResult($parent, $relation, $results, Relationship $relationship) { $keyName = $this->entityMap->getKeyName(); - if (! $parent instanceof InternallyMappable) { + if (!$parent instanceof InternallyMappable) { $parent = $this->factory->make($parent); } @@ -150,17 +143,18 @@ public function cacheLoadedRelationResult($parent, $relation, $results, Relation /** * Create a cachedRelationship instance which will hold related entity's hash and pivot attributes, if any. * - * @param [type] $parentKey [description] - * @param [type] $relation [description] - * @param [type] $result [description] - * @param Relationship $relationship [description] - * @return [type] [description] + * @param $parentKey + * @param string $relation + * @param $result + * @param Relationship $relationship + * @throws MappingException + * @return CachedRelationship */ protected function getCachedRelationship($parentKey, $relation, $result, Relationship $relationship) { $pivotColumns = $relationship->getPivotAttributes(); - if (! array_key_exists($relation, $this->pivotAttributes)) { + if (!array_key_exists($relation, $this->pivotAttributes)) { $this->pivotAttributes[$relation] = $pivotColumns; } @@ -189,11 +183,11 @@ protected function getCachedRelationship($parentKey, $relation, $result, Relatio /** * Cache a many relationship * - * @param [type] $parentKey [description] - * @param [type] $relation [description] - * @param [type] $results [description] - * @param Relationship $relationship [description] - * @return [type] [description] + * @param $parentKey + * @param string $relation + * @param EntityCollection $results + * @param Relationship $relationship + * @throws MappingException */ protected function cacheManyRelationResults($parentKey, $relation, $results, Relationship $relationship) { @@ -211,11 +205,11 @@ protected function cacheManyRelationResults($parentKey, $relation, $results, Rel /** * Cache a single relationship * - * @param [type] $parentKey [description] - * @param [type] $relation [description] - * @param [type] $results [description] - * @param Relationship $relationship [description] - * @return [type] [description] + * @param $parentKey + * @param string $relation + * @param Mappable $result + * @param Relationship $relationship + * @throws MappingException */ protected function cacheSingleRelationResult($parentKey, $relation, $result, Relationship $relationship) { @@ -226,6 +220,7 @@ protected function cacheSingleRelationResult($parentKey, $relation, $result, Rel * Get Entity's Hash * * @param $entity + * @throws MappingException * @return string */ protected function getEntityHash(InternallyMappable $entity) @@ -236,14 +231,12 @@ protected function getEntityHash(InternallyMappable $entity) $keyName = $mapper->getEntityMap()->getKeyName(); - return $class.'.'.$entity->getEntityAttribute($keyName); + return $class . '.' . $entity->getEntityAttribute($keyName); } /** * Refresh the cache record for an aggregated entity after a write operation - * - * @param InternallyMappable $entity [description] - * @return [type] [description] + * @param Aggregate $entity */ public function refresh(Aggregate $entity) { @@ -253,7 +246,8 @@ public function refresh(Aggregate $entity) /** * Transform an Aggregated Entity into a cache record * - * @param Aggregate $entity + * @param Aggregate $aggregatedEntity + * @throws MappingException * @return array */ protected function transform(Aggregate $aggregatedEntity) @@ -265,7 +259,7 @@ protected function transform(Aggregate $aggregatedEntity) // First we'll handle each relationships that are a one to one // relation, and which will be saved as a CachedRelationship // object inside the cache. - + // NOTE : storing localRelationships maybe useless has we store // the foreign key in the attributes already. @@ -299,7 +293,12 @@ protected function transform(Aggregate $aggregatedEntity) return $baseAttributes + $relationAttributes; } - + + /** + * @param $relation + * @param InternallyMappable $entity + * @return array + */ protected function getPivotValues($relation, InternallyMappable $entity) { $values = []; diff --git a/src/System/InternallyMappable.php b/src/System/InternallyMappable.php index c072016..b8c9e51 100755 --- a/src/System/InternallyMappable.php +++ b/src/System/InternallyMappable.php @@ -1,8 +1,9 @@ - entityMap associations + * This class keeps track of instantiated mappers, and entity <-> entityMap associations */ class Manager { - /** * Driver Manager * @@ -72,8 +69,18 @@ class Manager * * @var array */ - protected $events = ['initializing', 'initialized', 'store', 'stored', - 'creating', 'created', 'updating', 'updated', 'deleting', 'deleted' ]; + protected $events = [ + 'initializing', + 'initialized', + 'store', + 'stored', + 'creating', + 'created', + 'updating', + 'updated', + 'deleting', + 'deleted', + ]; /** * @param \Analogue\ORM\Drivers\Manager $driverManager @@ -101,8 +108,9 @@ public function getDriverManager() /** * Create a mapper for a given entity * - * @param \Analogue\ORM\Mappable|string $entity - * @param mixed $entityMap + * @param \Analogue\ORM\Mappable|string $entity + * @param mixed $entityMap + * @throws MappingException * @return Mapper */ public function mapper($entity, $entityMap = null) @@ -111,7 +119,7 @@ public function mapper($entity, $entityMap = null) throw new MappingException('Tried to instantiate mapper on wrapped Entity'); } - if (! is_string($entity)) { + if (!is_string($entity)) { $entity = get_class($entity); } @@ -126,15 +134,16 @@ public function mapper($entity, $entityMap = null) /** * Build a new Mapper instance for a given Entity * - * @param mixed|string $entity - * @param $entityMap + * @param string $entity + * @param $entityMap + * @throws MappingException * @return Mapper */ protected function buildMapper($entity, $entityMap) { // If an EntityMap hasn't been manually registered by the user // register it at runtime. - if (! $this->isRegisteredEntity($entity)) { + if (!$this->isRegisteredEntity($entity)) { $this->register($entity, $entityMap); } @@ -147,8 +156,8 @@ protected function buildMapper($entity, $entityMap) $this->mappers[$entity] = $mapper; // At this point we can safely call the boot() method on the entityMap as - // the mapper is now instanciated & registered within the manager. - + // the mapper is now instantiated & registered within the manager. + $mapper->getEntityMap()->boot(); return $mapper; @@ -157,8 +166,9 @@ protected function buildMapper($entity, $entityMap) /** * Create a mapper for a given entity (static alias) * - * @param \Analogue\ORM\Mappable|string $entity - * @param mixed $entityMap + * @param \Analogue\ORM\Mappable|string $entity + * @param null|EntityMap $entityMap + * @throws MappingException * @return Mapper */ public static function getMapper($entity, $entityMap = null) @@ -170,11 +180,13 @@ public static function getMapper($entity, $entityMap = null) * Get the Repository instance for the given Entity * * @param \Analogue\ORM\Mappable|string $entity + * @throws \InvalidArgumentException + * @throws MappingException * @return \Analogue\ORM\Repository */ public function repository($entity) { - if (! is_string($entity)) { + if (!is_string($entity)) { $entity = get_class($entity); } @@ -191,14 +203,15 @@ public function repository($entity) /** * Register an entity * - * @param string|Mappable $entity entity's class name - * @param string|EntityMap $entityMap map's class name + * @param string|\Analogue\ORM\Mappable $entity entity's class name + * @param string|EntityMap $entityMap map's class name + * @throws MappingException * @return void */ public function register($entity, $entityMap = null) { // If an object is provider, get the class name from it - if (! is_string($entity)) { + if (!is_string($entity)) { $entity = get_class($entity); } @@ -206,7 +219,7 @@ public function register($entity, $entityMap = null) throw new MappingException("Entity $entity is already registered."); } - if (! class_exists($entity)) { + if (!class_exists($entity)) { throw new MappingException("Class $entity does not exists"); } @@ -218,8 +231,8 @@ public function register($entity, $entityMap = null) $entityMap = new $entityMap; } - if (! $entityMap instanceof EntityMap) { - throw new MappingException(get_class($entityMap)." must be an instance of EntityMap."); + if (!$entityMap instanceof EntityMap) { + throw new MappingException(get_class($entityMap) . ' must be an instance of EntityMap.'); } $entityMap->setClass($entity); @@ -232,16 +245,16 @@ public function register($entity, $entityMap = null) /** * Get the entity map instance for a custom entity * - * @param string $entity - * @return Mappable + * @param string $entity + * @return \Analogue\ORM\Mappable */ protected function getEntityMapInstanceFor($entity) { - if (class_exists($entity.'Map')) { - $map = $entity.'Map'; + if (class_exists($entity . 'Map')) { + $map = $entity . 'Map'; $map = new $map; } else { - // Generate an EntityMap obeject + // Generate an EntityMap object $map = $this->getNewEntityMap(); } @@ -261,21 +274,22 @@ protected function getNewEntityMap() /** * Register a Value Object * - * @param string|ValueObject $valueObject + * @param string $valueObject * @param string $valueMap + * @throws MappingException * @return void */ public function registerValueObject($valueObject, $valueMap = null) { - if (! is_string($valueObject)) { + if (!is_string($valueObject)) { $valueObject = get_class($valueObject); } if (is_null($valueMap)) { - $valueMap = $valueObject.'Map'; + $valueMap = $valueObject . 'Map'; } - if (! class_exists($valueMap)) { + if (!class_exists($valueMap)) { throw new MappingException("$valueMap doesn't exists"); } @@ -285,12 +299,12 @@ public function registerValueObject($valueObject, $valueMap = null) /** * Return true is the object is registered as value object * - * @param mixed $object + * @param mixed $object * @return boolean */ public function isValueObject($object) { - if (! is_string($object)) { + if (!is_string($object)) { $object = get_class($object); } @@ -301,15 +315,16 @@ public function isValueObject($object) * Get the Value Map for a given Value Object Class * * @param string $valueObject + * @throws MappingException * @return \Analogue\ORM\ValueMap */ public function getValueMap($valueObject) { - if (! is_string($valueObject)) { + if (!is_string($valueObject)) { $valueObject = get_class($valueObject); } - if (! array_key_exists($valueObject, $this->valueClasses)) { + if (!array_key_exists($valueObject, $this->valueClasses)) { $this->registerValueObject($valueObject); } $valueMap = new $this->valueClasses[$valueObject]; @@ -320,18 +335,14 @@ public function getValueMap($valueObject) } /** - * Instanciate a new Value Object instance + * Instantiate a new Value Object instance * * @param string $valueObject - * @return ValueObject + * @return \Analogue\ORM\ValueObject */ public function getValueObjectInstance($valueObject) { - $prototype = unserialize(sprintf('O:%d:"%s":0:{}', - strlen($valueObject), - $valueObject - ) - ); + $prototype = unserialize(sprintf('O:%d:"%s":0:{}', strlen($valueObject), $valueObject)); return $prototype; } @@ -353,12 +364,12 @@ public function registerPlugin($plugin) /** * Check if the entity is already registered * - * @param string|object $entity + * @param string|object $entity * @return boolean */ public function isRegisteredEntity($entity) { - if (! is_string($entity)) { + if (!is_string($entity)) { $entity = get_class($entity); } @@ -370,12 +381,13 @@ public function isRegisteredEntity($entity) * of the entity. * * @param string $event - * @param closure|string $callback + * @param \Closure $callback + * @throws \Exception * @return void */ public function registerGlobalEvent($event, $callback) { - if (! in_array($event, $this->events)) { + if (!in_array($event, $this->events)) { throw new \Exception("Analogue : Event $event doesn't exist"); } $this->eventDispatcher->listen("analogue.{$event}.*", $callback); @@ -385,6 +397,7 @@ public function registerGlobalEvent($event, $callback) * Shortcut to Mapper store * * @param mixed $entity + * @throws MappingException * @return mixed */ public function store($entity) @@ -396,7 +409,8 @@ public function store($entity) * Shortcut to Mapper delete * * @param mixed $entity - * @return mixed + * @throws MappingException + * @return \Illuminate\Support\Collection|null */ public function delete($entity) { @@ -407,7 +421,8 @@ public function delete($entity) * Shortcut to Mapper query * * @param mixed $entity - * @return \Analogue\System\Query + * @throws MappingException + * @return Query */ public function query($entity) { @@ -418,7 +433,8 @@ public function query($entity) * Shortcut to Mapper Global Query * * @param mixed $entity - * @return \Analogue\System\Query + * @throws MappingException + * @return Query */ public function globalQuery($entity) { @@ -428,9 +444,7 @@ public function globalQuery($entity) /** * Return the Singleton instance of the manager * - * @param string $method - * @param array $parameters - * @return mixed + * @return Manager */ public static function getInstance() { diff --git a/src/System/Mapper.php b/src/System/Mapper.php index 9e121bf..2fbf25b 100755 --- a/src/System/Mapper.php +++ b/src/System/Mapper.php @@ -1,5 +1,8 @@ -checkEntityType($entity); - + $store = new Store($this->aggregate($entity), $this->newQueryBuilder()); return $store->execute(); @@ -132,20 +139,21 @@ protected function storeEntity($entity) /** * Convert an entity into an aggregate root * - * @param mixed $entity + * @param mixed $entity + * @throws MappingException * @return \Analogue\ORM\System\Aggregate */ protected function aggregate($entity) { - $aggregate = new Aggregate($entity); - - return $aggregate; + return new Aggregate($entity); } /** * Store an entity collection inside a single DB Transaction * - * @param Collection|array $entities [description] + * @param Collection|array $entities + * @throws \InvalidArgumentException + * @throws MappingException * @return Collection */ protected function storeCollection($entities) @@ -165,14 +173,16 @@ protected function storeCollection($entities) * Delete an entity or an entity collection from the database * * @param mixed|Collection - * @return void + * @throws MappingException + * @throws \InvalidArgumentException + * @return Collection|void */ public function delete($entity) { if ($this->isArrayOrCollection($entity)) { return $this->deleteCollection($entity); } else { - return $this->deleteEntity($entity); + $this->deleteEntity($entity); } } @@ -180,6 +190,8 @@ public function delete($entity) * Delete a single entity from the database. * * @param Mappable $entity + * @throws \InvalidArgumentException + * @throws MappingException * @return void */ protected function deleteEntity($entity) @@ -195,6 +207,8 @@ protected function deleteEntity($entity) * Delete an Entity Collection inside a single db transaction * * @param Collection|array $entities + * @throws \InvalidArgumentException + * @throws MappingException * @return Collection */ protected function deleteCollection($entities) @@ -233,18 +247,19 @@ public function getEntityCache() /** * Fire the given event for the entity * - * @param string $event - * @param \Analogue\ORM\Entity $entity - * @param bool $halt + * @param string $event + * @param \Analogue\ORM\Entity $entity + * @param bool $halt + * @throws InvalidArgumentException * @return mixed */ public function fireEvent($event, $entity, $halt = true) { if ($entity instanceof Wrapper) { - throw new InvalidArgumentException("Fired Event with invalid Entity Object"); + throw new InvalidArgumentException('Fired Event with invalid Entity Object'); } - $event = "analogue.{$event}.".$this->entityMap->getClass(); + $event = "analogue.{$event}." . $this->entityMap->getClass(); $method = $halt ? 'until' : 'fire'; @@ -254,8 +269,8 @@ public function fireEvent($event, $entity, $halt = true) /** * Register an entity event with the dispatcher. * - * @param string $event - * @param \Closure|string $callback + * @param string $event + * @param \Closure $callback * @return void */ public function registerEvent($event, $callback) @@ -279,23 +294,23 @@ public function addGlobalScope(ScopeInterface $scope) /** * Determine if the mapper has a global scope. * - * @param \Analogue\ORM\System\ScopeInterface $scope + * @param \Analogue\ORM\System\ScopeInterface $scope * @return bool */ public function hasGlobalScope($scope) { - return ! is_null($this->getGlobalScope($scope)); + return !is_null($this->getGlobalScope($scope)); } /** * Get a global scope registered with the modal. * - * @param \Analogue\ORM\System\ScopeInterface $scope - * @return \Analogue\ORM\System\ScopeInterface |null + * @param \Analogue\ORM\System\ScopeInterface $scope + * @return \Analogue\ORM\System\ScopeInterface|null */ public function getGlobalScope($scope) { - return array_first($this->globalScopes, function ($key, $value) use ($scope) { + return array_first($this->globalScopes, function($key, $value) use ($scope) { return $scope instanceof $value; }); } @@ -313,7 +328,7 @@ public function getGlobalScopes() /** * Apply all of the global scopes to an Analogue Query builder. * - * @param \Analogue\ORM\System\Query $builder + * @param Query $query * @return \Analogue\ORM\System\Query */ public function applyGlobalScopes($query) @@ -328,7 +343,7 @@ public function applyGlobalScopes($query) /** * Remove all of the global scopes from an Analogue Query builder. * - * @param \Analogue\ORM\System\Query $builder + * @param Query $query * @return \Analogue\ORM\System\Query */ public function removeGlobalScopes($query) @@ -343,7 +358,7 @@ public function removeGlobalScopes($query) /** * Get a new query instance without a given scope. * - * @param \Analogue\ORM\System\ScopeInterface $scope + * @param \Analogue\ORM\System\ScopeInterface $scope * @return \Analogue\ORM\System\Query */ public function newQueryWithoutScope($scope) @@ -356,7 +371,7 @@ public function newQueryWithoutScope($scope) /** * Get a new query builder that doesn't have any global scopes. * - * @return Analogue\ORM\System\Query|static + * @return Query */ public function newQueryWithoutScopes() { @@ -378,9 +393,11 @@ public function addCustomCommand($command) /** * Execute a custom command on an Entity * - * @param string $command + * @param string $command * @param mixed|Collection|array $entity - * @return void + * @throws \InvalidArgumentException + * @throws MappingException + * @return mixed */ public function executeCustomCommand($command, $entity) { @@ -398,8 +415,10 @@ public function executeCustomCommand($command, $entity) /** * Execute a single command instance * - * @param string $command + * @param string $commandClass * @param mixed $entity + * @throws \InvalidArgumentException + * @throws MappingException * @return mixed */ protected function executeSingleCustomCommand($commandClass, $entity) @@ -414,10 +433,9 @@ protected function executeSingleCustomCommand($commandClass, $entity) /** * Check that the entity correspond to the current mapper. * - * @param mixed $entity - * @return void - * + * @param mixed $entity * @throws InvalidArgumentException + * @return void */ protected function checkEntityType($entity) { @@ -429,7 +447,7 @@ protected function checkEntityType($entity) } /** - * Get all the custom commands regitered on this mapper + * Get all the custom commands registered on this mapper * * @return array */ @@ -440,7 +458,7 @@ public function getCustomCommands() /** * Check if this mapper supports this command - * @param string $command + * @param string $command * @return boolean */ public function hasCustomCommand($command) @@ -451,10 +469,10 @@ public function hasCustomCommand($command) /** * Create a new instance of the mapped entity class * - * @param array $attributes + * @param array $attributes * @return mixed */ - public function newInstance($attributes = array()) + public function newInstance($attributes = []) { $class = $this->entityMap->getClass(); @@ -476,24 +494,22 @@ public function newInstance($attributes = array()) * Use a trick to generate a class prototype that we * can instantiate without calling the constructor. * + * @param string|null $className + * @throws MappingException * @return mixed */ protected function customClassInstance($className) { - if (! class_exists($className)) { + if (!class_exists($className)) { throw new MappingException("Tried to instantiate a non-existing Entity class : $className"); } - $prototype = unserialize(sprintf('O:%d:"%s":0:{}', - strlen($className), - $className - ) - ); + $prototype = unserialize(sprintf('O:%d:"%s":0:{}', strlen($className), $className)); return $prototype; } /** - * Get the Analogue Query Builer for this instance + * Get the Analogue Query Builder for this instance * * @return \Analogue\ORM\System\Query */ @@ -505,7 +521,7 @@ public function getQuery() } /** - * Get the Analogue Query Builer for this instance + * Get the Analogue Query Builder for this instance * * @return \Analogue\ORM\System\Query */ @@ -515,7 +531,7 @@ public function query() } /** - * Get an unscoped Analogue Query Builer for this instance + * Get an unscoped Analogue Query Builder for this instance * * @return \Analogue\ORM\System\Query */ @@ -547,8 +563,9 @@ public function getManager() /** * Dynamically handle calls to custom commands, or Redirects to query() * - * @param string $method - * @param array $parameters + * @param string $method + * @param array $parameters + * @throws \Exception * @return mixed */ public function __call($method, $parameters) @@ -562,6 +579,6 @@ public function __call($method, $parameters) } // Redirect call on a new query instance - return call_user_func_array(array($this->query(), $method), $parameters); + return call_user_func_array([$this->query(), $method], $parameters); } } diff --git a/src/System/MapperFactory.php b/src/System/MapperFactory.php index 85eae8a..0912383 100755 --- a/src/System/MapperFactory.php +++ b/src/System/MapperFactory.php @@ -1,4 +1,6 @@ -drivers = $drivers; @@ -45,8 +52,8 @@ public function __construct(DriverManager $drivers, Dispatcher $dispatcher, Mana /** * Return a new Mapper instance * - * @param string $entityClass - * @param string $entityMap + * @param string $entityClass + * @param EntityMap $entityMap * @return Mapper */ public function make($entityClass, EntityMap $entityMap) diff --git a/src/System/Proxies/CollectionProxy.php b/src/System/Proxies/CollectionProxy.php index 9ca48c6..422a9e8 100755 --- a/src/System/Proxies/CollectionProxy.php +++ b/src/System/Proxies/CollectionProxy.php @@ -10,9 +10,13 @@ use Illuminate\Contracts\Support\Arrayable; use Analogue\ORM\EntityCollection; +/** + * Class CollectionProxy + * + * @mixin EntityCollection + */ class CollectionProxy extends Proxy implements ArrayAccess, Arrayable, Countable, IteratorAggregate, Jsonable, JsonSerializable { - /** * Underlying Lazyloaded collection * @var EntityCollection @@ -26,8 +30,8 @@ class CollectionProxy extends Proxy implements ArrayAccess, Arrayable, Countable protected $addedItems; /** - * @param mixed $parentEntity - * @param string $relation relationship method handled by the proxy. + * @param mixed $parentEntity + * @param string $relation relationship method handled by the proxy. */ public function __construct($parentEntity, $relation) { @@ -40,6 +44,7 @@ public function __construct($parentEntity, $relation) * Add an entity to the proxy collection, weither it's loaded or not * * @param mixed $entity + * @return self|void */ public function add($entity) { @@ -57,13 +62,13 @@ public function add($entity) */ public function isLoaded() { - return ! is_null($this->loadedCollection); + return !is_null($this->loadedCollection); } /** * Return the underlying collection * - * @return \Analogue\ORM\EntityCollection + * @return EntityCollection */ public function getUnderlyingCollection() { @@ -73,7 +78,7 @@ public function getUnderlyingCollection() /** * Return Items that has been added prior to lazy-loading * - * @return \Analogue\ORM\EntityCollection + * @return EntityCollection */ public function getAddedItems() { @@ -115,7 +120,7 @@ public function count() /** * Determine if an item exists at an offset. * - * @param mixed $key + * @param mixed $key * @return bool */ public function offsetExists($key) @@ -128,7 +133,7 @@ public function offsetExists($key) /** * Get an item at a given offset. * - * @param mixed $key + * @param mixed $key * @return mixed */ public function offsetGet($key) @@ -141,28 +146,26 @@ public function offsetGet($key) /** * Set the item at a given offset. * - * @param mixed $key - * @param mixed $value - * @return void + * @param mixed $key + * @param mixed $value */ public function offsetSet($key, $value) { $this->loadOnce(); - return $this->getUnderlyingCollection()->offsetSet($key, $value); + $this->getUnderlyingCollection()->offsetSet($key, $value); } /** * Unset the item at a given offset. * - * @param string $key - * @return void + * @param string $key */ public function offsetUnset($key) { $this->loadOnce(); - return $this->getUnderlyingCollection()->offsetUnset($key); + $this->getUnderlyingCollection()->offsetUnset($key); } /** @@ -192,7 +195,7 @@ public function jsonSerialize() /** * Get the collection of items as JSON. * - * @param int $options + * @param int $options * @return string */ public function toJson($options = 0) @@ -214,16 +217,15 @@ public function getIterator() return $this->getUnderlyingCollection()->getIterator(); } + /** - * Transparently Redirect non overrided calls to the lazy loaded collection - * - * @param [type] $method [description] - * @param [type] $parameters [description] - * @return [type] [description] + * @param $method + * @param $parameters + * @return mixed */ public function __call($method, $parameters) { - if (! $this->isLoaded()) { + if (!$this->isLoaded()) { $this->loadOnce(); } diff --git a/src/System/Proxies/EntityProxy.php b/src/System/Proxies/EntityProxy.php index a0e34ca..6ae35d9 100755 --- a/src/System/Proxies/EntityProxy.php +++ b/src/System/Proxies/EntityProxy.php @@ -1,8 +1,9 @@ -isLoaded()) { + if (!$this->isLoaded()) { $this->loadOnce(); } @@ -42,7 +43,7 @@ public function getUnderlyingObject() */ public function __get($attribute) { - if (! $this->isLoaded()) { + if (!$this->isLoaded()) { $this->loadOnce(); } @@ -58,7 +59,7 @@ public function __get($attribute) */ public function __set($attribute, $value) { - if (! $this->isLoaded()) { + if (!$this->isLoaded()) { $this->loadOnce(); } @@ -74,7 +75,7 @@ public function __set($attribute, $value) */ public function __call($method, $parameters) { - if (! $this->isLoaded()) { + if (!$this->isLoaded()) { $this->loadOnce(); } diff --git a/src/System/Proxies/Proxy.php b/src/System/Proxies/Proxy.php index 1e610aa..c371d8a 100755 --- a/src/System/Proxies/Proxy.php +++ b/src/System/Proxies/Proxy.php @@ -2,11 +2,11 @@ namespace Analogue\ORM\System\Proxies; +use Analogue\ORM\Exceptions\MappingException; use Analogue\ORM\System\Manager; abstract class Proxy implements ProxyInterface { - /** * The name of the relationship method handled by the proxy. * @@ -17,7 +17,7 @@ abstract class Proxy implements ProxyInterface /** * Reference to parent entity object * - * @var InternallyMappable + * @var \Analogue\ORM\System\InternallyMappable */ protected $parentEntity; @@ -30,7 +30,7 @@ abstract class Proxy implements ProxyInterface /** * @param mixed $parentEntity - * @param string $relation relationship method handled by the proxy. + * @param string $relation relationship method handled by the proxy. */ public function __construct($parentEntity, $relation) { @@ -42,12 +42,13 @@ public function __construct($parentEntity, $relation) /** * Call the relationship method on the underlying entity map * + * @throws MappingException * @return mixed */ public function load() { $entities = $this->query($this->parentEntity, $this->relation)->getResults($this->relation); - + $this->loaded = true; return $entities; @@ -66,9 +67,10 @@ public function isLoaded() /** * Return the Query Builder on the relation * - * @param mixed $entity - * @param string $relation - * @return Query + * @param \Analogue\ORM\System\InternallyMappable $entity + * @param string $relation + * @throws MappingException + * @return \Analogue\ORM\System\Query */ protected function query($entity, $relation) { @@ -80,7 +82,8 @@ protected function query($entity, $relation) /** * Get the mapper instance for the entity * - * @param mixed $entity + * @param \Analogue\ORM\System\InternallyMappable $entity + * @throws MappingException * @return \Analogue\ORM\System\Mapper */ protected function getMapper($entity) diff --git a/src/System/Proxies/ProxyInterface.php b/src/System/Proxies/ProxyInterface.php index d9c92fb..feb89d6 100755 --- a/src/System/Proxies/ProxyInterface.php +++ b/src/System/Proxies/ProxyInterface.php @@ -1,14 +1,14 @@ -getEntities($columns); // If we actually found models we will also eager load any relationships that // have been specified as needing to be eager loaded, which will solve the // n+1 query issue for the developers to avoid running a lot of queries. - + if (count($entities) > 0) { $entities = $this->eagerLoadRelations($entities); } @@ -124,10 +142,10 @@ public function get($columns = array('*')) * Find an entity by its primary key * * @param string|integer $id - * @param array $columns + * @param array $columns * @return \Analogue\ORM\Mappable */ - public function find($id, $columns = array('*')) + public function find($id, $columns = ['*']) { if (is_array($id)) { return $this->findMany($id, $columns); @@ -141,11 +159,11 @@ public function find($id, $columns = array('*')) /** * Find many entities by their primary keys. * - * @param array $id - * @param array $columns + * @param array $id + * @param array $columns * @return EntityCollection */ - public function findMany($id, $columns = array('*')) + public function findMany($id, $columns = ['*']) { if (empty($id)) { return new EntityCollection; @@ -159,15 +177,14 @@ public function findMany($id, $columns = array('*')) /** * Find a model by its primary key or throw an exception. * - * @param mixed $id - * @param array $columns - * @return mixed|static - * - * @throws \Analogue\ORM\Exception\EntityNotFoundException + * @param mixed $id + * @param array $columns + * @throws \Analogue\ORM\Exceptions\EntityNotFoundException + * @return mixed|self */ - public function findOrFail($id, $columns = array('*')) + public function findOrFail($id, $columns = ['*']) { - if (! is_null($entity = $this->find($id, $columns))) { + if (!is_null($entity = $this->find($id, $columns))) { return $entity; } @@ -178,10 +195,10 @@ public function findOrFail($id, $columns = array('*')) /** * Execute the query and get the first result. * - * @param array $columns + * @param array $columns * @return \Analogue\ORM\Entity */ - public function first($columns = array('*')) + public function first($columns = ['*']) { return $this->take(1)->get($columns)->first(); } @@ -189,14 +206,13 @@ public function first($columns = array('*')) /** * Execute the query and get the first result or throw an exception. * - * @param array $columns - * @return Mappable - * + * @param array $columns * @throws EntityNotFoundException + * @return \Analogue\ORM\Entity */ - public function firstOrFail($columns = array('*')) + public function firstOrFail($columns = ['*']) { - if (! is_null($entity = $this->first($columns))) { + if (!is_null($entity = $this->first($columns))) { return $entity; } @@ -206,12 +222,12 @@ public function firstOrFail($columns = array('*')) /** * Pluck a single column from the database. * - * @param string $column + * @param string $column * @return mixed */ public function pluck($column) { - $result = $this->first(array($column)); + $result = $this->first([$column]); if ($result) { return $result->{$column}; @@ -221,8 +237,8 @@ public function pluck($column) /** * Chunk the results of the query. * - * @param int $count - * @param callable $callback + * @param int $count + * @param callable $callback * @return void */ public function chunk($count, callable $callback) @@ -244,8 +260,8 @@ public function chunk($count, callable $callback) /** * Get an array with the values of a given column. * - * @param string $column - * @param string $key + * @param string $column + * @param string $key * @return array */ public function lists($column, $key = null) @@ -256,11 +272,11 @@ public function lists($column, $key = null) /** * Get a paginator for the "select" statement. * - * @param int $perPage - * @param array $columns - * @return \Illuminate\Pagination\Paginator + * @param int $perPage + * @param array $columns + * @return LengthAwarePaginator */ - public function paginate($perPage = null, $columns = array('*')) + public function paginate($perPage = null, $columns = ['*']) { $total = $this->query->getCountForPagination(); @@ -277,9 +293,9 @@ public function paginate($perPage = null, $columns = array('*')) /** * Get a paginator for a grouped statement. * - * @param \Illuminate\Pagination\Factory $paginator - * @param int $perPage - * @param array $columns + * @param \Illuminate\Pagination\Factory $paginator + * @param int $perPage + * @param array $columns * @return \Illuminate\Pagination\Paginator */ protected function groupedPaginate($paginator, $perPage, $columns) @@ -292,9 +308,9 @@ protected function groupedPaginate($paginator, $perPage, $columns) /** * Get a paginator for an ungrouped statement. * - * @param \Illuminate\Pagination\Factory $paginator - * @param int $perPage - * @param array $columns + * @param \Illuminate\Pagination\Factory $paginator + * @param int $perPage + * @param array $columns * @return \Illuminate\Pagination\Paginator */ protected function ungroupedPaginate($paginator, $perPage, $columns) @@ -314,8 +330,8 @@ protected function ungroupedPaginate($paginator, $perPage, $columns) /** * Paginate the given query into a simple paginator. * - * @param int $perPage - * @param array $columns + * @param int $perPage + * @param array $columns * @return \Illuminate\Contracts\Pagination\Paginator */ public function simplePaginate($perPage = null, $columns = ['*']) @@ -326,18 +342,16 @@ public function simplePaginate($perPage = null, $columns = ['*']) $this->skip(($page - 1) * $perPage)->take($perPage + 1); - return new Paginator($this->get($columns)->all(), $perPage, $page, [ - 'path' => Paginator::resolveCurrentPath() - ]); + return new Paginator($this->get($columns)->all(), $perPage, $page, ['path' => Paginator::resolveCurrentPath()]); } /** * Add a basic where clause to the query. * - * @param string $column - * @param string $operator - * @param mixed $value - * @param string $boolean + * @param string $column + * @param string $operator + * @param mixed $value + * @param string $boolean * @return $this */ public function where($column, $operator = null, $value = null, $boolean = 'and') @@ -349,7 +363,7 @@ public function where($column, $operator = null, $value = null, $boolean = 'and' $this->query->addNestedWhereQuery($query->getQuery(), $boolean); } else { - call_user_func_array(array($this->query, 'where'), func_get_args()); + call_user_func_array([$this->query, 'where'], func_get_args()); } return $this; @@ -358,9 +372,9 @@ public function where($column, $operator = null, $value = null, $boolean = 'and' /** * Add an "or where" clause to the query. * - * @param string $column - * @param string $operator - * @param mixed $value + * @param string $column + * @param string $operator + * @param mixed $value * @return \Analogue\ORM\System\Query */ public function orWhere($column, $operator = null, $value = null) @@ -371,11 +385,11 @@ public function orWhere($column, $operator = null, $value = null) /** * Add a relationship count condition to the query. * - * @param string $relation - * @param string $operator - * @param int $count - * @param string $boolean - * @param \Closure $callback + * @param string $relation + * @param string $operator + * @param int $count + * @param string $boolean + * @param \Closure $callback * @return \Analogue\ORM\System\Query */ public function has($relation, $operator = '>=', $count = 1, $boolean = 'and', $callback = null) @@ -396,10 +410,10 @@ public function has($relation, $operator = '>=', $count = 1, $boolean = 'and', $ /** * Add a relationship count condition to the query with where clauses. * - * @param string $relation - * @param \Closure $callback - * @param string $operator - * @param int $count + * @param string $relation + * @param \Closure $callback + * @param string $operator + * @param int $count * @return \Analogue\ORM\System\Query */ public function whereHas($relation, Closure $callback, $operator = '>=', $count = 1) @@ -410,9 +424,9 @@ public function whereHas($relation, Closure $callback, $operator = '>=', $count /** * Add a relationship count condition to the query with an "or". * - * @param string $relation - * @param string $operator - * @param int $count + * @param string $relation + * @param string $operator + * @param int $count * @return \Analogue\ORM\System\Query */ public function orHas($relation, $operator = '>=', $count = 1) @@ -423,10 +437,10 @@ public function orHas($relation, $operator = '>=', $count = 1) /** * Add a relationship count condition to the query with where clauses and an "or". * - * @param string $relation - * @param \Closure $callback - * @param string $operator - * @param int $count + * @param string $relation + * @param \Closure $callback + * @param string $operator + * @param int $count * @return \Analogue\ORM\System\Query */ public function orWhereHas($relation, Closure $callback, $operator = '>=', $count = 1) @@ -437,11 +451,11 @@ public function orWhereHas($relation, Closure $callback, $operator = '>=', $coun /** * Add the "has" condition where clause to the query. * - * @param \Analogue\ORM\System\Query $hasQuery - * @param \Analogue\ORM\Relationships\Relationship $relation - * @param string $operator - * @param int $count - * @param string $boolean + * @param \Analogue\ORM\System\Query $hasQuery + * @param \Analogue\ORM\Relationships\Relationship $relation + * @param string $operator + * @param int $count + * @param string $boolean * @return \Analogue\ORM\System\Query */ protected function addHasWhere(Query $hasQuery, Relationship $relation, $operator, $count, $boolean) @@ -452,14 +466,14 @@ protected function addHasWhere(Query $hasQuery, Relationship $relation, $operato $count = new Expression($count); } - return $this->where(new Expression('('.$hasQuery->toSql().')'), $operator, $count, $boolean); + return $this->where(new Expression('(' . $hasQuery->toSql() . ')'), $operator, $count, $boolean); } /** * Merge the "wheres" from a relation query to a has query. * - * @param \Analogue\ORM\System\Query $hasQuery - * @param \Analogue\ORM\Relationships\Relationship $relation + * @param \Analogue\ORM\System\Query $hasQuery + * @param \Analogue\ORM\Relationships\Relationship $relation * @return void */ protected function mergeWheresToHas(Query $hasQuery, Relationship $relation) @@ -479,7 +493,8 @@ protected function mergeWheresToHas(Query $hasQuery, Relationship $relation) /** * Get the "has relation" base query instance. * - * @param string $relation + * @param string $relation + * @param $entity * @return \Analogue\ORM\System\Query */ protected function getHasRelationQuery($relation, $entity) @@ -502,7 +517,7 @@ public function getTable() /** * Set the relationships that should be eager loaded. * - * @param mixed $relations + * @param mixed $relations * @return $this */ public function with($relations) @@ -521,12 +536,12 @@ public function with($relations) /** * Parse a list of relations into individuals. * - * @param array $relations + * @param array $relations * @return array */ protected function parseRelations(array $relations) { - $results = array(); + $results = []; foreach ($relations as $name => $constraints) { // If the "relation" value is actually a numeric key, we can assume that no @@ -535,7 +550,7 @@ protected function parseRelations(array $relations) if (is_numeric($name)) { $f = function () {}; - list($name, $constraints) = array($constraints, $f); + list($name, $constraints) = [$constraints, $f]; } // We need to separate out any nested includes. Which allows the developers @@ -553,13 +568,13 @@ protected function parseRelations(array $relations) /** * Parse the nested relationships in a relation. * - * @param string $name - * @param array $results + * @param string $name + * @param array $results * @return array */ protected function parseNested($name, $results) { - $progress = array(); + $progress = []; // If the relation has already been set on the result array, we will not set it // again, since that would override any constraints that were already placed @@ -567,7 +582,7 @@ protected function parseNested($name, $results) foreach (explode('.', $name) as $segment) { $progress[] = $segment; - if (! isset($results[$last = implode('.', $progress)])) { + if (!isset($results[$last = implode('.', $progress)])) { $results[$last] = function () {}; } } @@ -588,7 +603,7 @@ public function getEagerLoads() /** * Set the relationships being eagerly loaded. * - * @param array $eagerLoad + * @param array $eagerLoad * @return void */ public function setEagerLoads(array $eagerLoad) @@ -599,7 +614,7 @@ public function setEagerLoads(array $eagerLoad) /** * Eager load the relationships for the entities. * - * @param array $entities + * @param array $entities * @return array */ public function eagerLoadRelations($entities) @@ -619,9 +634,9 @@ public function eagerLoadRelations($entities) /** * Eagerly load the relationship on a set of entities. * - * @param array $entities - * @param string $name - * @param \Closure $constraints + * @param array $entities + * @param string $name + * @param \Closure $constraints * @return array */ protected function loadRelation(array $entities, $name, Closure $constraints) @@ -640,7 +655,7 @@ protected function loadRelation(array $entities, $name, Closure $constraints) // Once we have the results, we just match those back up to their parent models // using the relationship instance. Then we just return the finished arrays // of models which have been eagerly hydrated and are readied for return. - + $results = $relation->getEager(); return $relation->match($entities, $results, $name); @@ -649,7 +664,7 @@ protected function loadRelation(array $entities, $name, Closure $constraints) /** * Get the relation instance for the given relation name. * - * @param string $relation + * @param string $relation * @return \Analogue\ORM\Relationships\Relationship */ public function getRelation($relation) @@ -657,7 +672,7 @@ public function getRelation($relation) // We want to run a relationship query without any constrains so that we will // not have to remove these where clauses manually which gets really hacky // and is error prone while we remove the developer's own where clauses. - $query = Relationship::noConstraints(function () use ($relation) { + $query = Relationship::noConstraints(function() use ($relation) { return $this->entityMap->$relation($this->getEntityInstance()); }); @@ -676,19 +691,19 @@ public function getRelation($relation) /** * Get the deeply nested relations for a given top-level relation. * - * @param string $relation + * @param string $relation * @return array */ protected function nestedRelations($relation) { - $nested = array(); + $nested = []; // We are basically looking for any relationships that are nested deeper than // the given top-level relationship. We will just check for any relations // that start with the given top relations and adds them to our arrays. foreach ($this->eagerLoad as $name => $constraints) { if ($this->isNested($name, $relation)) { - $nested[substr($name, strlen($relation.'.'))] = $constraints; + $nested[substr($name, strlen($relation . '.'))] = $constraints; } } @@ -698,15 +713,15 @@ protected function nestedRelations($relation) /** * Determine if the relationship is nested. * - * @param string $name - * @param string $relation + * @param string $name + * @param string $relation * @return bool */ protected function isNested($name, $relation) { $dots = str_contains($name, '.'); - return $dots && starts_with($name, $relation.'.'); + return $dots && starts_with($name, $relation . '.'); } /** @@ -717,7 +732,7 @@ protected function isNested($name, $relation) */ protected function enforceIdColumn($columns) { - if (! in_array($this->entityMap->getKeyName(), $columns)) { + if (!in_array($this->entityMap->getKeyName(), $columns)) { $columns[] = $this->entityMap->getKeyName(); } return $columns; @@ -727,9 +742,9 @@ protected function enforceIdColumn($columns) * Get the hydrated models without eager loading. * * @param array $columns - * @return Analogue\ORM\EntityCollection + * @return \Analogue\ORM\EntityCollection */ - public function getEntities($columns = array('*')) + public function getEntities($columns = ['*']) { // As we need the primary key to feed the // entity cache, we need it loaded on each @@ -748,9 +763,9 @@ public function getEntities($columns = array('*')) * Get a new instance for the entity * * @param array $attributes - * @return Entity + * @return \Analogue\ORM\Entity */ - public function getEntityInstance(array $attributes = array()) + public function getEntityInstance(array $attributes = []) { return $this->mapper->newInstance($attributes); } @@ -758,8 +773,8 @@ public function getEntityInstance(array $attributes = array()) /** * Extend the builder with a given callback. * - * @param string $name - * @param \Closure $callback + * @param string $name + * @param \Closure $callback * @return void */ public function macro($name, Closure $callback) @@ -770,7 +785,7 @@ public function macro($name, Closure $callback) /** * Get the given macro by name. * - * @param string $name + * @param string $name * @return \Closure */ public function getMacro($name) @@ -816,7 +831,7 @@ public function getMapper() * (REFACTOR: this method should move out, we need to provide the client classes * with the adapter instead.) * - * @return \Analogue\ORM\Drivers\QueryAdapter + * @return \Analogue\ORM\Drivers\QueryAdapter|\Analogue\ORM\Drivers\IlluminateQueryAdapter */ public function getQuery() { @@ -826,8 +841,9 @@ public function getQuery() /** * Dynamically handle calls into the query instance. * - * @param string $method - * @param array $parameters + * @param string $method + * @param array $parameters + * @throws Exception * @return mixed */ public function __call($method, $parameters) @@ -842,7 +858,7 @@ public function __call($method, $parameters) throw new Exception("Method $method doesn't exist"); } - $result = call_user_func_array(array($this->query, $method), $parameters); + $result = call_user_func_array([$this->query, $method], $parameters); return in_array($method, $this->passthru) ? $result : $this; } diff --git a/src/System/ScopeInterface.php b/src/System/ScopeInterface.php index 6e42321..161ebe2 100755 --- a/src/System/ScopeInterface.php +++ b/src/System/ScopeInterface.php @@ -1,12 +1,13 @@ -getName(), $attributeList)) { return true; } }); } + /** + * @param string $name + * @return \ReflectionProperty + */ protected function getMappedProperty($name) { return $this->reflection->getProperty($name); @@ -101,6 +100,7 @@ protected function getMappedProperty($name) /** * Hydrate Plain PHP Object with wrapped attributes * + * @param $attributes * @return void */ protected function hydrate($attributes) @@ -114,13 +114,13 @@ protected function hydrate($attributes) $this->entity->$name = $attributes[$name]; } else { $property->setAccessible(true); - + $property->setValue($this->entity, $attributes[$name]); } } } - /** + /** * Method used by the mapper to set the object * attribute raw values (hydration) * @@ -189,16 +189,14 @@ public function getEntityAttribute($key) return $value; } - /** - * Test if a given attribute exists - * - * @param string $key - * @return boolean - */ + /** + * Test if a given attribute exists + * + * @param string $key + * @return boolean + */ public function hasAttribute($key) { - $attributes = $this->entity->getEntityAttributes(); - if (array_key_exists($key, $$this->attributeList)) { return true; } else { diff --git a/src/System/Wrappers/Wrapper.php b/src/System/Wrappers/Wrapper.php index b7bdd54..416e4c6 100755 --- a/src/System/Wrappers/Wrapper.php +++ b/src/System/Wrappers/Wrapper.php @@ -2,7 +2,6 @@ namespace Analogue\ORM\System\Wrappers; -use Analogue\ORM\EntityMap; use Analogue\ORM\System\InternallyMappable; use Analogue\ORM\System\Proxies\EntityProxy; use Analogue\ORM\System\Proxies\CollectionProxy; @@ -12,7 +11,6 @@ */ abstract class Wrapper implements InternallyMappable { - /** * Original Entity Object * @@ -27,6 +25,11 @@ abstract class Wrapper implements InternallyMappable */ protected $entityMap; + /** + * Wrapper constructor. + * @param $entity + * @param $entityMap + */ public function __construct($entity, $entityMap) { $this->entity = $entity; @@ -67,7 +70,7 @@ public function setProxies() $proxies = []; foreach ($this->entityMap->getRelationships() as $relation) { - if (! array_key_exists($relation, $attributes) || is_null($attributes[$relation])) { + if (!array_key_exists($relation, $attributes) || is_null($attributes[$relation])) { if (in_array($relation, $singleRelations)) { $proxies[$relation] = new EntityProxy($this->getObject(), $relation); } @@ -82,13 +85,33 @@ public function setProxies() } } + /** + * @param string $key + * @param string $value + * @return mixed + */ abstract public function setEntityAttribute($key, $value); + /** + * @param string $key + * @return mixed + */ abstract public function getEntityAttribute($key); + /** + * @param array $attributes + * @return mixed + */ abstract public function setEntityAttributes(array $attributes); + /** + * @return mixed + */ abstract public function getEntityAttributes(); + /** + * @param string $key + * @return mixed + */ abstract public function hasAttribute($key); } diff --git a/src/ValueMap.php b/src/ValueMap.php index b1539f8..e53f887 100755 --- a/src/ValueMap.php +++ b/src/ValueMap.php @@ -1,36 +1,64 @@ -attributes; } + /** + * @return array + */ public function getEmbeddables() { return $this->embeddables; } + /** + * @param $class + */ public function setClass($class) { $this->class = $class; } + /** + * @return mixed + */ public function getClass() { return $this->class; } + /** + * @return string + */ public function getName() { if (isset($this->name)) { diff --git a/src/ValueObject.php b/src/ValueObject.php index ee2655e..faf19c8 100755 --- a/src/ValueObject.php +++ b/src/ValueObject.php @@ -1,4 +1,6 @@ -$offset); } - + /** * Convert the object into something JSON serializable. * @@ -111,11 +113,11 @@ public function jsonSerialize() { return $this->toArray(); } - + /** * Convert the entity instance to JSON. * - * @param int $options + * @param int $options * @return string */ public function toJson($options = 0) @@ -123,7 +125,6 @@ public function toJson($options = 0) return json_encode($this->toArray(), $options); } - /** * Convert Mappable object to array; * @@ -134,9 +135,10 @@ public function toArray() return $this->attributesToArray($this->attributes); } - /** + /** * Transform the Object to array/json, * + * @param array $sourceAttributes * @return array */ protected function attributesToArray(array $sourceAttributes) @@ -146,7 +148,7 @@ protected function attributesToArray(array $sourceAttributes) foreach ($sourceAttributes as $key => $attribute) { // If the attribute is a proxy, and hasn't be loaded, we discard // it from the returned set. - if ($attribute instanceof ProxyInterface && ! $attribute->isLoaded()) { + if ($attribute instanceof ProxyInterface && !$attribute->isLoaded()) { continue; } diff --git a/tests/AnalogueTest/App/Avatar.php b/tests/AnalogueTest/App/Avatar.php index 783a148..b2cc4ab 100755 --- a/tests/AnalogueTest/App/Avatar.php +++ b/tests/AnalogueTest/App/Avatar.php @@ -1,10 +1,11 @@ -name = $name; diff --git a/tests/AnalogueTest/App/AvatarMap.php b/tests/AnalogueTest/App/AvatarMap.php index 12b48d3..f18db7f 100755 --- a/tests/AnalogueTest/App/AvatarMap.php +++ b/tests/AnalogueTest/App/AvatarMap.php @@ -1,10 +1,11 @@ -name = $name; diff --git a/tests/AnalogueTest/App/ExternalMap.php b/tests/AnalogueTest/App/ExternalMap.php index bd379a0..8e3d5df 100755 --- a/tests/AnalogueTest/App/ExternalMap.php +++ b/tests/AnalogueTest/App/ExternalMap.php @@ -1,10 +1,11 @@ -path = $path; diff --git a/tests/AnalogueTest/App/ImageMap.php b/tests/AnalogueTest/App/ImageMap.php index 7136904..7d88f0f 100755 --- a/tests/AnalogueTest/App/ImageMap.php +++ b/tests/AnalogueTest/App/ImageMap.php @@ -1,10 +1,11 @@ -morphTo($image); diff --git a/tests/AnalogueTest/App/Meta.php b/tests/AnalogueTest/App/Meta.php index 5a3f03d..b1151ed 100755 --- a/tests/AnalogueTest/App/Meta.php +++ b/tests/AnalogueTest/App/Meta.php @@ -1,10 +1,11 @@ -label = $label; diff --git a/tests/AnalogueTest/App/PermissionMap.php b/tests/AnalogueTest/App/PermissionMap.php index 94d732d..f452fd3 100755 --- a/tests/AnalogueTest/App/PermissionMap.php +++ b/tests/AnalogueTest/App/PermissionMap.php @@ -1,10 +1,11 @@ -belongsToMany($permission, 'AnalogueTest\App\Role', 'role_permission'); diff --git a/tests/AnalogueTest/App/Popo.php b/tests/AnalogueTest/App/Popo.php index fcc7b00..da0c6a2 100755 --- a/tests/AnalogueTest/App/Popo.php +++ b/tests/AnalogueTest/App/Popo.php @@ -1,8 +1,9 @@ -label = $label; diff --git a/tests/AnalogueTest/App/RoleMap.php b/tests/AnalogueTest/App/RoleMap.php index c539865..3ac96ef 100755 --- a/tests/AnalogueTest/App/RoleMap.php +++ b/tests/AnalogueTest/App/RoleMap.php @@ -1,10 +1,11 @@ -hasMany($entity, 'AnalogueTest\App\User'); diff --git a/tests/AnalogueTest/App/User.php b/tests/AnalogueTest/App/User.php index 3a3b530..eaef1bb 100755 --- a/tests/AnalogueTest/App/User.php +++ b/tests/AnalogueTest/App/User.php @@ -1,10 +1,11 @@ -email = $email; diff --git a/tests/AnalogueTest/App/UserMap.php b/tests/AnalogueTest/App/UserMap.php index 710d7fc..f431bd1 100755 --- a/tests/AnalogueTest/App/UserMap.php +++ b/tests/AnalogueTest/App/UserMap.php @@ -1,10 +1,11 @@ -uuid = $uuid; diff --git a/tests/AnalogueTest/App/UuidMap.php b/tests/AnalogueTest/App/UuidMap.php index 4ac9d8f..89e3b7c 100755 --- a/tests/AnalogueTest/App/UuidMap.php +++ b/tests/AnalogueTest/App/UuidMap.php @@ -1,10 +1,11 @@ -field_1 = $a; diff --git a/tests/AnalogueTest/App/VMap.php b/tests/AnalogueTest/App/VMap.php index aa68226..a418728 100755 --- a/tests/AnalogueTest/App/VMap.php +++ b/tests/AnalogueTest/App/VMap.php @@ -1,9 +1,10 @@ -add($e)->add($f); - $this->assertEquals(array($e, $f), $c->all()); + $this->assertEquals([$e, $f], $c->all()); } /*public function testConstructorRejectNonMappableItems() @@ -86,7 +87,7 @@ public function testContainsIndicatesIfEntityInArray() $entity3 = new Entity; $entity3->id = 3; - $c = new Collection(array($entity1, $entity2)); + $c = new Collection([$entity1, $entity2]); $this->assertTrue($c->contains($entity1)); $this->assertTrue($c->contains($entity2)); @@ -116,9 +117,9 @@ public function testCollectionDictionaryReturnsEntityKeys() $entity3 = new Entity; $entity3->id = 3; - $c = new Collection(array($entity1, $entity2, $entity3)); + $c = new Collection([$entity1, $entity2, $entity3]); - $this->assertEquals(array(1, 2, 3), $c->getEntityKeys()); + $this->assertEquals([1, 2, 3], $c->getEntityKeys()); } @@ -134,7 +135,7 @@ public function testCollectionMergesWithGivenCollection() $c1 = new Collection([$e1, $e2]); $c2 = new Collection([$e2, $e3]); - $this->assertEquals(new Collection(array($e1, $e2, $e3)), $c1->merge($c2)); + $this->assertEquals(new Collection([$e1, $e2, $e3]), $c1->merge($c2)); } @@ -150,7 +151,7 @@ public function testCollectionDiffsWithGivenCollection() $c1 = new Collection([$e1, $e2]); $c2 = new Collection([$e2, $e3]); - $this->assertEquals(new Collection(array($e1)), $c1->diff($c2)); + $this->assertEquals(new Collection([$e1]), $c1->diff($c2)); } @@ -166,7 +167,7 @@ public function testCollectionIntersectsWithGivenCollection() $c1 = new Collection([$e1, $e2]); $c2 = new Collection([$e2, $e3]); - $this->assertEquals(new Collection(array($e2)), $c1->intersect($c2)); + $this->assertEquals(new Collection([$e2]), $c1->intersect($c2)); } @@ -177,9 +178,9 @@ public function testCollectionReturnsUniqueItems() $e2 = new Entity; $e2->id = 2; - $c = new Collection(array($e1, $e2, $e2)); + $c = new Collection([$e1, $e2, $e2]); - $this->assertEquals(new Collection(array($e1, $e2)), $c->unique()); + $this->assertEquals(new Collection([$e1, $e2]), $c->unique()); } @@ -225,7 +226,7 @@ public function testExceptReturnsCollectionWithoutGivenModelKeys() $c = new Collection([$e1, $e2, $e3]); - $this->assertEquals(new Collection(array($e1, $e3)), $c->except(2)); - $this->assertEquals(new Collection(array($e1)), $c->except(array(2, 3))); + $this->assertEquals(new Collection([$e1, $e3]), $c->except(2)); + $this->assertEquals(new Collection([$e1]), $c->except([2, 3])); } } diff --git a/tests/AnalogueTest/EntityTest.php b/tests/AnalogueTest/EntityTest.php index a1e5bf8..41f2b3a 100755 --- a/tests/AnalogueTest/EntityTest.php +++ b/tests/AnalogueTest/EntityTest.php @@ -1,12 +1,12 @@ - 1, - 'column2' => "2", + 'column2' => '2', ]; $entity = $mapper->newInstance($attributes); $this->assertEquals($entity->getEntityAttributes(), $attributes); diff --git a/tests/AnalogueTest/PlainObjectTest.php b/tests/AnalogueTest/PlainObjectTest.php index 290cb9c..8b387d0 100755 --- a/tests/AnalogueTest/PlainObjectTest.php +++ b/tests/AnalogueTest/PlainObjectTest.php @@ -6,7 +6,6 @@ class PlainObjectTest extends PHPUnit_Framework_TestCase { - public function testPopoStore() { diff --git a/tests/AnalogueTest/QueryTest.php b/tests/AnalogueTest/QueryTest.php index b075ad2..6314c78 100755 --- a/tests/AnalogueTest/QueryTest.php +++ b/tests/AnalogueTest/QueryTest.php @@ -1,9 +1,10 @@ -add(new Permission("P$x")); } $mapper->store($c); @@ -218,8 +219,8 @@ public function testPaginationWithCustomValue() { $mapper = get_mapper('AnalogueTest\App\Permission'); $c = new EntityCollection; - $y=0; - for ($x=0;$x<30;$x++) { + $y = 0; + for ($x = 0; $x < 30; $x++) { $c->add(new Permission("P$x")); } $mapper->store($c); @@ -231,8 +232,8 @@ public function testSimplePaginate() { $mapper = get_mapper('AnalogueTest\App\Permission'); $c = new EntityCollection; - $y=0; - for ($x=0;$x<30;$x++) { + $y = 0; + for ($x = 0; $x < 30; $x++) { $c->add(new Permission("P$x")); } $mapper->store($c); diff --git a/tests/AnalogueTest/RepositoryTest.php b/tests/AnalogueTest/RepositoryTest.php index 1cbd686..230b79c 100755 --- a/tests/AnalogueTest/RepositoryTest.php +++ b/tests/AnalogueTest/RepositoryTest.php @@ -1,4 +1,6 @@ -store([$p, $q]); $r = $repo->allMatching(['label' => 'Third']); - + $this->assertInstanceOf('Analogue\ORM\EntityCollection', $r); $this->assertEquals(2, $r->count()); } @@ -68,7 +69,7 @@ public function testPaginate() $p = new Permission('First'); $q = new Permission('Second'); $repo->store([$p, $q]); - + $s = $repo->paginate(1); $this->assertEquals(1, count($s)); } diff --git a/tests/AnalogueTest/ValueObjectTest.php b/tests/AnalogueTest/ValueObjectTest.php index 19c64c4..4b810e1 100755 --- a/tests/AnalogueTest/ValueObjectTest.php +++ b/tests/AnalogueTest/ValueObjectTest.php @@ -1,7 +1,8 @@ -add('AnalogueTest', __DIR__); // Date setup