1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* File containing the Language Handler class. |
5
|
|
|
* |
6
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
7
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
namespace eZ\Publish\Core\Persistence\Legacy\Content\Language; |
10
|
|
|
|
11
|
|
|
use eZ\Publish\SPI\Persistence\Content\Language; |
12
|
|
|
use eZ\Publish\SPI\Persistence\Content\Language\Handler as BaseLanguageHandler; |
13
|
|
|
use eZ\Publish\SPI\Persistence\Content\Language\CreateStruct; |
14
|
|
|
use eZ\Publish\Core\Base\Exceptions\NotFoundException; |
15
|
|
|
use LogicException; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Language Handler. |
19
|
|
|
*/ |
20
|
|
|
class Handler implements BaseLanguageHandler |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Language Gateway. |
24
|
|
|
* |
25
|
|
|
* @var \eZ\Publish\Core\Persistence\Legacy\Content\Language\Gateway |
26
|
|
|
*/ |
27
|
|
|
protected $languageGateway; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Language Mapper. |
31
|
|
|
* |
32
|
|
|
* @var \eZ\Publish\Core\Persistence\Legacy\Content\Language\Mapper |
33
|
|
|
*/ |
34
|
|
|
protected $languageMapper; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Creates a new Language Handler. |
38
|
|
|
* |
39
|
|
|
* @param \eZ\Publish\Core\Persistence\Legacy\Content\Language\Gateway $languageGateway |
40
|
|
|
* @param \eZ\Publish\Core\Persistence\Legacy\Content\Language\Mapper $languageMapper |
41
|
|
|
*/ |
42
|
|
|
public function __construct(Gateway $languageGateway, Mapper $languageMapper) |
43
|
|
|
{ |
44
|
|
|
$this->languageGateway = $languageGateway; |
45
|
|
|
$this->languageMapper = $languageMapper; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Create a new language. |
50
|
|
|
* |
51
|
|
|
* @param \eZ\Publish\SPI\Persistence\Content\Language\CreateStruct $struct |
52
|
|
|
* |
53
|
|
|
* @return \eZ\Publish\SPI\Persistence\Content\Language |
54
|
|
|
*/ |
55
|
|
|
public function create(CreateStruct $struct) |
56
|
|
|
{ |
57
|
|
|
$language = $this->languageMapper->createLanguageFromCreateStruct( |
58
|
|
|
$struct |
59
|
|
|
); |
60
|
|
|
$language->id = $this->languageGateway->insertLanguage($language); |
61
|
|
|
|
62
|
|
|
return $language; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Update language. |
67
|
|
|
* |
68
|
|
|
* @param \eZ\Publish\SPI\Persistence\Content\Language $language |
69
|
|
|
*/ |
70
|
|
|
public function update(Language $language) |
71
|
|
|
{ |
72
|
|
|
$this->languageGateway->updateLanguage($language); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get language by id. |
77
|
|
|
* |
78
|
|
|
* @param mixed $id |
79
|
|
|
* |
80
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If language could not be found by $id |
81
|
|
|
* |
82
|
|
|
* @return \eZ\Publish\SPI\Persistence\Content\Language |
83
|
|
|
*/ |
84
|
|
View Code Duplication |
public function load($id) |
85
|
|
|
{ |
86
|
|
|
$languages = $this->languageMapper->extractLanguagesFromRows( |
87
|
|
|
$this->languageGateway->loadLanguageListData([$id]) |
88
|
|
|
); |
89
|
|
|
|
90
|
|
|
if (count($languages) < 1) { |
91
|
|
|
throw new NotFoundException('Language', $id); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return reset($languages); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* {@inheritdoc} |
99
|
|
|
*/ |
100
|
|
|
public function loadList(array $ids): iterable |
101
|
|
|
{ |
102
|
|
|
return $this->languageMapper->extractLanguagesFromRows( |
|
|
|
|
103
|
|
|
$this->languageGateway->loadLanguageListData($ids), |
104
|
|
|
'id' |
105
|
|
|
); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Get language by Language Code (eg: eng-GB). |
110
|
|
|
* |
111
|
|
|
* @param string $languageCode |
112
|
|
|
* |
113
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If language could not be found by $languageCode |
114
|
|
|
* |
115
|
|
|
* @return \eZ\Publish\SPI\Persistence\Content\Language |
116
|
|
|
*/ |
117
|
|
View Code Duplication |
public function loadByLanguageCode($languageCode) |
118
|
|
|
{ |
119
|
|
|
$languages = $this->languageMapper->extractLanguagesFromRows( |
120
|
|
|
$this->languageGateway->loadLanguageListDataByLanguageCode([$languageCode]) |
121
|
|
|
); |
122
|
|
|
|
123
|
|
|
if (count($languages) < 1) { |
124
|
|
|
throw new NotFoundException('Language', $languageCode); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return reset($languages); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* {@inheritdoc} |
132
|
|
|
*/ |
133
|
|
|
public function loadListByLanguageCodes(array $languageCodes): iterable |
134
|
|
|
{ |
135
|
|
|
return $this->languageMapper->extractLanguagesFromRows( |
|
|
|
|
136
|
|
|
$this->languageGateway->loadLanguageListDataByLanguageCode($languageCodes) |
137
|
|
|
); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Get all languages. |
142
|
|
|
* |
143
|
|
|
* @return \eZ\Publish\SPI\Persistence\Content\Language[] |
144
|
|
|
*/ |
145
|
|
|
public function loadAll() |
146
|
|
|
{ |
147
|
|
|
return $this->languageMapper->extractLanguagesFromRows( |
148
|
|
|
$this->languageGateway->loadAllLanguagesData() |
149
|
|
|
); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Delete a language. |
154
|
|
|
* |
155
|
|
|
* @param mixed $id |
156
|
|
|
* |
157
|
|
|
* @throws LogicException If language could not be deleted |
158
|
|
|
*/ |
159
|
|
|
public function delete($id) |
160
|
|
|
{ |
161
|
|
|
if (!$this->languageGateway->canDeleteLanguage($id)) { |
162
|
|
|
throw new LogicException("Deleting language logic error, some content still references that language and therefore it can't be deleted"); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
$this->languageGateway->deleteLanguage($id); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.