1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kaliop\eZMigrationBundle\Core\Matcher; |
4
|
|
|
|
5
|
|
|
use eZ\Publish\API\Repository\Exceptions\NotFoundException; |
6
|
|
|
use eZ\Publish\API\Repository\Values\Content\UrlWildcard; |
7
|
|
|
use Kaliop\eZMigrationBundle\API\Collection\UrlWildcardCollection; |
8
|
|
|
use Kaliop\eZMigrationBundle\API\Exception\InvalidMatchConditionsException; |
9
|
|
|
use Kaliop\eZMigrationBundle\API\KeyMatcherInterface; |
10
|
|
|
|
11
|
|
|
class UrlWildcardMatcher extends RepositoryMatcher implements KeyMatcherInterface |
12
|
|
|
{ |
13
|
|
|
use FlexibleKeyMatcherTrait; |
14
|
|
|
|
15
|
|
|
const MATCH_URL_ID = 'url_id'; |
16
|
|
|
|
17
|
|
|
protected $allowedConditions = array( |
18
|
|
|
self::MATCH_ALL, self::MATCH_AND, self::MATCH_OR, self::MATCH_NOT, |
19
|
|
|
self::MATCH_URL_ID, |
20
|
|
|
// aliases |
21
|
|
|
'id' |
22
|
|
|
); |
23
|
|
|
protected $returns = 'UrlWildcard'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param array $conditions key: condition, value: int / string / int[] / string[] |
27
|
|
|
* @param bool $tolerateMisses |
28
|
|
|
* @return UrlWildcardCollection |
29
|
|
|
* @throws InvalidMatchConditionsException |
30
|
|
|
* @throws NotFoundException |
31
|
|
|
*/ |
32
|
|
|
public function match(array $conditions, $tolerateMisses = false) |
33
|
|
|
{ |
34
|
|
|
return $this->matchUrlWildcard($conditions, $tolerateMisses); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param array $conditions key: condition, value: int / string / int[] / string[] |
39
|
|
|
* @param bool $tolerateMisses |
40
|
|
|
* @return UrlWildcardCollection |
41
|
|
|
* @throws InvalidMatchConditionsException |
42
|
|
|
* @throws NotFoundException |
43
|
|
|
*/ |
44
|
|
|
public function matchUrlWildcard(array $conditions, $tolerateMisses = false) |
45
|
|
|
{ |
46
|
|
|
$this->validateConditions($conditions); |
47
|
|
|
|
48
|
|
|
foreach ($conditions as $key => $values) { |
49
|
|
|
|
50
|
|
|
if (!is_array($values)) { |
51
|
|
|
$values = array($values); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
switch ($key) { |
55
|
|
|
case 'id': |
56
|
|
|
case self::MATCH_URL_ID: |
57
|
|
|
return new UrlWildcardCollection($this->findUrlWildcardsById($values, $tolerateMisses)); |
58
|
|
|
|
59
|
|
|
case self::MATCH_ALL: |
60
|
|
|
return new UrlWildcardCollection($this->findAllUrlWildcards()); |
61
|
|
|
|
62
|
|
|
case self::MATCH_AND: |
63
|
|
|
return $this->matchAnd($values, $tolerateMisses = false); |
|
|
|
|
64
|
|
|
|
65
|
|
|
case self::MATCH_OR: |
66
|
|
|
return $this->matchOr($values, $tolerateMisses = false); |
|
|
|
|
67
|
|
|
|
68
|
|
|
case self::MATCH_NOT: |
69
|
|
|
return new UrlWildcardCollection(array_diff_key($this->findAllUrlWildcards(), $this->matchUrlWildcard($values, true)->getArrayCopy())); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
protected function getConditionsFromKey($key) |
75
|
|
|
{ |
76
|
|
|
//if (is_int($key) || ctype_digit($key)) { |
77
|
|
|
return array(self::MATCH_URL_ID => $key); |
78
|
|
|
//} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param int[] $urlIds |
83
|
|
|
* @param bool $tolerateMisses |
84
|
|
|
* @return UrlWildcard[] |
85
|
|
|
* @throws NotFoundException |
86
|
|
|
*/ |
87
|
|
|
protected function findUrlWildcardsById(array $urlIds, $tolerateMisses = false) |
88
|
|
|
{ |
89
|
|
|
$urls = []; |
90
|
|
|
|
91
|
|
|
foreach ($urlIds as $urlId) { |
92
|
|
|
try { |
93
|
|
|
// return unique contents |
94
|
|
|
$urlWildcard = $this->repository->getURLWildcardService()->load($urlId); |
95
|
|
|
$urls[$urlWildcard->id] = $urlWildcard; |
96
|
|
|
} catch (NotFoundException $e) { |
97
|
|
|
if (!$tolerateMisses) { |
98
|
|
|
throw $e; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $urls; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return UrlWildcard[] |
108
|
|
|
*/ |
109
|
|
|
protected function findAllUrlWildcards() |
110
|
|
|
{ |
111
|
|
|
$urls = []; |
112
|
|
|
|
113
|
|
|
foreach ($this->repository->getURLWildcardService()->loadAll() as $urlWildcard) { |
114
|
|
|
// return unique contents |
115
|
|
|
$urls[$urlWildcard->id] = $urlWildcard; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $urls; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|