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

Commit

Permalink
Merge remote-tracking branch 'origin/master' into feature/77-helper
Browse files Browse the repository at this point in the history
  • Loading branch information
johanjanssens committed May 3, 2016
2 parents 9b8048f + 422d4f8 commit b5aabf2
Show file tree
Hide file tree
Showing 6 changed files with 333 additions and 126 deletions.
154 changes: 129 additions & 25 deletions code/database/rowset/abstract.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\Database\Rowset
*/
abstract class DatabaseRowsetAbstract extends ObjectSet implements DatabaseRowsetInterface
abstract class DatabaseRowsetAbstract extends Object implements DatabaseRowsetInterface
{
/**
* The rowset
*
* @var ObjectSet
*/
private $__rowset;

/**
* Name of the identity column in the rowset
*
Expand Down Expand Up @@ -55,6 +62,7 @@ public function __construct(ObjectConfig $config)
{
parent::__construct($config);

//Set the prototypable
$this->_prototypable = $config->prototypable;

//Set the table identifier
Expand Down Expand Up @@ -151,7 +159,7 @@ public function insert($row, $status = null)
}

//Insert the row into the rowset
parent::insert($row);
$this->__rowset->insert($row);

return $this;
}
Expand All @@ -161,7 +169,7 @@ public function insert($row, $status = null)
*
* The row will be removed based on it's identity_column if set or otherwise by it's object handle.
*
* @param ObjectHandlable|DatabaseRowInterface $row
* @param DatabaseRowInterface $row
* @throws \InvalidArgumentException if the object doesn't implement DatabaseRowInterface
* @return DatabaseRowsetAbstract
*/
Expand All @@ -171,13 +179,13 @@ public function remove($row)
throw new \InvalidArgumentException('Row needs to implement DatabaseRowInterface');
}

return parent::remove($row);
return $this->__rowset->remove($row);
}

/**
* Checks if the collection contains a specific row
*
* @param ObjectHandlable|DatabaseRowInterface $row
* @param DatabaseRowInterface $row
* @throws \InvalidArgumentException if the object doesn't implement DatabaseRowInterface
* @return bool Returns TRUE if the object is in the set, FALSE otherwise
*/
Expand All @@ -187,7 +195,7 @@ public function contains($row)
throw new \InvalidArgumentException('Entity needs to implement ModelEntityInterface');
}

return parent::contains($row);
return $this->__rowset->contains($row);
}

/**
Expand Down Expand Up @@ -239,25 +247,32 @@ public function create(array $properties = array(), $status = null)
*/
public function find($needle)
{
$result = null;

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

foreach($this as $row)
if(is_array($needle))
{
foreach($needle as $key => $value)
{
if(!in_array($row->{$key}, (array) $value)) {
$result->remove($row);
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 @@ -272,7 +287,7 @@ public function save()
{
$result = false;

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

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

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

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

Expand Down Expand Up @@ -448,9 +463,6 @@ public function getComputedProperties()
}

/**
/**
*
* Returns the status
*
* @return string The status
Expand Down Expand Up @@ -594,9 +606,10 @@ public function setTable($table)
public function toArray()
{
$result = array();
foreach ($this as $key => $row) {
$result[$key] = $row->toArray();
if($row = $this->getIterator()->current()) {
$result = $row->toArray();
}

return $result;
}

Expand Down Expand Up @@ -643,6 +656,97 @@ public function isConnected()
return (bool)$this->getTable();
}

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

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

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

/**
* Defined by IteratorAggregate
*
* @return \ArrayIterator
*/
public function getIterator()
{
return $this->__rowset->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 The property value
*/
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);
}

/**
* Get a property
*
Expand Down
Loading

0 comments on commit b5aabf2

Please sign in to comment.