A fast and light-weight component library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
2.2 KiB

<?php
7 years ago
/**
7 years ago
* Collection class with dynamic members
* Similar functionality to an Array or Dictionary class
* Typically holding a collection/array of Entity members
* This class is not particularly useful compared to a standard array,
7 years ago
* but it's used to `extend` the functionality of standard arrays.
7 years ago
*/
7 years ago
namespace Twister\ORM;
class Collection implements \Iterator, \Countable, \ArrayAccess
{
7 years ago
protected $members = null;
7 years ago
public function __construct(array $members = null)
{
7 years ago
$this->members =& $members;
}
7 years ago
/**
7 years ago
* Get member by id/index
7 years ago
*
7 years ago
* Note: This is not very useful, because most members will be indexed by integer.
*
* @param string|int $idx
* @return mixed
7 years ago
*/
public function __get($idx)
{
7 years ago
return $this->members[$idx];
}
7 years ago
/**
7 years ago
* Set member by id/index
7 years ago
*
7 years ago
* @param string|int $idx
* @param mixed $value
* @return void
7 years ago
*/
public function __set($idx, $value)
{
7 years ago
$this->members[$idx] = $value;
}
7 years ago
function __isset($idx)
{
7 years ago
return isset($this->members[$idx]);
}
7 years ago
function __unset($idx)
{
7 years ago
unset($this->members[$idx]);
}
7 years ago
/**
* Workaround for the `array access functions` eg. array_push($obj->toArray(), $value);
*/
public function &toArray()
{
return $this->members;
}
7 years ago
/**
* Iterator interface
*/
public function rewind()
{
7 years ago
return reset($this->members);
}
public function current()
{
7 years ago
return current($this->members);
}
public function key()
{
7 years ago
return key($this->members);
}
public function next()
{
7 years ago
return next($this->members);
}
public function valid()
{
7 years ago
return key($this->members) !== null;
}
7 years ago
/**
* Countable interface
*/
public function count()
{
7 years ago
return count($this->members);
}
7 years ago
/**
* ArrayAccess interface
*/
public function offsetGet($idx) // eg. var_dump($obj['two']);
{
7 years ago
return $this->members[$idx];
}
7 years ago
public function offsetSet($idx, $value) // eg. $obj['two'] = 'A value';
{
7 years ago
$this->members[$idx] = $value;
}
7 years ago
public function offsetExists($idx) // eg. isset($obj['two'])
{
7 years ago
return isset($this->members[$idx]);
}
7 years ago
public function offsetUnset($idx) // eg. unset($obj['two']);
{
7 years ago
unset($this->members[$idx]);
}
}