Implement more ArrayTraits and use them in Iterator

This commit is contained in:
Matias Griese
2014-08-21 12:58:48 +03:00
parent ec3b5d6c73
commit 7d5426144d
8 changed files with 194 additions and 250 deletions

View File

@@ -1,7 +1,12 @@
<?php
namespace Grav\Common;
use Symfony\Component\Yaml\Yaml;
use Grav\Component\ArrayTraits\Constructor;
use Grav\Component\ArrayTraits\ArrayAccessWithGetters;
use Grav\Component\ArrayTraits\Iterator as ArrayIterator;
use Grav\Component\ArrayTraits\Countable;
use Grav\Component\ArrayTraits\Serializable;
use Grav\Component\ArrayTraits\Export;
/**
* Class Iterator
@@ -9,25 +14,12 @@ use Symfony\Component\Yaml\Yaml;
*/
class Iterator implements \ArrayAccess, \Iterator, \Countable, \Serializable
{
use Constructor, ArrayAccessWithGetters, ArrayIterator, Countable, Serializable, Export;
/**
* @var array
*/
protected $items = array();
/**
* @var bool
*/
protected $unset = false;
/**
* Constructor.
*
* @param array $items Initial items inside the iterator.
*/
public function __construct(array $items = array())
{
$this->items = $items;
}
protected $items;
/**
* Convert function calls for the existing keys into their values.
@@ -41,49 +33,6 @@ class Iterator implements \ArrayAccess, \Iterator, \Countable, \Serializable
return (isset($this->items[$key])) ? $this->items[$key] : null;
}
/**
* Array getter shorthand to get items.
*
* @param string $key
* @return mixed
*/
public function __get($key)
{
return (isset($this->items[$key])) ? $this->items[$key] : null;
}
/**
* Array setter shorthand to set the value.
*
* @param string $key
* @param mixed $value
*/
public function __set($key, $value)
{
$this->items[$key] = $value;
}
/**
* Array isset shorthand to set the value.
*
* @param string $key
* @return bool
*/
public function __isset($key)
{
return isset($this->items[$key]);
}
/**
* Array unset shorthand to remove the key.
*
* @param string $key
*/
public function __unset($key)
{
$this->offsetUnset($key);
}
/**
* Clone the iterator.
*/
@@ -215,180 +164,4 @@ class Iterator implements \ArrayAccess, \Iterator, \Countable, \Serializable
return $this;
}
// Implements export functions to array, YAML and JSON.
/**
* Return items as an array.
*
* @return array Array presentation of the iterator.
*/
public function toArray()
{
return $this->items;
}
/**
* Return YAML encoded string of items.
*
* @return string YAML presentation of the iterator.
*/
public function toYaml()
{
return Yaml::dump($this->items);
}
/**
* Return JSON encoded string of items.
*
* @return string JSON presentation of the iterator.
*/
public function toJson()
{
return json_encode($this->items);
}
// Implements Iterator.
/**
* Returns the current element.
*
* @return mixed Can return any type.
*/
public function current()
{
return current($this->items);
}
/**
* Returns the key of the current element.
*
* @return mixed Returns scalar on success, or NULL on failure.
*/
public function key()
{
return key($this->items);
}
/**
* Moves the current position to the next element.
*
* @return void
*/
public function next()
{
if ($this->unset) {
// If current item was unset, position is already in the next element (do nothing).
$this->unset = false;
} else {
next($this->items);
}
}
/**
* Rewinds back to the first element of the Iterator.
*
* @return void
*/
public function rewind()
{
$this->unset = false;
reset($this->items);
}
/**
* This method is called after Iterator::rewind() and Iterator::next() to check if the current position is valid.
*
* @return bool Returns TRUE on success or FALSE on failure.
*/
public function valid()
{
return key($this->items) !== null;
}
// Implements ArrayAccess
/**
* Whether or not an offset exists.
*
* @param mixed $offset An offset to check for.
* @return bool Returns TRUE on success or FALSE on failure.
*/
public function offsetExists($offset)
{
return isset($this->items[$offset]);
}
/**
* Returns the value at specified offset.
*
* @param mixed $offset The offset to retrieve.
* @return mixed Can return all value types.
*/
public function offsetGet($offset)
{
return isset($this->items[$offset]) ? $this->items[$offset] : null;
}
/**
* Assigns a value to the specified offset.
*
* @param mixed $offset The offset to assign the value to.
* @param mixed $value The value to set.
*/
public function offsetSet($offset, $value)
{
if (is_null($offset)) {
$this->items[] = $value;
} else {
$this->items[$offset] = $value;
}
}
/**
* Unsets an offset.
*
* @param mixed $offset The offset to unset.
*/
public function offsetUnset($offset)
{
if ($offset == key($this->items)) {
$this->unset = true;
}
unset($this->items[$offset]);
}
// Implements Countable
/**
* This method is executed when using the count() function.
*
* @return int The count of items.
*/
public function count()
{
return count($this->items);
}
// Implements Serializable
/**
* Returns string representation of the object.
*
* @return string Returns the string representation of the object.
*/
public function serialize()
{
return serialize($this->items);
}
/**
* Called during unserialization of the object.
*
* @param string $serialized The string representation of the object.
*/
public function unserialize($serialized)
{
$this->items = unserialize($serialized);
}
}

View File

