1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Webino (http://webino.sk) |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/webino/WebinoDraw for the canonical source repository |
6
|
|
|
* @copyright Copyright (c) 2012-2017 Webino, s. r. o. (http://webino.sk) |
7
|
|
|
* @author Peter Bačinský <[email protected]> |
8
|
|
|
* @license BSD-3-Clause |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace WebinoDraw\Instructions; |
12
|
|
|
|
13
|
|
|
use ArrayObject; |
14
|
|
|
use WebinoDraw\Exception\InvalidInstructionException; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Draw instructions utilities |
18
|
|
|
*/ |
19
|
|
|
class Instructions extends ArrayObject implements |
20
|
|
|
InstructionsInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Stack space before instruction without index |
24
|
|
|
*/ |
25
|
|
|
const STACK_SPACER = 10; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param array $array |
29
|
|
|
*/ |
30
|
|
|
public function __construct(array $array = []) |
31
|
|
|
{ |
32
|
|
|
empty($array) or |
33
|
|
|
$this->merge($array); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param array $array |
38
|
|
|
*/ |
39
|
|
|
public function exchangeArray($array) |
40
|
|
|
{ |
41
|
|
|
parent::exchangeArray([]); |
42
|
|
|
$this->merge($array); |
43
|
|
|
return $this; |
|
|
|
|
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritDoc} |
48
|
|
|
*/ |
49
|
|
|
public function getSortedArrayCopy() |
50
|
|
|
{ |
51
|
|
|
$instructions = $this->getArrayCopy(); |
52
|
|
|
ksort($instructions); |
53
|
|
|
return $instructions; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Merge draw instructions |
58
|
|
|
* |
59
|
|
|
* If node with name exists merge else add, |
60
|
|
|
* or if same stackIndex throws exception. |
61
|
|
|
* |
62
|
|
|
* Instructions structure: |
63
|
|
|
* <pre> |
64
|
|
|
* [ |
65
|
|
|
* 'node_name' => [ |
66
|
|
|
* 'stackIndex' => '50', |
67
|
|
|
* 'customkey' => 'customvalue', |
68
|
|
|
* .... |
69
|
|
|
* ], |
70
|
|
|
* ]; |
71
|
|
|
* </pre> |
72
|
|
|
* |
73
|
|
|
* If no stackIndex is defined add as last with |
74
|
|
|
* space before. |
75
|
|
|
* |
76
|
|
|
* @param array $instructions Merge from |
77
|
|
|
* @return self |
78
|
|
|
* @throws InvalidInstructionException |
79
|
|
|
*/ |
80
|
|
|
public function merge(array $instructions) |
81
|
|
|
{ |
82
|
|
|
$mergeWith = $this->getArrayCopy(); |
83
|
|
|
$mergeFrom = $instructions; |
84
|
|
|
$instructionsN = count($mergeWith) * self::STACK_SPACER; |
85
|
|
|
|
86
|
|
|
foreach ($mergeWith as &$spec) { |
87
|
|
|
foreach ($mergeFrom as $iKey => $iSpec) { |
88
|
|
|
if (key($spec) != $iKey) { |
89
|
|
|
continue; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
// merge existing spec |
93
|
|
|
unset($mergeFrom[$iKey]); |
94
|
|
|
$spec = array_replace_recursive($spec, [$iKey => $iSpec]); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
unset($spec); |
99
|
|
|
|
100
|
|
|
foreach ($mergeFrom as $index => $spec) { |
101
|
|
|
if (null === $spec) { |
102
|
|
|
continue; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
if (!is_array($spec)) { |
106
|
|
|
throw new InvalidInstructionException('Instruction node spec expect array'); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
if (!isset($spec['stackIndex'])) { |
110
|
|
|
// add without stack index |
111
|
|
|
$stackIndex = $instructionsN + self::STACK_SPACER; |
112
|
|
|
|
113
|
|
|
if (!isset($mergeWith[$stackIndex])) { |
114
|
|
|
$instructionsN = $stackIndex; |
115
|
|
|
$mergeWith[$stackIndex][$index] = $spec; |
116
|
|
|
continue; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
unset($stackIndex); |
120
|
|
|
|
121
|
|
|
} elseif (!isset($mergeWith[$spec['stackIndex']])) { |
122
|
|
|
// add with stackindex |
123
|
|
|
$mergeWith[$spec['stackIndex']][$index] = $spec; |
124
|
|
|
continue; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
throw new InvalidInstructionException(sprintf('Stack index already exists `%s`', print_r($spec, true))); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
parent::exchangeArray($mergeWith); |
|
|
|
|
131
|
|
|
return $this; |
|
|
|
|
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
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.