|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Selami\Entity\DataType; |
|
5
|
|
|
|
|
6
|
|
|
use Selami\Entity\Interfaces\DataTypeInterface; |
|
7
|
|
|
use DateTime; |
|
8
|
|
|
use Assert\Assertion; |
|
9
|
|
|
use BadMethodCallException; |
|
10
|
|
|
use InvalidArgumentException; |
|
11
|
|
|
|
|
12
|
|
|
class Date extends DataTypeAbstract implements DataTypeInterface |
|
13
|
|
|
{ |
|
14
|
|
|
const DATA_TYPE_ERROR = 'Error for value "%s" for "%s" : INVALID_TYPE. Must be a date string.'; |
|
15
|
|
|
const DATA_FORMAT_ERROR = 'Error for value "%s" for "%s": INVALID_DATE_FORMAT'; |
|
16
|
|
|
const DATA_MIN_ERROR = 'Error for value "%s" for "%s": MIN_VALUE='; |
|
17
|
|
|
const DATA_MAX_ERROR = 'Error for value "%s" for "%s": MAX_VALUE='; |
|
18
|
|
|
|
|
19
|
|
|
protected static $defaults = [ |
|
20
|
|
|
'format' => 'Y-m-d H:i:s', |
|
21
|
|
|
'default' => 'now', |
|
22
|
|
|
'min' => null, |
|
23
|
|
|
'max' => null |
|
24
|
|
|
]; |
|
25
|
|
|
|
|
26
|
|
|
protected static $validDateOptions = [ |
|
27
|
|
|
'now', |
|
28
|
|
|
'tomorrow', |
|
29
|
|
|
'next week', |
|
30
|
|
|
'next month', |
|
31
|
|
|
'next year', |
|
32
|
|
|
'yesterday', |
|
33
|
|
|
'previous week', |
|
34
|
|
|
'previous month', |
|
35
|
|
|
'previous year', |
|
36
|
|
|
]; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Date constructor. |
|
40
|
|
|
* @param string $key |
|
41
|
|
|
* @param string $datum |
|
42
|
|
|
* @param array $options |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __construct(string $key, string $datum, array $options = []) |
|
45
|
|
|
{ |
|
46
|
|
|
$this->key = $key; |
|
47
|
|
|
$this->datum = $datum; |
|
48
|
|
|
$this->options = array_merge(self::$defaults, $options); |
|
49
|
|
|
$this->errorMessageTemplate = self::DATA_TYPE_ERROR; |
|
50
|
|
|
} |
|
51
|
|
|
/** |
|
52
|
|
|
* {@inheritdoc} |
|
53
|
|
|
*/ |
|
54
|
|
|
public function assert() |
|
55
|
|
|
{ |
|
56
|
|
|
$this->isString(); |
|
57
|
|
|
$this->checkFormat(); |
|
58
|
|
|
$this->checkMin(); |
|
59
|
|
|
$this->checkMax(); |
|
60
|
|
|
return true; |
|
|
|
|
|
|
61
|
|
|
} |
|
62
|
|
|
private function isString() |
|
63
|
|
|
{ |
|
64
|
|
|
if (!is_string($this->datum)) { |
|
65
|
|
|
$this->throwException(); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
private function checkFormat() |
|
70
|
|
|
{ |
|
71
|
|
|
$this->errorMessageTemplate = self::DATA_FORMAT_ERROR; |
|
72
|
|
|
|
|
73
|
|
|
if (in_array($this->datum, self::$validDateOptions, true)) { |
|
74
|
|
|
return true; |
|
75
|
|
|
} |
|
76
|
|
|
try { |
|
77
|
|
|
Assertion::date($this->datum, $this->options['format']); |
|
78
|
|
|
} catch (BadMethodCallException $e) { |
|
79
|
|
|
$this->throwException(); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
View Code Duplication |
private function checkMin() |
|
|
|
|
|
|
84
|
|
|
{ |
|
85
|
|
|
$this->errorMessageTemplate = self::DATA_MIN_ERROR . $this->options['min']; |
|
86
|
|
|
if ($this->options['min'] === null) { |
|
87
|
|
|
return true; |
|
88
|
|
|
} |
|
89
|
|
|
if ($this->datum < $this->options['min']) { |
|
90
|
|
|
$this->throwException(); |
|
91
|
|
|
} |
|
92
|
|
|
return true; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
View Code Duplication |
private function checkMax() |
|
|
|
|
|
|
96
|
|
|
{ |
|
97
|
|
|
$this->errorMessageTemplate = self::DATA_MIN_ERROR . $this->options['max']; |
|
98
|
|
|
if ($this->options['max'] === null) { |
|
99
|
|
|
return true; |
|
100
|
|
|
} |
|
101
|
|
|
if ($this->datum > $this->options['min']) { |
|
102
|
|
|
$this->throwException(); |
|
103
|
|
|
} |
|
104
|
|
|
return true; |
|
105
|
|
|
} |
|
106
|
|
|
/** |
|
107
|
|
|
* {@inheritdoc} |
|
108
|
|
|
*/ |
|
109
|
|
|
public function normalize() |
|
110
|
|
|
{ |
|
111
|
|
|
try { |
|
112
|
|
|
$this->assert(); |
|
113
|
|
|
$date = new DateTime($this->datum); |
|
114
|
|
|
} catch (InvalidArgumentException $e) { |
|
115
|
|
|
$date = new DateTime($this->options['default']); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
return $date->format($this->options['format']); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
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.