Skip to content

Commit

Permalink
changes
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarotero committed Sep 2, 2015
1 parent f08ef4e commit 8eabd7d
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 95 deletions.
18 changes: 10 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
}
```
Expand All @@ -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;
}
```

Expand Down
53 changes: 29 additions & 24 deletions src/BaseRow.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
abstract class BaseRow implements RowInterface
{
protected $entity;
protected $db;
protected $functions = [];
protected $methods = [];
protected $properties = [];

/**
* Constructor
Expand All @@ -18,7 +18,6 @@ abstract class BaseRow implements RowInterface
public function __construct(Entity $entity)
{
$this->entity = $entity;
$this->db = $entity->getDb();
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -93,31 +112,17 @@ 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
*
* @param string $name
*/
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);
}
}
}
80 changes: 36 additions & 44 deletions src/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
}
}
}
19 changes: 10 additions & 9 deletions src/Row.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand Down
21 changes: 12 additions & 9 deletions src/RowInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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);
}
38 changes: 37 additions & 1 deletion tests/RowTest.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
<?php
use SimpleCrud\SimpleCrud;
use SimpleCrud\EntityFactory;
use SimpleCrud\Entity;

class RowTest extends PHPUnit_Framework_TestCase
{
protected $db;

public function setUp()
{
$this->db = new SimpleCrud(initSqlitePdo());
$entityFactory = new EntityFactory();
$entityFactory->setAutocreate('DefaultEntity');

$this->db = new SimpleCrud(initSqlitePdo(), $entityFactory);
}

public function testPost()
Expand Down Expand Up @@ -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);
});
}
}

0 comments on commit 8eabd7d

Please sign in to comment.