@@ -2,7 +2,7 @@
namespace Grav\Component\ArrayTraits;
/**
* Class ArrayAccess
* Implements ArrayAccess interface
* @package Grav\Component\ArrayTraits
*
* @property array $items
@@ -10,8 +10,10 @@ namespace Grav\Component\ArrayTraits;
trait ArrayAccess
{
/**
* @param mixed $offset
* @return bool
* Whether or not an offset exists.
*
* @param mixed $offset An offset to check for.
* @return bool Returns TRUE on success or FALSE on failure.
*/
public function offsetExists($offset)
{
@@ -19,8 +21,10 @@ trait ArrayAccess
}
/**
* @param mixed $offset
* @return mixed
* Returns the value at specified offset.
*
* @param mixed $offset The offset to retrieve.
* @return mixed Can return all value types.
*/
public function offsetGet($offset)
{
@@ -28,19 +32,32 @@ trait ArrayAccess
}
/**
* @param mixed $offset
* @param mixed $value
* Assigns a value to the specified offset.
*
* @param mixed $offset The offset to assign the value to.
* @param mixed $value The value to set.
*/
public function offsetSet($offset, $value)
{
$this->items[$offset] = $value;
if (is_null($offset)) {
$this->items[] = $value;
} else {
$this->items[$offset] = $value;
}
}
/**
* @param mixed $offset
* Unsets an offset.
*
* @param mixed $offset The offset to unset.
*/
public function offsetUnset($offset)
{
// Hack to make Iterator trait work together with unset.
if (isset($this->iteratorUnset) && $offset == key($this->items)) {
$this->iteratorUnset = true;
}
unset($this->items[$offset]);
}
}

View File

@@ -1,7 +1,11 @@
<?php
namespace Grav\Component\ArrayTraits;
trait Getters
/**
* Implements all getters and setters
* @package Grav\Component\ArrayTraits
*/
trait ArrayAccessWithGetters
{
use ArrayAccess;

View File

@@ -2,7 +2,7 @@
namespace Grav\Component\ArrayTraits;
/**
* Class Constructor
* Implements Constructor
* @package Grav\Component\ArrayTraits
*
* @property array $items
@@ -10,7 +10,7 @@ namespace Grav\Component\ArrayTraits;
trait Constructor
{
/**
* Constructor.
* Constructor to initialize array.
*
* @param array $items Initial items inside the iterator.
*/

View File

@@ -2,7 +2,7 @@
namespace Grav\Component\ArrayTraits;
/**
* Class Countable
* Implements Countable interface
* @package Grav\Component\ArrayTraits
*
* @property array $items;
@@ -10,10 +10,12 @@ namespace Grav\Component\ArrayTraits;
trait Countable
{
/**
* Implements Countable interface.
*
* @return int
*/
public function count()
{
count($this->items);
return count($this->items);
}
}

View File

@@ -0,0 +1,43 @@
<?php
namespace Grav\Component\ArrayTraits;
use Symfony\Component\Yaml\Yaml;
/**
* Implements data export to array, YAML and JSON
* @package Grav\Component\ArrayTraits
*
* @property array $items
*/
trait Export
{
/**
* Convert blueprints into an array.
*
* @return array
*/
public function toArray()
{
return $this->items;
}
/**
* Convert blueprints into YAML string.
*
* @return string
*/
public function toYaml()
{
return Yaml::dump($this->items);
}
/**
* Convert blueprints into JSON string.
*
* @return string
*/
public function toJson()
{
return json_encode($this->items);
}
}

View File

@@ -0,0 +1,74 @@
<?php
namespace Grav\Component\ArrayTraits;
/**
* Implements Iterator interface
* @package Grav\Component\ArrayTraits
*
* @property array $items
*/
trait Iterator
{
/**
* Hack to make Iterator work together with unset().
*
* @var bool
*/
private $iteratorUnset = false;
/**
* Returns the current element.
*
* @return mixed Can return any type.
*/
public function current()
{
return current($this->items);
}
/**
* Returns the key of the current element.
*
* @return mixed Returns scalar on success, or NULL on failure.
*/
public function key()
{
return key($this->items);
}
/**
* Moves the current position to the next element.
*
* @return void
*/
public function next()
{
if ($this->iteratorUnset) {
// If current item was unset, position is already in the next element (do nothing).
$this->iteratorUnset = false;
} else {
next($this->items);
}
}
/**
* Rewinds back to the first element of the Iterator.
*
* @return void
*/
public function rewind()
{
$this->iteratorUnset = false;
reset($this->items);
}
/**
* This method is called after Iterator::rewind() and Iterator::next() to check if the current position is valid.
*
* @return bool Returns TRUE on success or FALSE on failure.
*/
public function valid()
{
return key($this->items) !== null;
}
}

View File

@@ -0,0 +1,31 @@
<?php
namespace Grav\Component\ArrayTraits;
/**
* Implements Serializable interface
* @package Grav\Component\ArrayTraits
*
* @property array $items
*/
trait Serializable
{
/**
* Returns string representation of the object.
*
* @return string Returns the string representation of the object.
*/
public function serialize()
{
return serialize($this->items);
}
/**
* Called during unserialization of the object.
*
* @param string $serialized The string representation of the object.
*/
public function unserialize($serialized)
{
$this->items = unserialize($serialized);
}
}