|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Kaliop\eZMigrationBundle\Core\ReferenceResolver; |
|
4
|
|
|
|
|
5
|
|
|
use Kaliop\eZMigrationBundle\API\ReferenceResolverInterface; |
|
6
|
|
|
use Kaliop\eZMigrationBundle\API\ReferenceBagInterface; |
|
7
|
|
|
|
|
8
|
|
|
class ChainResolver implements ReferenceResolverInterface, ReferenceBagInterface |
|
9
|
|
|
{ |
|
10
|
|
|
/** @var ReferenceResolverInterface[] $resolvers */ |
|
11
|
|
|
protected $resolvers = array(); |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @param ReferenceResolverInterface[] $resolvers |
|
15
|
|
|
*/ |
|
16
|
27 |
|
public function __construct(array $resolvers) |
|
17
|
|
|
{ |
|
18
|
27 |
|
$this->resolvers = $resolvers; |
|
19
|
27 |
|
} |
|
20
|
|
|
|
|
21
|
27 |
|
public function addResolver(ReferenceResolverInterface $resolver) |
|
22
|
|
|
{ |
|
23
|
27 |
|
$this->resolvers[] = $resolver; |
|
24
|
27 |
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param string $stringIdentifier |
|
28
|
|
|
* @return bool |
|
29
|
|
|
*/ |
|
30
|
14 |
|
public function isReference($stringIdentifier) |
|
31
|
|
|
{ |
|
32
|
14 |
|
foreach ($this->resolvers as $resolver) { |
|
33
|
14 |
|
if ($resolver->isReference($stringIdentifier)) { |
|
34
|
12 |
|
return true; |
|
35
|
|
|
} |
|
36
|
13 |
|
} |
|
37
|
|
|
|
|
38
|
11 |
|
return false; |
|
39
|
1 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param string $stringIdentifier |
|
43
|
|
|
* @return mixed |
|
44
|
|
|
*/ |
|
45
|
12 |
|
public function getReferenceValue($stringIdentifier) |
|
46
|
|
|
{ |
|
47
|
12 |
|
$resolvedOnce = false; |
|
48
|
|
|
|
|
49
|
12 |
|
foreach ($this->resolvers as $resolver) { |
|
50
|
12 |
|
if ($resolver->isReference($stringIdentifier)) { |
|
51
|
12 |
|
$stringIdentifier = $resolver->getReferenceValue($stringIdentifier); |
|
52
|
11 |
|
$resolvedOnce = true; |
|
53
|
11 |
|
} |
|
54
|
11 |
|
} |
|
55
|
|
|
|
|
56
|
11 |
|
if (!$resolvedOnce) { |
|
57
|
|
|
throw new \Exception("Could not resolve reference with identifier: '$stringIdentifier'"); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
11 |
|
return $stringIdentifier; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
13 |
|
public function resolveReference($stringIdentifier) |
|
64
|
|
|
{ |
|
65
|
13 |
|
if ($this->isReference($stringIdentifier)) { |
|
66
|
12 |
|
return $this->getReferenceValue($stringIdentifier); |
|
67
|
|
|
} |
|
68
|
10 |
|
return $stringIdentifier; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Tries to add the reference to one of the resolvers in the chain (the first accepting it) |
|
73
|
|
|
* |
|
74
|
|
|
* @param string $identifier |
|
75
|
|
|
* @param mixed $value |
|
76
|
|
|
* @param bool $overwrite do overwrite the existing ref if it exist without raising an exception |
|
77
|
|
|
* @return bool |
|
78
|
|
|
*/ |
|
79
|
11 |
|
public function addReference($identifier, $value, $overwrite = false) |
|
80
|
|
|
{ |
|
81
|
11 |
|
foreach ($this->resolvers as $resolver) { |
|
82
|
11 |
|
if ($resolver instanceof ReferenceBagInterface) { |
|
83
|
11 |
|
if ($resolver->addReference($identifier, $value, $overwrite)) { |
|
84
|
11 |
|
return true; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return false; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|