|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace CultuurNet\UDB3\EventExport\Command; |
|
4
|
|
|
|
|
5
|
|
|
use CultuurNet\Deserializer\JSONDeserializer; |
|
6
|
|
|
use CultuurNet\Deserializer\MissingValueException; |
|
7
|
|
|
use CultuurNet\UDB3\EventExport\EventExportQuery; |
|
8
|
|
|
use ValueObjects\String\String; |
|
9
|
|
|
use ValueObjects\Web\EmailAddress; |
|
10
|
|
|
|
|
11
|
|
|
abstract class ExportEventsJSONDeserializer extends JSONDeserializer |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @param \ValueObjects\String\String $data |
|
15
|
|
|
* @return ExportEvents |
|
16
|
|
|
*/ |
|
17
|
|
|
public function deserialize(String $data) |
|
18
|
|
|
{ |
|
19
|
|
|
$data = parent::deserialize($data); |
|
20
|
|
|
|
|
21
|
|
|
if (!isset($data->query)) { |
|
22
|
|
|
throw new MissingValueException('query is missing'); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
$query = new EventExportQuery($data->query); |
|
26
|
|
|
$email = $selection = $include = null; |
|
27
|
|
|
|
|
28
|
|
|
// @todo This throws an exception when the e-mail is invalid. How do we handle this? |
|
29
|
|
|
if (isset($data->email)) { |
|
30
|
|
|
$email = new EmailAddress($data->email); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
if (isset($data->selection)) { |
|
34
|
|
|
$selection = $data->selection; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
if (isset($data->include)) { |
|
38
|
|
|
$include = $data->include; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
return $this->createCommand($query, $email, $selection, $include); |
|
|
|
|
|
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param EventExportQuery $query |
|
46
|
|
|
* @param EmailAddress|null $address |
|
47
|
|
|
* @param string[]|null $selection |
|
48
|
|
|
* @param string[]|null $include |
|
49
|
|
|
* @return ExportEvents |
|
50
|
|
|
*/ |
|
51
|
|
|
abstract protected function createCommand( |
|
52
|
|
|
EventExportQuery $query, |
|
53
|
|
|
EmailAddress $address = null, |
|
54
|
|
|
$selection = null, |
|
55
|
|
|
$include = null |
|
56
|
|
|
); |
|
57
|
|
|
} |
|
58
|
|
|
|
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.