|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* File containing the ContentIdCriterion parser 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
|
|
|
* @version //autogentag// |
|
10
|
|
|
*/ |
|
11
|
|
|
namespace eZ\Publish\Core\REST\Server\Input\Parser\Criterion; |
|
12
|
|
|
|
|
13
|
|
|
use eZ\Publish\Core\REST\Common\Input\BaseParser; |
|
14
|
|
|
use eZ\Publish\Core\REST\Common\Input\ParsingDispatcher; |
|
15
|
|
|
use eZ\Publish\Core\REST\Common\Exceptions; |
|
16
|
|
|
use eZ\Publish\API\Repository\Values\Content\Query\Criterion\UserMetadata as UserMetadataCriterion; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Parser for ViewInput. |
|
20
|
|
|
*/ |
|
21
|
|
|
class UserMetadata extends BaseParser |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* Parses input structure to a Criterion object. |
|
25
|
|
|
* |
|
26
|
|
|
* @param array $data |
|
27
|
|
|
* @param \eZ\Publish\Core\REST\Common\Input\ParsingDispatcher $parsingDispatcher |
|
28
|
|
|
* |
|
29
|
|
|
* @throws \eZ\Publish\Core\REST\Common\Exceptions\Parser |
|
30
|
|
|
* |
|
31
|
|
|
* @return \eZ\Publish\API\Repository\Values\Content\Query\Criterion\UserMetadata |
|
32
|
|
|
*/ |
|
33
|
|
|
public function parse(array $data, ParsingDispatcher $parsingDispatcher) |
|
34
|
|
|
{ |
|
35
|
|
|
if (!isset($data['UserMetadataCriterion'])) { |
|
36
|
|
|
throw new Exceptions\Parser('Invalid <UserMetadataCriterion> format'); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
if (!isset($data['UserMetadataCriterion']['Target'])) { |
|
40
|
|
|
throw new Exceptions\Parser('Invalid <Target> format'); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
$target = $data['UserMetadataCriterion']['Target']; |
|
44
|
|
|
|
|
45
|
|
|
if (!isset($data['UserMetadataCriterion']['Value'])) { |
|
46
|
|
|
throw new Exceptions\Parser('Invalid <Value> format'); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
if (!in_array(gettype($data['UserMetadataCriterion']['Value']), ['integer', 'string', 'array'])) { |
|
50
|
|
|
throw new Exceptions\Parser('Invalid <Value> format'); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$value = is_array($data['UserMetadataCriterion']['Value']) |
|
54
|
|
|
? $data['UserMetadataCriterion']['Value'] |
|
55
|
|
|
: explode(',', $data['UserMetadataCriterion']['Value']); |
|
56
|
|
|
|
|
57
|
|
|
return new UserMetadataCriterion($target, null, $value); |
|
|
|
|
|
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
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_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.