1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
4
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
5
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
6
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
7
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
8
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace MpaCustomDoctrineHydrator\Form\Element; |
12
|
|
|
|
13
|
|
|
use Zend\Filter\StringTrim; |
14
|
|
|
|
15
|
|
|
class Time extends DateTime |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Accepted options for DateTime: |
19
|
|
|
* - format: A \DateTime compatible string |
20
|
|
|
* |
21
|
|
|
* @param array|\Traversable $options |
22
|
|
|
* @return \DateTime |
23
|
|
|
*/ |
24
|
1 |
|
public function setOptions($options) |
25
|
|
|
{ |
26
|
1 |
|
parent::setOptions($options); |
27
|
|
|
|
28
|
1 |
|
if (isset($this->options['time_format'])) { |
29
|
1 |
|
$this->setFormat($this->options['time_format']); |
30
|
|
|
} |
31
|
|
|
|
32
|
1 |
|
return $this; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Provide default input rules for this element |
37
|
|
|
* Attaches default validators for the Date input. |
38
|
|
|
* |
39
|
|
|
* @return array |
40
|
|
|
*/ |
41
|
5 |
|
public function getInputSpecification() |
42
|
|
|
{ |
43
|
|
|
return [ |
|
|
|
|
44
|
5 |
|
'name' => $this->getName(), |
45
|
|
|
'required' => true, |
46
|
|
|
'filters' => [ |
47
|
|
|
['name' => StringTrim::class], |
48
|
|
|
[ |
49
|
5 |
|
'name' => 'MpaCustomDoctrineHydrator\Filter\TimeToDateTime', |
50
|
|
|
'options' => [ |
51
|
5 |
|
'time_format' => $this->getFormat(), |
52
|
|
|
] |
53
|
|
|
], |
54
|
|
|
], |
55
|
|
|
'validators' => [ |
56
|
5 |
|
$this->getDateValidator() |
57
|
|
|
], |
58
|
|
|
]; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
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.