Skip to content

Commit

Permalink
- Added Arrays::walk to deeply walk an array or object properties (br…
Browse files Browse the repository at this point in the history
…anches and leafs)

- Coding standards
  • Loading branch information
maryo committed Oct 28, 2017
1 parent 5acf3e2 commit f5c6949
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/Arrays.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,32 @@ 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
1 change: 1 addition & 0 deletions src/Enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ final public function __wakeup()
* Prevents implementing of the \Serializable interface.
*
* @codeCoverageIgnore
* @phpcsSuppress SlevomatCodingStandard.Classes.UnusedPrivateElements.UnusedMethod
*/
private function unserialize()
{}
Expand Down
1 change: 1 addition & 0 deletions tests/Fixtures/SerializableWakeableSleeper.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function serialize(): string

/**
* @param string $serialized
* @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint
*/
public function unserialize($serialized)
{
Expand Down

0 comments on commit f5c6949

Please sign in to comment.