From 8eabd7d7c81355ce09cac46aa8318bac968502b0 Mon Sep 17 00:00:00 2001 From: oscarotero Date: Wed, 2 Sep 2015 12:16:49 +0200 Subject: [PATCH] changes --- README.md | 18 +++++----- src/BaseRow.php | 53 ++++++++++++++++------------- src/Entity.php | 80 ++++++++++++++++++++------------------------ src/Row.php | 19 ++++++----- src/RowInterface.php | 21 +++++++----- tests/RowTest.php | 38 ++++++++++++++++++++- 6 files changed, 134 insertions(+), 95 deletions(-) diff --git a/README.md b/README.md index d5caddb..7efe07d 100644 --- a/README.md +++ b/README.md @@ -438,13 +438,15 @@ class Posts extends Entity { public function init() { - //Use my own row class - $this->setRow(new MyCustomRow($this)); - - //Add some methods to rowcollection default class - $this->collection->setCustomFunction('sumIds', function ($collection) { + //Add some methods to collections + $this->collection->registerMethod('sumIds', function ($collection) { return array_sum($collection->id); }); + + //Add some properties to row + $this->row->registerProperty('titleLowerCase', function ($row) { + return strtolower($row->title); + }); } } ``` @@ -454,12 +456,12 @@ Now, on use the entity: ```php $posts = $db->post->select()->all(); -//Execute the registered function in the collection +//Execute the registered method in the collection echo $posts->sumIds(); -//Execute custom functions in each row +//Execute the registered property in the row foreach ($posts as $post) { - $post->customFunction(); + echo $post->titleLowerCase; } ``` diff --git a/src/BaseRow.php b/src/BaseRow.php index a708f68..4798653 100644 --- a/src/BaseRow.php +++ b/src/BaseRow.php @@ -7,8 +7,8 @@ abstract class BaseRow implements RowInterface { protected $entity; - protected $db; - protected $functions = []; + protected $methods = []; + protected $properties = []; /** * Constructor @@ -18,7 +18,6 @@ abstract class BaseRow implements RowInterface public function __construct(Entity $entity) { $this->entity = $entity; - $this->db = $entity->getDb(); } /** @@ -36,19 +35,37 @@ public function getEntity() * * {@inheritdoc} */ - public function getDb() + public function getAttribute($name) { - return $this->db; + return $this->entity->getDb()->getAttribute($name); } /** * @see RowInterface * * {@inheritdoc} + * + * @return self */ - public function getAttribute($name) + public function registerMethod($name, callable $callable) + { + $this->methods[$name] = $callable; + + return $this; + } + + /** + * @see RowInterface + * + * {@inheritdoc} + * + * @return self + */ + public function registerProperty($name, callable $callable) { - return $this->db->getAttribute($name); + $this->properties[$name] = $callable; + + return $this; } /** @@ -70,7 +87,9 @@ public function jsonSerialize() */ public function select($entity) { - return $this->db->$entity->select()->relatedWith($this); + $db = $this->entity->getDb(); + + return $db->$entity->select()->relatedWith($this); } /** @@ -93,20 +112,6 @@ public function delete() return $this; } - /** - * Set a custom function - * - * {@inheritdoc} - * - * @return self - */ - public function setCustomFunction($name, callable $function) - { - $this->functions[$name] = $function; - - return $this; - } - /** * Magic method to execute custom method defined in the entity class * @@ -114,10 +119,10 @@ public function setCustomFunction($name, callable $function) */ public function __call($name, $arguments) { - if (isset($this->functions[$name])) { + if (isset($this->methods[$name])) { array_unshift($arguments, $this); - return call_user_func_array($this->functions[$name], $arguments); + return call_user_func_array($this->methods[$name], $arguments); } } } diff --git a/src/Entity.php b/src/Entity.php index b0494b9..d1c4b3c 100644 --- a/src/Entity.php +++ b/src/Entity.php @@ -318,6 +318,14 @@ public function prepareDataToDatabase(array $data, $new) */ public function getRelation($entity) { + if (is_string($entity)) { + if (!$this->db->has($entity)) { + return false; + } + + $entity = $this->db->get($entity); + } + if ($this->hasOne($entity)) { return self::RELATION_HAS_ONE; } @@ -332,78 +340,62 @@ public function getRelation($entity) } /** - * Returns the entity that works as a bridge between this entity and other + * Returns whether the relation type of this entity with other is HAS_MANY. * * @param Entity $entity * - * @return Entity|null + * @return boolean */ - public function getBridge(Entity $entity) + public function hasMany(Entity $entity) { - if ($this->name < $entity->name) { - $name = "{$this->name}_{$entity->name}"; - } else { - $name = "{$entity->name}_{$this->name}"; - } - - if ($this->db->has($name)) { - $bridge = $this->db->$name; - - if (isset($bridge->fields[$this->foreignKey]) && isset($bridge->fields[$entity->foreignKey])) { - return $bridge; - } - } + return isset($entity->fields[$this->foreignKey]); } /** * Returns whether the relation type of this entity with other is HAS_MANY. * - * @param Entity|string $entity + * @param Entity $entity * * @return boolean */ - public function hasMany($entity) + public function hasOne(Entity $entity) { - if (is_string($entity)) { - if (!isset($this->db->$entity)) { - return false; - } - - $entity = $this->db->$entity; - } - - return isset($entity->fields[$this->foreignKey]); + return isset($this->fields[$entity->foreignKey]); } /** - * Returns whether the relation type of this entity with other is HAS_MANY. + * Returns whether the relation type of this entity with other is HAS_BRIDGE. * - * @param Entity|string $entity + * @param Entity $entity * * @return boolean */ - public function hasOne($entity) + public function hasBridge(Entity $entity) { - if (is_string($entity)) { - if (!isset($this->db->$entity)) { - return false; - } - - $entity = $this->db->$entity; - } - - return isset($this->fields[$entity->foreignKey]); + return $this->getBridge($entity) !== null; } /** - * Returns whether the relation type of this entity with other is HAS_BRIDGE. + * Returns the entity that works as a bridge between this entity and other * - * @param Entity|string $entity + * @param Entity $entity * - * @return boolean + * @return Entity|null */ - public function hasBridge($entity) + public function getBridge(Entity $entity) { - return $this->getBridge($entity) !== null; + if ($this->name < $entity->name) { + $name = "{$this->name}_{$entity->name}"; + } else { + $name = "{$entity->name}_{$this->name}"; + } + + if ($this->db->has($name)) { + $bridge = $this->db->$name; + + if (isset($bridge->fields[$this->foreignKey]) && isset($bridge->fields[$entity->foreignKey])) { + return $bridge; + } + } } } diff --git a/src/Row.php b/src/Row.php index fac2c9a..4af99e8 100644 --- a/src/Row.php +++ b/src/Row.php @@ -34,18 +34,19 @@ public function __get($name) return $this->values[$name]; } - //Custom method - if (method_exists($this->entity, "row{$name}")) { - return $this->values[$name] = $this->__call($name, []); + //Custom property + if (isset($this->properties[$name])) { + return $this->values[$name] = call_user_func($this->properties[$name], $this); } //Load related data - if ($this->entity->hasOne($name)) { - return $this->values[$name] = $this->select($name)->one(); - } - - if ($this->entity->hasMany($name)) { - return $this->values[$name] = $this->select($name)->all(); + switch ($this->entity->getRelation($name)) { + case Entity::RELATION_HAS_ONE: + return $this->values[$name] = $this->select($name)->one(); + + case Entity::RELATION_HAS_MANY: + case Entity::RELATION_HAS_BRIDGE: + return $this->values[$name] = $this->select($name)->all(); } } diff --git a/src/RowInterface.php b/src/RowInterface.php index 8cfb7d8..9443f69 100644 --- a/src/RowInterface.php +++ b/src/RowInterface.php @@ -25,13 +25,6 @@ public function toArray($keysAsId = false, array $parentEntities = array()); */ public function getEntity(); - /** - * Return the database connection - * - * @return SimpleCrud - */ - public function getDb(); - /** * Returns an attribute * @@ -45,9 +38,19 @@ public function getAttribute($name); * Register a new custom method * * @param string $name - * @param callable $function + * @param callable $callable + * + * @return self + */ + public function registerMethod($name, callable $callable); + + /** + * Register a new custom property + * + * @param string $name + * @param callable $callable * * @return self */ - public function setCustomFunction($name, callable $function); + public function registerProperty($name, callable $callable); } diff --git a/tests/RowTest.php b/tests/RowTest.php index 65ece13..03f37b1 100644 --- a/tests/RowTest.php +++ b/tests/RowTest.php @@ -1,5 +1,7 @@ db = new SimpleCrud(initSqlitePdo()); + $entityFactory = new EntityFactory(); + $entityFactory->setAutocreate('DefaultEntity'); + + $this->db = new SimpleCrud(initSqlitePdo(), $entityFactory); } public function testPost() @@ -38,4 +43,35 @@ public function testPost() $this->assertEquals(1, $post->id); $this->assertEquals(1, $post->get('id')); } + + public function testCustomFunction() + { + $post = $this->db->post->create([ + 'title' => 'THIS IS THE TITLE' + ])->save(); + + $this->assertSame('this is the title', $post->getTitleLowerCase()); + $this->assertSame('this is the title', $post->titleLowerCase); + + $this->db->post->insert()->data(['title' => 'second'])->run(); + $this->assertSame(3, $this->db->post->select()->all()->sumIds()); + } } + +class DefaultEntity extends Entity +{ + protected function init() + { + $this->row + ->registerMethod('getTitleLowerCase', function ($row) { + return strtolower($row->title); + }) + ->registerProperty('titleLowerCase', function ($row) { + return $row->getTitleLowerCase(); + }); + + $this->collection->registerMethod('sumIds', function($collection) { + return array_sum($collection->id); + }); + } +} \ No newline at end of file