1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kaliop\eZMigrationBundle\Core\ReferenceResolver; |
4
|
|
|
|
5
|
|
|
use Kaliop\eZMigrationBundle\API\EnumerableReferenceResolverInterface; |
6
|
|
|
|
7
|
|
|
class LoopResolver extends AbstractResolver implements EnumerableReferenceResolverInterface |
8
|
|
|
{ |
9
|
|
|
protected $referencePrefixes = array('loop:'); |
10
|
|
|
|
11
|
|
|
protected $stack = array(); |
12
|
|
|
|
13
|
1 |
|
public function beginLoop() |
14
|
|
|
{ |
15
|
1 |
|
$this->stack[] = array('step' => 0, 'key' => null, 'value' => null); |
16
|
1 |
|
} |
17
|
|
|
|
18
|
1 |
|
public function endLoop() |
19
|
|
|
{ |
20
|
1 |
|
array_pop($this->stack); |
21
|
1 |
|
} |
22
|
|
|
|
23
|
1 |
|
public function loopStep($key = null, $value = null) |
24
|
|
|
{ |
25
|
1 |
|
$idx = count($this->stack) - 1; |
26
|
1 |
|
$this->stack[$idx]['step'] = $this->stack[$idx]['step'] + 1; |
27
|
1 |
|
$this->stack[$idx]['key'] = $key; |
28
|
1 |
|
$this->stack[$idx]['value'] = $value; |
29
|
1 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param string $identifier format: 'loop:iteration', 'loop:depth', 'loop:key', 'loop:value' |
33
|
|
|
* @return int |
34
|
|
|
* @throws \Exception When trying to retrieve anything else but index and depth |
35
|
|
|
*/ |
36
|
1 |
|
public function getReferenceValue($identifier) |
37
|
|
|
{ |
38
|
1 |
|
switch(substr($identifier, 5)) { |
39
|
1 |
|
case 'iteration': |
40
|
1 |
|
$current = end($this->stack); |
41
|
1 |
|
return $current['step']; |
42
|
1 |
|
case 'key': |
43
|
1 |
|
$current = end($this->stack); |
44
|
1 |
|
return $current['key']; |
45
|
1 |
|
case 'value': |
46
|
1 |
|
$current = end($this->stack); |
47
|
1 |
|
return $current['value']; |
48
|
|
|
case 'depth': |
49
|
|
|
return count($this->stack); |
50
|
|
|
default: |
51
|
|
|
throw new \Exception("Can not resolve loop value '$identifier'"); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* We implement this method (interface) purely for convenience, as it allows this resolver to be added to the |
57
|
|
|
* 'custom-reference-resolver' chain and not break migration suspend/resume |
58
|
|
|
* @return array |
59
|
|
|
*/ |
60
|
1 |
|
public function listReferences() |
61
|
|
|
{ |
62
|
1 |
|
return array(); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|