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