Skip to content

Commit

Permalink
Merge 00b27aa into ba88c4d
Browse files Browse the repository at this point in the history
  • Loading branch information
kohkimakimoto committed Jul 21, 2014
2 parents ba88c4d + 00b27aa commit b467160
Showing 1 changed file with 56 additions and 15 deletions.
71 changes: 56 additions & 15 deletions src/Kohkimakimoto/EArray/EArray.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
<?php
/*
* This program was created by Kohki Makimoto <kohki.makimoto@gmail.com>
*/
namespace Kohkimakimoto\EArray;

/**
* EArray is a PHP Class to provide convenient ways to access a PHP Array.
* EArray is a PHP class to provide convenient ways to access a PHP Array.
*
* @author Kohki Makimoto <kohki.makimoto@gmail.com>
*/
Expand All @@ -14,26 +11,41 @@ class EArray implements \ArrayAccess, \Iterator, \Countable
const ORDER_LOW_TO_HIGH = 1;
const ORDER_HIGHT_TO_LOW = -1;

/**
* array data
* @var [type]
*/
protected $array;

/**
* Default delimiter
* @var [type]
*/
protected $delimiter;

/**
* Constructor
* @param Array $array
*/
public function __construct($array = array())
public function __construct($array = array(), $delimiter = "/")
{
if (!is_array($array)) {
throw new \RuntimeException("You need to pass Array to constructor.");
}
$this->array = $array;
$this->delimiter = $delimiter;
}

/**
* Get a value
* @param unknown $key
*/
public function get($key, $default = null, $delimiter = '/')
public function get($key, $default = null, $delimiter = null)
{
if ($delimiter === null) {
$delimiter = $this->delimiter;
}

$array = $this->array;

foreach (explode($delimiter, $key) as $k) {
Expand All @@ -52,8 +64,12 @@ public function get($key, $default = null, $delimiter = '/')
* @param unknown $key
* @param unknown $value
*/
public function set($key, $value, $delimiter = '/')
public function set($key, $value, $delimiter = null)
{
if ($delimiter === null) {
$delimiter = $this->delimiter;
}

if (strpos($key, $delimiter) === false) {
$this->array[$key] = $value;
return $this;
Expand Down Expand Up @@ -106,13 +122,22 @@ protected function convertMultidimentional($oneDimArray, $leafValue)
return $multiDimArray;
}

/**
* Get keys
* @return array keys
*/
public function getKeys()
{
return array_keys($this->array);
}

/**
* Sort a array.
* @param String $key
* @param String $delimiter
* @return EArray $earray
*/
public function sort($key = null, $delimiter = '/')
public function sort($key = null, $delimiter = null)
{
return $this->doSort($key, $delimiter, self::ORDER_LOW_TO_HIGH);
}
Expand All @@ -123,13 +148,17 @@ public function sort($key = null, $delimiter = '/')
* @param String $delimiter
* @return EArray $earray
*/
public function rsort($key = null, $delimiter = '/')
public function rsort($key = null, $delimiter = null)
{
return $this->doSort($key, $delimiter, self::ORDER_HIGHT_TO_LOW);
}

protected function doSort($key = null, $delimiter = '/', $order = 1)
protected function doSort($key = null, $delimiter = null, $order = 1)
{
if ($delimiter === null) {
$delimiter = $this->delimiter;
}

uasort($this->array, function($one, $another) use ($key, $delimiter, $order) {

$oneValue = null;
Expand Down Expand Up @@ -202,30 +231,42 @@ public function offsetUnset($offset) {
}

public function offsetGet($offset) {
return isset($this->array[$offset]) ? $this->array[$offset] : null;
$ret = isset($this->array[$offset]) ? $this->array[$offset] : null;
if (is_array($ret)) {
$ret = new EArray($ret);
}
return $ret;
}

public function current() {
return current($this->array);
$ret = current($this->array);
if (is_array($ret)) {
$ret = new EArray($ret);
}
return $ret;
}

public function key() {
return key($this->array);
}

public function next() {
return next($this->array);
$ret = next($this->array);
if (is_array($ret)) {
$ret = new EArray($ret);
}
return $ret;
}

public function rewind() {
reset($this->array);
}

public function valid() {
return ($this->current() !== false);
}

public function count() {
return count($this->array);
}
}
}

0 comments on commit b467160

Please sign in to comment.