|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Kaliop\eZMigrationBundle\Core\ReferenceResolver; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Used to turn regexp-based matchers (which do match whole strings) into embedded-matchers (used to match parts of strings). |
|
7
|
|
|
* Implements EmbeddedReferenceResolverInterface - but in php Traits can not implement interfaces... |
|
8
|
|
|
*/ |
|
9
|
|
|
trait EmbeddedRegexpReferenceResolverTrait |
|
10
|
|
|
{ |
|
11
|
|
|
protected $beginToken = '['; |
|
12
|
|
|
protected $endToken = ']'; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @param string $string |
|
16
|
|
|
* @return bool true if the given $stringIdentifier contains at least one occurrence of the reference(s) |
|
17
|
|
|
*/ |
|
18
|
|
|
public function hasEmbeddedReferences($string) |
|
19
|
|
|
{ |
|
20
|
|
|
$regexp = $this->getEmbeddedRegexp(); |
|
21
|
|
|
return (bool) preg_match_all($regexp, $string, $matches); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Returns the $string with eventual refs resolved |
|
26
|
|
|
* |
|
27
|
|
|
* @param string $string |
|
28
|
|
|
* @return string |
|
29
|
|
|
* @todo q: if reference is an array, should we recurse on it ? |
|
30
|
|
|
*/ |
|
31
|
75 |
|
public function resolveEmbeddedReferences($string) |
|
32
|
|
|
{ |
|
33
|
75 |
|
$regexp = $this->getEmbeddedRegexp(); |
|
34
|
75 |
|
$count = preg_match_all($regexp, $string, $matches); |
|
35
|
|
|
// $matches[0][] will have the matched full string eg.: [reference:example_reference] |
|
36
|
75 |
|
if ($count) { |
|
|
|
|
|
|
37
|
5 |
|
foreach ($matches[0] as $referenceIdentifier) { |
|
38
|
5 |
|
$reference = $this->getReferenceValue(substr($referenceIdentifier, 1, -1)); |
|
39
|
5 |
|
if (!is_array($reference)) { |
|
40
|
5 |
|
$string = str_replace($referenceIdentifier, $reference, $string); |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
75 |
|
return $string; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* NB: here we assume that all regexp resolvers give us a regexp with a very specific format, notably using '/' as |
|
50
|
|
|
* delimiter...... |
|
51
|
|
|
* @return string |
|
52
|
|
|
* @todo make the start and end tokens flexible (it probably wont work well if we use eg. '}}' as end token) |
|
53
|
|
|
*/ |
|
54
|
75 |
|
protected function getEmbeddedRegexp() |
|
55
|
|
|
{ |
|
56
|
|
|
// we need to alter the regexp we usr for std ref resolving, as it will be used to match parts of text, not the whole string |
|
57
|
75 |
|
$regexp = substr($this->getRegexp(), 1, -1); |
|
58
|
75 |
|
return '/' . preg_quote($this->beginToken). preg_replace(array('/^\^/'), array(''), $regexp) . '[^' . $this->endToken . ']+' . preg_quote($this->endToken) . '/'; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param mixed $stringOrArray |
|
63
|
|
|
* @return array|string |
|
64
|
|
|
* @todo decide what to do with this method... |
|
65
|
|
|
*/ |
|
66
|
|
|
/*protected function resolveEmbeddedReferencesRecursively($stringOrArray) |
|
67
|
|
|
{ |
|
68
|
|
|
if (is_array($stringOrArray)) { |
|
69
|
|
|
foreach ($stringOrArray as $condition => $values) { |
|
70
|
|
|
$stringOrArray[$condition] = $this->resolveEmbeddedReferencesRecursively($values); |
|
71
|
|
|
} |
|
72
|
|
|
return $stringOrArray; |
|
73
|
|
|
} else { |
|
74
|
|
|
return $this->resolveEmbeddedReferences($stringOrArray); |
|
75
|
|
|
} |
|
76
|
|
|
}*/ |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Has to be implemented in the class which uses this trait |
|
80
|
|
|
* @return string |
|
81
|
|
|
*/ |
|
82
|
|
|
abstract public function getRegexp(); |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Has to be implemented in the class which uses this trait |
|
86
|
|
|
* @param string $stringIdentifier |
|
87
|
|
|
* @return mixed |
|
88
|
|
|
*/ |
|
89
|
|
|
abstract public function getReferenceValue($stringIdentifier); |
|
90
|
|
|
} |
|
91
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: