|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Kaliop\eZMigrationBundle\Core\Matcher; |
|
4
|
|
|
|
|
5
|
|
|
use eZ\Publish\API\Repository\Values\Content\Language; |
|
6
|
|
|
use Kaliop\eZMigrationBundle\API\Collection\LanguageCollection; |
|
7
|
|
|
use Kaliop\eZMigrationBundle\API\KeyMatcherInterface; |
|
8
|
|
|
|
|
9
|
|
View Code Duplication |
class LanguageMatcher extends RepositoryMatcher implements KeyMatcherInterface |
|
|
|
|
|
|
10
|
|
|
{ |
|
11
|
|
|
use FlexibleKeyMatcherTrait; |
|
12
|
|
|
|
|
13
|
|
|
const MATCH_LANGUAGE_ID = 'language_id'; |
|
14
|
|
|
const MATCH_LANGUAGE_CODE = 'language_code'; |
|
15
|
|
|
|
|
16
|
|
|
protected $allowedConditions = array( |
|
17
|
|
|
self::MATCH_ALL, self::MATCH_AND, self::MATCH_OR, self::MATCH_NOT, |
|
18
|
|
|
self::MATCH_LANGUAGE_ID, self::MATCH_LANGUAGE_CODE, |
|
19
|
|
|
// aliases |
|
20
|
|
|
'id', 'language' |
|
21
|
|
|
); |
|
22
|
|
|
protected $returns = 'Language'; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param array $conditions key: condition, value: int / string / int[] / string[] |
|
26
|
|
|
* @return LanguageCollection |
|
27
|
|
|
*/ |
|
28
|
|
|
public function match(array $conditions) |
|
29
|
|
|
{ |
|
30
|
|
|
return $this->matchLanguage($conditions); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param array $conditions key: condition, value: int / string / int[] / string[] |
|
35
|
|
|
* @return LanguageCollection |
|
36
|
|
|
*/ |
|
37
|
|
|
public function matchLanguage(array $conditions) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->validateConditions($conditions); |
|
40
|
|
|
|
|
41
|
|
|
foreach ($conditions as $key => $values) { |
|
42
|
|
|
|
|
43
|
|
|
if (!is_array($values)) { |
|
44
|
|
|
$values = array($values); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
switch ($key) { |
|
48
|
|
|
case 'id': |
|
49
|
|
|
case self::MATCH_LANGUAGE_ID: |
|
50
|
|
|
return new LanguageCollection($this->findLanguagesById($values)); |
|
51
|
|
|
|
|
52
|
|
|
case 'langauge_code': |
|
53
|
|
|
case self::MATCH_LANGUAGE_CODE: |
|
54
|
|
|
return new LanguageCollection($this->findLanguagesByCode($values)); |
|
55
|
|
|
|
|
56
|
|
|
case self::MATCH_ALL: |
|
57
|
|
|
return new LanguageCollection($this->findAllLanguages()); |
|
58
|
|
|
|
|
59
|
|
|
case self::MATCH_AND: |
|
60
|
|
|
return $this->matchAnd($values); |
|
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
case self::MATCH_OR: |
|
63
|
|
|
return $this->matchOr($values); |
|
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
case self::MATCH_NOT: |
|
66
|
|
|
return new LanguageCollection(array_diff_key($this->findAllLanguages(), $this->matchLanguage($values)->getArrayCopy())); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
protected function getConditionsFromKey($key) |
|
72
|
|
|
{ |
|
73
|
|
|
if (is_int($key) || ctype_digit($key)) { |
|
74
|
|
|
return array(self::MATCH_LANGUAGE_ID => $key); |
|
75
|
|
|
} |
|
76
|
|
|
return array(self::MATCH_LANGUAGE_CODE => $key); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param int[] $languageIds |
|
81
|
|
|
* @return Language[] |
|
82
|
|
|
*/ |
|
83
|
|
|
protected function findLanguagesById(array $languageIds) |
|
84
|
|
|
{ |
|
85
|
|
|
$languages = []; |
|
86
|
|
|
|
|
87
|
|
|
foreach ($languageIds as $languageId) { |
|
88
|
|
|
// return unique contents |
|
89
|
|
|
$language = $this->repository->getContentLanguageService()->loadLanguageById($languageId); |
|
90
|
|
|
$languages[$language->id] = $language; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return $languages; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param string[] $languageIdentifiers |
|
98
|
|
|
* @return Language[] |
|
99
|
|
|
*/ |
|
100
|
|
|
protected function findLanguagesByCode(array $languageIdentifiers) |
|
101
|
|
|
{ |
|
102
|
|
|
$languages = []; |
|
103
|
|
|
|
|
104
|
|
|
foreach ($languageIdentifiers as $languageIdentifier) { |
|
105
|
|
|
// return unique contents |
|
106
|
|
|
$language = $this->repository->getContentLanguageService()->loadLanguage($languageIdentifier); |
|
107
|
|
|
$languages[$language->id] = $language; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return $languages; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @return Language[] |
|
115
|
|
|
*/ |
|
116
|
|
|
protected function findAllLanguages() |
|
117
|
|
|
{ |
|
118
|
|
|
$languages = []; |
|
119
|
|
|
|
|
120
|
|
|
foreach ($this->repository->getLanguageService()->loadLanguages() as $language) { |
|
|
|
|
|
|
121
|
|
|
// return unique contents |
|
122
|
|
|
$languages[$language->id] = $language; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
return $languages; |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.