Skip to content

Commit

Permalink
Внедрение ArrayObject. Рефакторинг методов ArrayMap.
Browse files Browse the repository at this point in the history
  • Loading branch information
petrgrishin committed Jun 25, 2014
1 parent f1171ac commit 4374cab
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 104 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"license": "MIT",
"require": {
"php": ">=5.3.0",
"petrgrishin/array-map": "~1.0"
"petrgrishin/array-map": "dev-master",
"petrgrishin/array-object": "~1.0"
},
"require-dev": {
"league/phpunit-coverage-listener": "~1.0"
Expand Down
71 changes: 17 additions & 54 deletions src/ArrayAccess.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@
use PetrGrishin\ArrayAccess\Exception\ArrayAccessException;
use PetrGrishin\ArrayMap\ArrayMap;
use PetrGrishin\ArrayMap\Exception\ArrayMapException;
use PetrGrishin\ArrayObject\BaseArrayObject;

class ArrayAccess {
/** @var array */
private $data;
class ArrayAccess extends BaseArrayObject {
/** @var string */
private $pathDelimiter;
/** @var ArrayMap */
private $map;

/**
* @return string
Expand All @@ -28,15 +29,15 @@ public static function className() {
* @param string|null $pathDelimiter
* @return static
*/
public static function create(array $data = null, $pathDelimiter = null) {
public static function create($data = null, $pathDelimiter = null) {
return new static($data, $pathDelimiter);
}

/**
* @param array|null $data
* @param string|null $pathDelimiter
*/
public function __construct(array $data = null, $pathDelimiter = null) {
public function __construct($data = null, $pathDelimiter = null) {
$this->setArray($data ?: array());
$this->setPathDelimiter($pathDelimiter ?: '.');
}
Expand All @@ -50,30 +51,14 @@ public function setPathDelimiter($delimiter) {
return $this;
}

/**
* @param array $data
* @return $this
*/
public function setArray(array $data) {
$this->data = $data;
return $this;
}

/**
* @return array
*/
public function getArray() {
return $this->data;
}

/**
* @param string $path
* @param mixed $defaultValue
* @throws Exception\ArrayAccessException
* @return mixed
*/
public function getValue($path, $defaultValue = null) {
$array = $this->data;
$array = $this->getArray();
if (array_key_exists($path, $array)) {
return $array[$path];
}
Expand Down Expand Up @@ -105,7 +90,8 @@ public function getValue($path, $defaultValue = null) {
* @throws Exception\ArrayAccessException
*/
public function setValue($path, $value) {
$array = & $this->data;
$data = $this->getArray();
$array = & $data;
$keys = explode($this->pathDelimiter, $path);
while (count($keys) > 1) {
$key = array_shift($keys);
Expand All @@ -118,6 +104,7 @@ public function setValue($path, $value) {
}
$key = array_shift($keys);
$array[$key] = $value;
$this->setArray($data);
return $this;
}

Expand All @@ -127,7 +114,8 @@ public function setValue($path, $value) {
* @throws Exception\ArrayAccessException
*/
public function remove($path) {
$array = & $this->data;
$data = $this->getArray();
$array = & $data;
$keys = explode($this->pathDelimiter, $path);
while (count($keys) > 1) {
$key = array_shift($keys);
Expand All @@ -143,39 +131,14 @@ public function remove($path) {
throw new ArrayAccessException(sprintf('Not exist key'));
}
unset($array[$key]);
$this->setArray($data);
return $this;
}

public function map($callback) {
try {
$this->data = ArrayMap::create($this->data)
->map($callback)
->getArray();
} catch (ArrayMapException $e) {
throw new ArrayAccessException(sprintf('Error when mapping: %s', $e->getMessage()), null, $e);
public function getMap() {
if ($this->map) {
$this->map = ArrayMap::create($this);
}
return $this;
}

public function mergeWith(array $data, $recursive = true) {
try {
$this->data = ArrayMap::create($this->data)
->mergeWith($data, $recursive)
->getArray();
} catch (ArrayMapException $e) {
throw new ArrayAccessException(sprintf('Error when merge: %s', $e->getMessage()), null, $e);
}
return $this;
}

public function filter($callback) {
try {
$this->data = ArrayMap::create($this->data)
->filter($callback)
->getArray();
} catch (ArrayMapException $e) {
throw new ArrayAccessException(sprintf('Error when filter: %s', $e->getMessage()), null, $e);
}
return $this;
return $this->map;
}
}
98 changes: 49 additions & 49 deletions tests/unit/ArrayAccessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,53 +158,53 @@ public function testRemoveElementByNotExistKeyInPath() {
$instance->remove('notExistKye1.notExistKye2');
}

public function testSimpleMapping() {
$original = array(1, 2, 3);
$instance = ArrayAccess::create($original);
$instance->map(function ($value) {
return $value * 2;
});
$this->assertEquals(array(2, 4, 6), $instance->getArray());
}

public function testKeyMapping() {
$original = array(1 => 1, 2 => 2, 3 => 3);
$instance = ArrayAccess::create($original);
$instance->map(function ($value, $key) {
return array(($key - 1) => $value * 2);
});
$this->assertEquals(array(0 => 2, 1 => 4, 2 => 6), $instance->getArray());
}

public function testMergeWith() {
$original = array(1, 2, 3);
$instance = ArrayAccess::create($original);
$instance->mergeWith(array(4, 5, 6), false);
$this->assertEquals(array(1, 2, 3, 4, 5, 6), $instance->getArray());
}

public function testRecursiveMergeWith() {
$original = array('a' => array(1), 'b', 'c');
$instance = ArrayAccess::create($original);
$instance->mergeWith(array('a' => array(2), 'd', 'e'));
$this->assertEquals(array('a' => array(1, 2), 'b', 'c', 'd', 'e'), $instance->getArray());
}

public function testFiltering() {
$original = array('a' => 1, 'b' => 2, 'c' => 3);
$instance = ArrayAccess::create($original);
$instance->filter(function ($value) {
return $value > 2;
});
$this->assertEquals(array('c' => 3), $instance->getArray());
}

public function testFilteringUseKeys() {
$original = array('a' => 1, 'b' => 2, 'c' => 3);
$instance = ArrayAccess::create($original);
$instance->filter(function ($value, $key) {
return $key === 'c';
});
$this->assertEquals(array('c' => 3), $instance->getArray());
}
// public function testSimpleMapping() {
// $original = array(1, 2, 3);
// $instance = ArrayAccess::create($original);
// $instance->map(function ($value) {
// return $value * 2;
// });
// $this->assertEquals(array(2, 4, 6), $instance->getArray());
// }
//
// public function testKeyMapping() {
// $original = array(1 => 1, 2 => 2, 3 => 3);
// $instance = ArrayAccess::create($original);
// $instance->map(function ($value, $key) {
// return array(($key - 1) => $value * 2);
// });
// $this->assertEquals(array(0 => 2, 1 => 4, 2 => 6), $instance->getArray());
// }
//
// public function testMergeWith() {
// $original = array(1, 2, 3);
// $instance = ArrayAccess::create($original);
// $instance->mergeWith(array(4, 5, 6), false);
// $this->assertEquals(array(1, 2, 3, 4, 5, 6), $instance->getArray());
// }
//
// public function testRecursiveMergeWith() {
// $original = array('a' => array(1), 'b', 'c');
// $instance = ArrayAccess::create($original);
// $instance->mergeWith(array('a' => array(2), 'd', 'e'));
// $this->assertEquals(array('a' => array(1, 2), 'b', 'c', 'd', 'e'), $instance->getArray());
// }
//
// public function testFiltering() {
// $original = array('a' => 1, 'b' => 2, 'c' => 3);
// $instance = ArrayAccess::create($original);
// $instance->filter(function ($value) {
// return $value > 2;
// });
// $this->assertEquals(array('c' => 3), $instance->getArray());
// }
//
// public function testFilteringUseKeys() {
// $original = array('a' => 1, 'b' => 2, 'c' => 3);
// $instance = ArrayAccess::create($original);
// $instance->filter(function ($value, $key) {
// return $key === 'c';
// });
// $this->assertEquals(array('c' => 3), $instance->getArray());
// }
}

0 comments on commit 4374cab

Please sign in to comment.