Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
TraverseTrait
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
13
100.00% covered (success)
100.00%
1 / 1
 traverseData
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
13
1<?php
2declare(strict_types=1);
3/**
4 * Pop PHP Framework (https://www.popphp.org/)
5 *
6 * @link       https://github.com/popphp/popphp-framework
7 * @author     Nick Sagona, III <nick@popphp.org>
8 * @copyright  Copyright (c) 2009-2026 Nick Sagona, III
9 * @license    https://www.popphp.org/license     New BSD License
10 */
11
12/**
13 * @namespace
14 */
15namespace Pop\Validator;
16
17use Pop\Utils;
18
19/**
20 * Traverse trait
21 *
22 * @category   Pop
23 * @package    Pop\Validator
24 * @author     Nick Sagona, III <nick@popphp.org>
25 * @copyright  Copyright (c) 2009-2026 Nick Sagona, III
26 * @license    https://www.popphp.org/license     New BSD License
27 * @version    5.0.0
28 */
29trait TraverseTrait
30{
31
32    /**
33     * Traverse data
34     *
35     * @param  string  $targetNode
36     * @param  mixed   $data
37     * @param  array   $nodeValues
38     * @param  ?string $currentNode
39     * @param  int     $depth
40     * @return void
41     */
42    public static function traverseData(
43        string $targetNode, mixed $data, array &$nodeValues = [], ?string &$currentNode = null, int &$depth = 0
44    ): void
45    {
46        if ($targetNode === $currentNode) {
47            $nodeValues[] = $data;
48        } else if (is_array($data)) {
49            foreach ($data as $key => $datum) {
50                if (!is_numeric($key)) {
51                    $currentNode = ($currentNode !== null) ? $currentNode . '.' . $key : $key;
52                }
53                $depth++;
54                self::traverseData($targetNode, $datum, $nodeValues, $currentNode, $depth);
55                $depth--;
56                $hasDot = ($currentNode !== null) && str_contains($currentNode, '.');
57                if (($hasDot && !is_numeric($key)) ||
58                    (is_numeric($key) && (($key + 1) == count($data)))) {
59                    $currentNode = $hasDot ? substr($currentNode, 0, strrpos($currentNode, '.')) : null;
60                } else if ($depth == 0) {
61                    $currentNode = null;
62                }
63            }
64        }
65    }
66
67}