Skip to content

Commit

Permalink
Moved Arrays::walk to Objects::walk
Browse files Browse the repository at this point in the history
  • Loading branch information
maryo committed Oct 28, 2017
1 parent f5c6949 commit 5f8c1d0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 27 deletions.
26 changes: 0 additions & 26 deletions src/Arrays.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,32 +83,6 @@ public static function unset(array &$array, $path)
unset($array[$key]);
}

/**
* @param array|object $object
* @param callable $callback
* @param string[] $visitedObjects
*/
public static function walk($object, callable $callback, array $visitedObjects = [])
{
array_walk($object, function (&$value, string $property) use ($object, $callback, $visitedObjects) {
if (is_object($value)) {
$hash = spl_object_hash($value);

if (isset($visitedObjects[$hash])) {
return;
}

$visitedObjects[$hash] = true;
}

if (call_user_func_array($callback, [&$value, $property, $object]) !== false) {
if (is_array($value) || is_object($value)) {
self::walk($value, $callback, $visitedObjects);
}
}
});
}

public static function iterableToArray(iterable $iterable): array
{
if (is_array($iterable)) {
Expand Down
28 changes: 27 additions & 1 deletion src/Objects.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static function setPropertyValue($object, string $property, $value, $scop
{
$setPropertyValue = function ($value) use ($object, $property) {
if (is_object($object)) {
$object->$property = $value;
$object->$property = $value;
} else {
$object::$$property = $value;
}
Expand Down Expand Up @@ -98,4 +98,30 @@ public static function getConstants($class, string $prefix = ''): array

return $constants[$class][$prefix];
}

/**
* @param array|object $object
* @param callable $callback
* @param string[] $visitedObjects
*/
public static function walk($object, callable $callback, array $visitedObjects = [])
{
array_walk($object, function (&$value, string $property) use ($object, $callback, $visitedObjects) {
if (is_object($value)) {
$hash = spl_object_hash($value);

if (isset($visitedObjects[$hash])) {
return;
}

$visitedObjects[$hash] = true;
}

if (call_user_func_array($callback, [&$value, $property, $object]) !== false) {
if (is_array($value) || is_object($value)) {
self::walk($value, $callback, $visitedObjects);
}
}
});
}
}

0 comments on commit 5f8c1d0

Please sign in to comment.