|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Kaliop\eZMigrationBundle\Core\ReferenceResolver; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* A class which eases creating reference resolvers based on prefix strings. |
|
7
|
|
|
*/ |
|
8
|
|
|
abstract class PrefixBasedResolver implements PrefixBasedResolverInterface |
|
9
|
|
|
{ |
|
10
|
|
|
/// To be set by subclasses at constructor time or in definition |
|
11
|
|
|
protected $referencePrefixes = array(); |
|
12
|
|
|
|
|
13
|
|
|
private $prefixMatchRegexp; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* NB: always call this from constructor of subclasses! |
|
17
|
|
|
*/ |
|
18
|
|
|
public function __construct() |
|
19
|
|
|
{ |
|
20
|
|
|
$quotedPrefixes = []; |
|
21
|
|
|
foreach ($this->referencePrefixes as $prefix) { |
|
22
|
|
|
$quotedPrefixes[] = preg_quote($prefix, '/'); |
|
23
|
|
|
} |
|
24
|
|
|
$this->prefixMatchRegexp = '/^(' . implode('|', $quotedPrefixes) . ')/'; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function getRegexp() |
|
28
|
|
|
{ |
|
29
|
|
|
return $this->prefixMatchRegexp; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param string $stringIdentifier |
|
34
|
|
|
* @return bool |
|
35
|
|
|
*/ |
|
36
|
|
|
public function isReference($stringIdentifier) |
|
37
|
|
|
{ |
|
38
|
|
|
if (!is_string($stringIdentifier)) { |
|
39
|
|
|
return false; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
return (bool)preg_match($this->prefixMatchRegexp, $stringIdentifier); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function resolveReference($stringIdentifier) |
|
46
|
|
|
{ |
|
47
|
|
|
if ($this->isReference($stringIdentifier)) { |
|
48
|
|
|
return $this->getReferenceValue($stringIdentifier); |
|
49
|
|
|
} |
|
50
|
|
|
return $stringIdentifier; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param string $stringIdentifier |
|
55
|
|
|
* @return mixed |
|
56
|
|
|
*/ |
|
57
|
|
|
abstract public function getReferenceValue($stringIdentifier); |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Returns the value-identifying part of the reference identifier, stripped of its prefix. |
|
61
|
|
|
* Useful for subclasses with a single $referencePrefixes |
|
62
|
|
|
* |
|
63
|
|
|
* @param string $stringIdentifier |
|
64
|
|
|
* @return string |
|
65
|
|
|
*/ |
|
66
|
|
|
protected function getReferenceIdentifier($stringIdentifier) |
|
67
|
|
|
{ |
|
68
|
|
|
return preg_replace($this->prefixMatchRegexp, '', $stringIdentifier); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Useful for subclasses with many $referencePrefixes |
|
73
|
|
|
* |
|
74
|
|
|
* @param string $stringIdentifier |
|
75
|
|
|
* @return array with 2 keys, 'prefix' and 'identifier' |
|
76
|
|
|
* @throws \Exception |
|
77
|
|
|
*/ |
|
78
|
|
|
protected function getReferenceIdentifierByPrefix($stringIdentifier) |
|
79
|
|
|
{ |
|
80
|
|
|
foreach ($this->referencePrefixes as $prefix) { |
|
81
|
|
|
$regexp = '/^' . preg_quote($prefix, '/') . '/'; |
|
82
|
|
|
if (preg_match($regexp, $stringIdentifier)) { |
|
83
|
|
|
return array('prefix' => $prefix, 'identifier' => preg_replace($regexp, '', $stringIdentifier)); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
throw new \Exception("Can not match reference with identifier '$stringIdentifier'"); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|