Skip to content

Instantly share code, notes, and snippets.

@jverdeyen
Created March 19, 2014 21:11
Show Gist options
  • Save jverdeyen/9651422 to your computer and use it in GitHub Desktop.
Save jverdeyen/9651422 to your computer and use it in GitHub Desktop.
ArrayAccess, Iteratable and countable object
<?php
namespace xxx\AppBundle\Model\User\Search;
class UserSearch implements \ArrayAccess, \IteratorAggregate, \Countable
{
private $data = [];
public function &__get ($key)
{
return $this->data[$key];
}
public function __set($key,$value)
{
$this->data[$key] = $value;
}
public function __isset ($key)
{
return isset($this->data[$key]);
}
public function __unset($key)
{
unset($this->data[$key]);
}
public function offsetSet($offset,$value)
{
if (is_null($offset)) {
$this->data[] = $value;
} else {
$this->data[$offset] = $value;
}
}
public function offsetExists($offset)
{
return isset($this->data[$offset]);
}
public function offsetUnset($offset)
{
if ($this->offsetExists($offset)) {
unset($this->data[$offset]);
}
}
public function offsetGet($offset)
{
return $this->offsetExists($offset) ? $this->data[$offset] : null;
}
public function getIterator() {
return new ArrayIterator($this->data);
}
public function count()
{
return count($this->data);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment