|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* apparat-object |
|
5
|
|
|
* |
|
6
|
|
|
* @category Apparat |
|
7
|
|
|
* @package Apparat\Object |
|
8
|
|
|
* @subpackage Apparat\Object\Application |
|
9
|
|
|
* @author Joschi Kuphal <[email protected]> / @jkphl |
|
10
|
|
|
* @copyright Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl |
|
11
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
/*********************************************************************************** |
|
15
|
|
|
* The MIT License (MIT) |
|
16
|
|
|
* |
|
17
|
|
|
* Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl |
|
18
|
|
|
* |
|
19
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of |
|
20
|
|
|
* this software and associated documentation files (the "Software"), to deal in |
|
21
|
|
|
* the Software without restriction, including without limitation the rights to |
|
22
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
|
23
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so, |
|
24
|
|
|
* subject to the following conditions: |
|
25
|
|
|
* |
|
26
|
|
|
* The above copyright notice and this permission notice shall be included in all |
|
27
|
|
|
* copies or substantial portions of the Software. |
|
28
|
|
|
* |
|
29
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
30
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
|
31
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
|
32
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
|
33
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
|
34
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
|
35
|
|
|
***********************************************************************************/ |
|
36
|
|
|
|
|
37
|
|
|
namespace Apparat\Object\Application\Model\Object; |
|
38
|
|
|
|
|
39
|
|
|
use Apparat\Object\Application\Contract\CommonMarkPayloadProcessorInterface; |
|
40
|
|
|
use Apparat\Object\Domain\Model\Object\AbstractObject; |
|
41
|
|
|
use Apparat\Object\Domain\Model\Object\ObjectInterface; |
|
42
|
|
|
use Apparat\Object\Domain\Model\Path\RepositoryPathInterface; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Abstract CommonMark object |
|
46
|
|
|
* |
|
47
|
|
|
* @package Apparat\Object |
|
48
|
|
|
* @subpackage Apparat\Object\Application |
|
49
|
|
|
*/ |
|
50
|
|
|
abstract class AbstractCommonMarkObject extends AbstractObject |
|
51
|
|
|
{ |
|
52
|
|
|
/** |
|
53
|
|
|
* Payload processor |
|
54
|
|
|
* |
|
55
|
|
|
* @var CommonMarkPayloadProcessorInterface |
|
56
|
|
|
*/ |
|
57
|
|
|
protected $payloadProcessor; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Object constructor |
|
61
|
|
|
* |
|
62
|
|
|
* @param CommonMarkPayloadProcessorInterface $payloadProcessor Payload processor |
|
63
|
|
|
* @param RepositoryPathInterface $path Object repository path |
|
64
|
|
|
* @param string $payload Object payload |
|
65
|
|
|
* @param array $propertyData Property data |
|
66
|
|
|
*/ |
|
67
|
1 |
|
public function __construct( |
|
68
|
|
|
CommonMarkPayloadProcessorInterface $payloadProcessor, |
|
69
|
|
|
RepositoryPathInterface $path, |
|
70
|
|
|
$payload, |
|
71
|
|
|
array $propertyData |
|
72
|
|
|
) { |
|
73
|
1 |
|
$this->payloadProcessor = $payloadProcessor; |
|
74
|
1 |
|
$this->payloadProcessor->setObject($this); |
|
75
|
|
|
|
|
76
|
1 |
|
parent::__construct($path, $payload, $propertyData); |
|
77
|
1 |
|
} |
|
78
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Set the payload |
|
82
|
|
|
* |
|
83
|
|
|
* @param string $payload Payload |
|
84
|
|
|
* @return ObjectInterface Self reference |
|
85
|
|
|
*/ |
|
86
|
1 |
|
public function setPayload($payload) |
|
87
|
|
|
{ |
|
88
|
1 |
|
parent::setPayload($payload); |
|
89
|
|
|
|
|
90
|
|
|
// Process the payload |
|
91
|
1 |
|
return $this->payloadProcessor->processPayload(); |
|
|
|
|
|
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
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.