|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of library-template |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
* |
|
8
|
|
|
* @author Nicolò Martini <[email protected]> |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace StringTemplate; |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
use RecursiveArrayIterator; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class NestedKeyIterator |
|
18
|
|
|
* |
|
19
|
|
|
* This iterator iterates recoursively through an array, |
|
20
|
|
|
* returning as the current key an imploded list of the stacked keys |
|
21
|
|
|
* separated by a given separator. |
|
22
|
|
|
* |
|
23
|
|
|
* Example: |
|
24
|
|
|
* <code> |
|
25
|
|
|
* $ary = [ |
|
26
|
|
|
* 'foo' => 'bar', |
|
27
|
|
|
* 'baz' => ['foo' => ['uh' => 'ah'], 'oh' => 'eh'] |
|
28
|
|
|
* ] |
|
29
|
|
|
* foreach (new NestedKeyIterator($ary) as $key => $value) { |
|
30
|
|
|
* echo "$key: value\n", |
|
31
|
|
|
* } |
|
32
|
|
|
* </code> |
|
33
|
|
|
* prints |
|
34
|
|
|
* foo: bar |
|
35
|
|
|
* baz.foo.uh: ah |
|
36
|
|
|
* baz.oh: eh |
|
37
|
|
|
* |
|
38
|
|
|
* @package StringTemplate |
|
39
|
|
|
*/ |
|
40
|
|
|
class NestedKeyIterator extends \RecursiveIteratorIterator |
|
41
|
|
|
{ |
|
42
|
|
|
private $stack = array(); |
|
43
|
|
|
private $keySeparator; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param \Traversable $iterator |
|
47
|
|
|
* @param string $separator |
|
48
|
|
|
* @param int $mode |
|
49
|
|
|
* @param int $flags |
|
50
|
|
|
*/ |
|
51
|
|
|
public function __construct(\Traversable $iterator, $separator = '.', $mode = \RecursiveIteratorIterator::LEAVES_ONLY, $flags = 0) |
|
52
|
|
|
{ |
|
53
|
|
|
$this->keySeparator = $separator; |
|
54
|
|
|
parent::__construct($iterator, $mode, $flags); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* {@inheritdoc} |
|
59
|
|
|
*/ |
|
60
|
|
|
public function callGetChildren() |
|
61
|
|
|
{ |
|
62
|
|
|
$this->stack[] = parent::key(); |
|
|
|
|
|
|
63
|
|
|
return parent::callGetChildren(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* {@inheritdoc} |
|
68
|
|
|
*/ |
|
69
|
|
|
public function endChildren() |
|
70
|
|
|
{ |
|
71
|
|
|
parent::endChildren(); |
|
72
|
|
|
array_pop($this->stack); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* {@inheritdoc} |
|
77
|
|
|
*/ |
|
78
|
|
|
public function key() |
|
79
|
|
|
{ |
|
80
|
|
|
$keys = $this->stack; |
|
81
|
|
|
$keys[] = parent::key(); |
|
82
|
|
|
|
|
83
|
|
|
return implode($this->keySeparator, $keys); |
|
84
|
|
|
} |
|
85
|
|
|
} |
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()method in theSoncalls the wrong method in the parent class.