Skip to content
This repository has been archived by the owner on Feb 4, 2022. It is now read-only.

Commit

Permalink
Issue #68: Refactor ModelEntityComposite
Browse files Browse the repository at this point in the history
- Do not extend from ObjectSet, compose it
- Implement missing interface methods
- Refactor implemetation of filter() method
- Change toArray() to return the current entity in the set

BREAKING! ModelEntityComposite::toArray() no longer flattens the whole entity collection it only returns a array representation of the current entity. To migrate your code, use iterator_to_array if you want to get a array representation of the all entities in the collection, or traverse through the collection.
  • Loading branch information
johanjanssens committed Apr 28, 2016
1 parent 362ddbf commit 569f436
Showing 1 changed file with 137 additions and 24 deletions.
161 changes: 137 additions & 24 deletions code/model/entity/composite.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,15 @@
* @author Johan Janssens <https://github.com/johanjanssens>
* @package Kodekit\Library\Model\Entity
*/
class ModelEntityComposite extends ObjectSet implements ModelEntityComposable
class ModelEntityComposite extends Object implements ModelEntityComposable
{
/**
* The entity set
*
* @var ObjectSet
*/
private $__entities;

/**
* Name of the identity key in the collection
*
Expand Down Expand Up @@ -144,7 +151,7 @@ public function insert($entity, $status = null)
}

//Insert the entity into the collection
parent::insert($entity);
$this->__entities->insert($entity);

return $this;
}
Expand All @@ -155,29 +162,36 @@ public function insert($entity, $status = null)
* This functions accepts either a know position or associative array of property/value pairs
*
* @param string|array $needle The position or the key or an associative array of column data to match
* @return ModelEntityComposite Returns a collection if successful. Otherwise NULL.
* @return ModelEntityComposite Returns a collection if successful or FALSE
*/
public function find($needle)
{
$result = null;

if(is_array($needle))
//Filter the objects
$objects = $this->__entities->filter(function($object) use ($needle)
{
$result = clone $this;

foreach($this as $entity)
if(is_array($needle))
{
foreach($needle as $key => $value)
{
if(!in_array($entity->{$key}, (array) $value)) {
$result->remove($entity);
if(!in_array($object->getProperty($key), (array) $value)) {
return false;
}
}
}
}
else return (bool) ($object->getHandle() == $needle);
});

$result = false;
if(is_array($needle) || count($objects))
{
//Create the entities
$result = clone $this;
$result->clear();

if(is_scalar($needle) && isset($this->_data[$needle])) {
$result = $this->_data[$needle];
//Create the resultset
foreach($objects as $object) {
$result->insert($object);
}
}

return $result;
Expand All @@ -198,7 +212,7 @@ public function remove($entity)
throw new \InvalidArgumentException('Entity needs to implement ModelEntityInterface');
}

return parent::remove($entity);
return $this->__entities->remove($entity);
}

/**
Expand All @@ -214,7 +228,7 @@ public function contains($entity)
throw new \InvalidArgumentException('Entity needs to implement ModelEntityInterface');
}

return parent::contains($entity);
return $this->__entities->contains($entity);
}

/**
Expand All @@ -226,7 +240,7 @@ public function save()
{
$result = false;

if (count($this))
if ($this->count())
{
$result = true;

Expand All @@ -253,7 +267,7 @@ public function delete()
{
$result = false;

if (count($this))
if ($this->count())
{
$result = true;

Expand All @@ -278,7 +292,7 @@ public function delete()
*/
public function clear()
{
$this->_data = array();
$this->__entities = $this->getObject('object.set');
return $this;
}

Expand Down Expand Up @@ -528,23 +542,122 @@ public function isConnected()
public function toArray()
{
$result = array();
foreach ($this as $key => $entity) {
$result[$key] = $entity->toArray();
if($entity = $this->getIterator()->current()) {
$result = $entity->toArray();
}

return $result;
}

/**
* Get a handle for this object
*
* This function returns an unique identifier for the object. This id can be used as a hash key for storing objects
* or for identifying an object
*
* @return string A string that is unique
*/
public function getHandle()
{
$result = false;
if($entity = $this->getIterator()->current()) {
$result = $entity->getHandle();
}

return $result;
}

/**
* Return a string representation of the set
*
* Required by interface \Serializable
*
* @return string A serialized object
*/
public function serialize()
{
return $this->__entities->serialize();
}

/**
* Unserializes a set from its string representation
*
* Required by interface \Serializable
*
* @param string $serialized The serialized data
*/
public function unserialize($serialized)
{
$this->__entities->unserialize($serialized);
}

/**
* Returns the number of elements in the collection.
*
* Required by the Countable interface
*
* @return int
*/
public function count()
{
return $this->__entities->count();
}

/**
* Defined by IteratorAggregate
*
* @return \ArrayIterator
*/
public function getIterator()
{
return $this->__entities->getIterator();
}

/**
* Set a property
*
* @param string $property The property name.
* @param mixed $value The property value.
* @return void
*/
final public function offsetSet($property, $value)
{
$this->setProperty($property, $value);
}

/**
* Get a property
*
* @param string $property The property name.
* @return mixed
* @param string $property The property name.
* @return mixed The property value
*/
final public function __get($property)
final public function offsetGet($property)
{
return $this->getProperty($property);
}

/**
* Check if a property exists
*
* @param string $property The property name.
* @return boolean
*/
final public function offsetExists($property)
{
return $this->hasProperty($property);
}

/**
* Remove a property
*
* @param string $property The property name.
* @return void
*/
final public function offsetUnset($property)
{
$this->removeProperty($property);
}

/**
* Set a property
*
Expand Down

0 comments on commit 569f436

Please sign in to comment.