|
1
|
|
|
<?php |
|
2
|
|
|
namespace Germania\Coupons; |
|
3
|
|
|
|
|
4
|
|
|
class CouponSheet extends CouponSheetAbstract implements CouponSheetInterface |
|
5
|
|
|
{ |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* @param string $id |
|
9
|
|
|
* @return self Fluid Interface |
|
10
|
|
|
*/ |
|
11
|
5 |
|
public function setId( $id ) { |
|
12
|
5 |
|
$this->id = $id; |
|
13
|
5 |
|
return $this; |
|
14
|
|
|
} |
|
15
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @param int $quantity |
|
19
|
|
|
* @return self Fluid Interface |
|
20
|
|
|
*/ |
|
21
|
|
|
public function setQuantity( $quantity ) { |
|
22
|
|
|
$this->quantity = $quantity; |
|
23
|
|
|
return $this; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param string $id |
|
29
|
|
|
* @return self Fluid Interface |
|
30
|
|
|
*/ |
|
31
|
5 |
|
public function setSlug( $slug ) { |
|
32
|
5 |
|
$this->slug = $slug; |
|
33
|
5 |
|
return $this; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param string $id |
|
39
|
|
|
* @return self Fluid Interface |
|
40
|
|
|
*/ |
|
41
|
5 |
|
public function setName( $name ) { |
|
42
|
5 |
|
$this->name = $name; |
|
43
|
5 |
|
return $this; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param DateTimeInterface $valid_from |
|
52
|
|
|
* @return self Fluid Interface |
|
53
|
|
|
*/ |
|
54
|
10 |
|
public function setValidFrom( \DateTimeInterface $valid_from ) { |
|
55
|
10 |
|
$this->valid_from = $valid_from; |
|
56
|
10 |
|
return $this; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @inheritdoc |
|
62
|
|
|
* |
|
63
|
|
|
* If the valid_from value is a datetime string (like "Y-m-d H:i:s"), |
|
64
|
|
|
* the value will be converted to a DateTime object. |
|
65
|
|
|
* |
|
66
|
|
|
* @return DateTime |
|
67
|
|
|
* @implements CouponSheetInterface |
|
68
|
|
|
*/ |
|
69
|
10 |
|
public function getValidFrom() { |
|
70
|
10 |
|
if ($this->valid_from and is_string($this->valid_from)): |
|
71
|
5 |
|
$this->setValidFrom( \DateTime::createFromFormat("Y-m-d H:i:s", $this->valid_from) ); |
|
72
|
1 |
|
endif; |
|
73
|
10 |
|
return $this->valid_from; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param DateTimeInterface $valid_until |
|
80
|
|
|
* @return self Fluid Interface |
|
81
|
|
|
*/ |
|
82
|
10 |
|
public function setValidUntil( \DateTimeInterface $valid_until ) { |
|
83
|
10 |
|
$this->valid_until = $valid_until; |
|
84
|
10 |
|
return $this; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @inheritdoc |
|
89
|
|
|
* |
|
90
|
|
|
* If the valid_until value is a datetime string (like "Y-m-d H:i:s"), |
|
91
|
|
|
* the value will be converted to a DateTime object. |
|
92
|
|
|
* |
|
93
|
|
|
* @return DateTime |
|
94
|
|
|
* @implements CouponSheetInterface |
|
95
|
|
|
*/ |
|
96
|
10 |
|
public function getValidUntil() { |
|
97
|
10 |
|
if ($this->valid_until and is_string($this->valid_until)): |
|
98
|
5 |
|
$this->setValidUntil( \DateTime::createFromFormat("Y-m-d H:i:s", $this->valid_until) ); |
|
99
|
1 |
|
endif; |
|
100
|
|
|
|
|
101
|
10 |
|
return $this->valid_until; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
|
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @return CouponInterface[] |
|
108
|
|
|
* @implements CouponSheetInterface |
|
109
|
|
|
*/ |
|
110
|
10 |
|
public function getCoupons() { |
|
111
|
10 |
|
if ($this->coupons and is_string( $this->coupons )) |
|
112
|
6 |
|
$this->setCoupons( $this->coupons ); |
|
113
|
|
|
|
|
114
|
10 |
|
return $this->coupons; |
|
|
|
|
|
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @param array|\Traversable $coupons |
|
120
|
|
|
* @return self Fluid Interface |
|
121
|
|
|
*/ |
|
122
|
10 |
|
public function setCoupons( $coupons ) |
|
123
|
|
|
{ |
|
124
|
10 |
|
if ($coupons instanceOf \Traversable ): |
|
125
|
5 |
|
$coupons = iterator_to_array( $coupons, "use_keys"); |
|
126
|
|
|
|
|
127
|
10 |
|
elseif (is_string( $coupons )): |
|
128
|
10 |
|
$instances = array(); |
|
129
|
10 |
|
foreach(explode(",", $coupons) as $raw_coupon) |
|
130
|
10 |
|
array_push($instances, Coupon::fromArray([ |
|
131
|
10 |
|
'code' => $raw_coupon, |
|
132
|
8 |
|
'coupon_sheet' => $this |
|
133
|
2 |
|
])); |
|
134
|
10 |
|
$coupons = $instances; |
|
135
|
|
|
|
|
136
|
6 |
|
elseif (!is_array( $coupons )): |
|
137
|
5 |
|
throw new \InvalidArgumentException("Array or Traversable expected"); |
|
138
|
|
|
|
|
139
|
|
|
endif; |
|
140
|
|
|
|
|
141
|
10 |
|
$this->coupons = $coupons; |
|
142
|
10 |
|
return $this; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
|
|
146
|
|
|
} |
|
147
|
|
|
|
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.