1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* 2016 Romain CANON <[email protected]> |
4
|
|
|
* |
5
|
|
|
* This file is part of the TYPO3 Formz project. |
6
|
|
|
* It is free software; you can redistribute it and/or modify it |
7
|
|
|
* under the terms of the GNU General Public License, either |
8
|
|
|
* version 3 of the License, or any later version. |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, see: |
11
|
|
|
* http://www.gnu.org/licenses/gpl-3.0.html |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Romm\Formz\Configuration\Form\Condition\Activation; |
15
|
|
|
|
16
|
|
|
use Romm\ConfigurationObject\Service\Items\Parents\ParentsTrait; |
17
|
|
|
use Romm\Formz\Configuration\AbstractFormzConfiguration; |
18
|
|
|
use Romm\Formz\Configuration\Form\Condition\ConditionItemResolver; |
19
|
|
|
use Romm\Formz\Configuration\Form\Form; |
20
|
|
|
use TYPO3\CMS\Extbase\Utility\ArrayUtility; |
21
|
|
|
|
22
|
|
|
abstract class AbstractActivation extends AbstractFormzConfiguration implements ActivationInterface |
23
|
|
|
{ |
24
|
|
|
|
25
|
|
|
use ParentsTrait; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $condition; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var \ArrayObject<Romm\Formz\Configuration\Form\Condition\ConditionItemResolver> |
34
|
|
|
* @validate NotEmpty |
35
|
|
|
*/ |
36
|
|
|
protected $items = []; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @inheritdoc |
40
|
|
|
*/ |
41
|
|
|
public function getCondition() |
42
|
|
|
{ |
43
|
|
|
return $this->condition; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Will merge the items with the ones from the `$activationCondition` |
48
|
|
|
* property of the root form configuration. |
49
|
|
|
* |
50
|
|
|
* @return ConditionItemResolver[] |
51
|
|
|
*/ |
52
|
|
|
public function getItems() |
53
|
|
|
{ |
54
|
|
|
$activationCondition = $this->withFirstParent( |
55
|
|
|
Form::class, |
56
|
|
|
function (Form $formConfiguration) { |
57
|
|
|
return $formConfiguration->getActivationCondition(); |
58
|
|
|
} |
59
|
|
|
); |
60
|
|
|
$activationCondition = ($activationCondition) ?: []; |
61
|
|
|
|
62
|
|
|
return ArrayUtility::arrayMergeRecursiveOverrule($activationCondition, $this->items); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @inheritdoc |
67
|
|
|
*/ |
68
|
|
|
public function hasItem($itemName) |
69
|
|
|
{ |
70
|
|
|
$items = $this->getItems(); |
71
|
|
|
|
72
|
|
|
return (true === isset($items[$itemName])); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @inheritdoc |
77
|
|
|
*/ |
78
|
|
|
public function getItem($itemName) |
79
|
|
|
{ |
80
|
|
|
if (true === $this->hasItem($itemName)) { |
81
|
|
|
$items = $this->getItems(); |
82
|
|
|
|
83
|
|
|
return $items[$itemName]; |
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return null; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
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.