1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* WellCommerce Open-Source E-Commerce Platform |
4
|
|
|
* |
5
|
|
|
* This file is part of the WellCommerce package. |
6
|
|
|
* |
7
|
|
|
* (c) Adam Piotrowski <[email protected]> |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, |
10
|
|
|
* please view the LICENSE file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace WellCommerce\Component\Form\Elements; |
14
|
|
|
|
15
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
16
|
|
|
use Symfony\Component\PropertyAccess\PropertyPath; |
17
|
|
|
use WellCommerce\Component\Form\DataTransformer\DataTransformerInterface; |
18
|
|
|
use WellCommerce\Component\Form\Filters\FilterInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class AbstractField |
22
|
|
|
* |
23
|
|
|
* @author Adam Piotrowski <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
abstract class AbstractField extends AbstractContainer |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var mixed |
29
|
|
|
*/ |
30
|
|
|
protected $value; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* {@inheritdoc} |
34
|
|
|
*/ |
35
|
|
|
public function configureOptions(OptionsResolver $resolver) |
36
|
|
|
{ |
37
|
|
|
parent::configureOptions($resolver); |
38
|
|
|
|
39
|
|
|
$resolver->setDefaults([ |
40
|
|
|
'comment' => '', |
41
|
|
|
'error' => [], |
42
|
|
|
'default' => null, |
43
|
|
|
'dependencies' => [], |
44
|
|
|
'rules' => [], |
45
|
|
|
'filters' => [], |
46
|
|
|
'transformer' => null, |
47
|
|
|
]); |
48
|
|
|
|
49
|
|
|
$resolver->setNormalizer('property_path', function ($options) { |
50
|
|
|
return new PropertyPath($options['name']); |
51
|
|
|
}); |
52
|
|
|
|
53
|
|
|
$resolver->setAllowedTypes('comment', 'string'); |
54
|
|
|
$resolver->setAllowedTypes('error', 'array'); |
55
|
|
|
$resolver->setAllowedTypes('dependencies', 'array'); |
56
|
|
|
$resolver->setAllowedTypes('rules', 'array'); |
57
|
|
|
$resolver->setAllowedTypes('filters', 'array'); |
58
|
|
|
$resolver->setAllowedTypes('transformer', ['null', DataTransformerInterface::class]); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
protected function getFilters() |
62
|
|
|
{ |
63
|
|
|
return $this->options['filters']; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
|
|
public function addChild(ElementInterface $element) |
70
|
|
|
{ |
71
|
|
|
throw new \BadMethodCallException( |
72
|
|
|
sprintf('Cannot add child "%s" as it is allowed only for element of type fieldset', get_class($element)) |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritdoc} |
78
|
|
|
*/ |
79
|
|
|
public function addFilter(FilterInterface $filter) |
80
|
|
|
{ |
81
|
|
|
$this->options['filters'][] = $filter; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function setValue($value) |
85
|
|
|
{ |
86
|
|
|
$this->value = $value; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function getValue() |
90
|
|
|
{ |
91
|
|
|
return $this->value; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function getError() |
95
|
|
|
{ |
96
|
|
|
if (is_array($this->error)) { |
97
|
|
|
return implode('.', array_values($this->error)); |
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return ''; |
|
|
|
|
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* {@inheritdoc} |
105
|
|
|
*/ |
106
|
|
|
public function prepareAttributesCollection(AttributeCollection $collection) |
107
|
|
|
{ |
108
|
|
|
parent::prepareAttributesCollection($collection); |
109
|
|
|
$collection->add(new Attribute('sValue', $this->value)); |
110
|
|
|
$collection->add(new Attribute('sComment', $this->getOption('comment'))); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
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.