Skip to content

Commit

Permalink
Вынесение реализации методов map и merge в отдельную библеотеку array…
Browse files Browse the repository at this point in the history
…-map
  • Loading branch information
petrgrishin committed Jun 1, 2014
1 parent f52a34c commit 9c7ef83
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"description": "PHP multi array access",
"license": "MIT",
"require": {
"php": ">=5.3.0"
"php": ">=5.3.0",
"petrgrishin/array-map": "~1.0"
},
"require-dev": {
"league/phpunit-coverage-listener": "~1.0"
Expand Down
24 changes: 13 additions & 11 deletions src/ArrayAccess.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@


use PetrGrishin\ArrayAccess\Exception\ArrayAccessException;
use PetrGrishin\ArrayMap\ArrayMap;

class ArrayAccess {
/** @var array */
Expand Down Expand Up @@ -145,22 +146,23 @@ public function remove($path) {
}

public function map($callback) {
if (!is_callable($callback)) {
throw new ArrayAccessException('Argument is not callable');
try {
$this->data = ArrayMap::create($this->data)
->map($callback)
->getArray();
} catch (\PetrGrishin\ArrayMap\Exception\ArrayMapException $e) {
throw new ArrayAccessException(sprintf('Error when mapping: %s', $e->getMessage()), null, $e);
}
$array = array();
foreach ($this->data as $key => $item) {
$array = array_merge_recursive($array, (array)call_user_func($callback, $item, $key));
}
$this->data = $array;
return $this;
}

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

0 comments on commit 9c7ef83

Please sign in to comment.