1
|
|
|
<?php namespace Darryldecode\Cart; |
2
|
|
|
use Darryldecode\Cart\Exceptions\InvalidConditionException; |
3
|
|
|
use Darryldecode\Cart\Helpers\Helpers; |
4
|
|
|
use Darryldecode\Cart\Validators\CartConditionValidator; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Created by PhpStorm. |
8
|
|
|
* User: darryl |
9
|
|
|
* Date: 1/15/2015 |
10
|
|
|
* Time: 9:02 PM |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
class CartCondition { |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
private $args; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* the parsed raw value of the condition |
22
|
|
|
* |
23
|
|
|
* @var |
24
|
|
|
*/ |
25
|
|
|
public $parsedRawValue; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param array $args (name, type, target, value) |
29
|
|
|
* @throws InvalidConditionException |
30
|
|
|
*/ |
31
|
|
|
public function __construct(array $args) |
32
|
|
|
{ |
33
|
|
|
$this->args = $args; |
34
|
|
|
|
35
|
|
|
if( Helpers::isMultiArray($args) ) |
36
|
|
|
{ |
37
|
|
|
Throw new InvalidConditionException('Multi dimensional array is not supported.'); |
38
|
|
|
} |
39
|
|
|
else |
40
|
|
|
{ |
41
|
|
|
$this->validate($this->args); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* the target of where the condition is applied. |
47
|
|
|
* NOTE: On conditions added to per item bases, target is not needed. |
48
|
|
|
* |
49
|
|
|
* @return mixed |
50
|
|
|
*/ |
51
|
|
|
public function getTarget() |
52
|
|
|
{ |
53
|
|
|
return (isset($this->args['target'])) ? $this->args['target'] : ''; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* the name of the condition |
58
|
|
|
* |
59
|
|
|
* @return mixed |
60
|
|
|
*/ |
61
|
|
|
public function getName() |
62
|
|
|
{ |
63
|
|
|
return $this->args['name']; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* the type of the condition |
68
|
|
|
* |
69
|
|
|
* @return mixed |
70
|
|
|
*/ |
71
|
|
|
public function getType() |
72
|
|
|
{ |
73
|
|
|
return $this->args['type']; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* get the additional attributes of a condition |
78
|
|
|
* |
79
|
|
|
* @return array |
80
|
|
|
*/ |
81
|
|
|
public function getAttributes() |
82
|
|
|
{ |
83
|
|
|
return (isset($this->args['attributes'])) ? $this->args['attributes'] : array(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* the value of this the condition |
88
|
|
|
* |
89
|
|
|
* @return mixed |
90
|
|
|
*/ |
91
|
|
|
public function getValue() |
92
|
|
|
{ |
93
|
|
|
return $this->args['value']; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* the quantity of this the condition |
98
|
|
|
* |
99
|
|
|
* @return mixed |
100
|
|
|
*/ |
101
|
|
|
public function getQuantity() |
102
|
|
|
{ |
103
|
|
|
return $this->args['quantity']; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Set the order to apply this condition. If no argument order is applied we return 0 as |
108
|
|
|
* indicator that no assignment has been made |
109
|
|
|
* @param int $order |
110
|
|
|
* @return Integer |
111
|
|
|
*/ |
112
|
|
|
public function setOrder($order = 1) |
113
|
|
|
{ |
114
|
|
|
$this->args['order'] = $order; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* the order to apply this condition. If no argument order is applied we return 0 as |
119
|
|
|
* indicator that no assignment has been made |
120
|
|
|
* |
121
|
|
|
* @return Integer |
122
|
|
|
*/ |
123
|
|
|
public function getOrder() |
124
|
|
|
{ |
125
|
|
|
return isset($this->args['order']) && is_numeric($this->args['order']) ? (int)$this->args['order'] : 0; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* apply condition to total or subtotal |
130
|
|
|
* |
131
|
|
|
* @param $totalOrSubTotalOrPrice |
132
|
|
|
* @return float |
133
|
|
|
*/ |
134
|
|
|
public function applyCondition($totalOrSubTotalOrPrice) |
135
|
|
|
{ |
136
|
|
|
return $this->apply($totalOrSubTotalOrPrice, $this->getValue()); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* get the calculated value of this condition supplied by the subtotal|price |
141
|
|
|
* |
142
|
|
|
* @param $totalOrSubTotalOrPrice |
143
|
|
|
* @return mixed |
144
|
|
|
*/ |
145
|
|
|
public function getCalculatedValue($totalOrSubTotalOrPrice) |
146
|
|
|
{ |
147
|
|
|
$this->apply($totalOrSubTotalOrPrice, $this->getValue()); |
148
|
|
|
|
149
|
|
|
return $this->parsedRawValue; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* apply condition |
154
|
|
|
* |
155
|
|
|
* @param $totalOrSubTotalOrPrice |
156
|
|
|
* @param $conditionValue |
157
|
|
|
* @return float |
158
|
|
|
*/ |
159
|
|
|
protected function apply($totalOrSubTotalOrPrice, $conditionValue) |
160
|
|
|
{ |
161
|
|
|
// if type of condition is multiply we make count it or |
162
|
|
|
// if value has a percentage sign on it, we will get first |
163
|
|
|
// its percentage then we will evaluate again if the value |
164
|
|
|
// has a minus or plus sign so we can decide what to do with the |
165
|
|
|
// percentage, whether to add or subtract it to the total/subtotal/price |
166
|
|
|
// if we can't find any plus/minus sign, we will assume it as plus sign |
167
|
|
|
if( $this->getType() == 'multiply' ){ |
168
|
|
|
$result = $totalOrSubTotalOrPrice + ( $this->getValue() * $this->getQuantity() ); |
|
|
|
|
169
|
|
|
} |
170
|
|
|
if( $this->valueIsPercentage($conditionValue) ) |
171
|
|
|
{ |
172
|
|
|
if( $this->valueIsToBeSubtracted($conditionValue) ) |
173
|
|
|
{ |
174
|
|
|
$value = Helpers::normalizePrice( $this->cleanValue($conditionValue) ); |
175
|
|
|
|
176
|
|
|
$this->parsedRawValue = $totalOrSubTotalOrPrice * ($value / 100); |
177
|
|
|
|
178
|
|
|
$result = floatval($totalOrSubTotalOrPrice - $this->parsedRawValue); |
179
|
|
|
} |
180
|
|
|
else if ( $this->valueIsToBeAdded($conditionValue) ) |
181
|
|
|
{ |
182
|
|
|
$value = Helpers::normalizePrice( $this->cleanValue($conditionValue) ); |
183
|
|
|
|
184
|
|
|
$this->parsedRawValue = $totalOrSubTotalOrPrice * ($value / 100); |
185
|
|
|
|
186
|
|
|
$result = floatval($totalOrSubTotalOrPrice + $this->parsedRawValue); |
187
|
|
|
} |
188
|
|
|
else |
189
|
|
|
{ |
190
|
|
|
$value = Helpers::normalizePrice($conditionValue); |
191
|
|
|
|
192
|
|
|
$this->parsedRawValue = $totalOrSubTotalOrPrice * ($value / 100); |
193
|
|
|
|
194
|
|
|
$result = floatval($totalOrSubTotalOrPrice + $this->parsedRawValue); |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
// if the value has no percent sign on it, the operation will not be a percentage |
199
|
|
|
// next is we will check if it has a minus/plus sign so then we can just deduct it to total/subtotal/price |
200
|
|
|
else |
201
|
|
|
{ |
202
|
|
|
if( $this->valueIsToBeSubtracted($conditionValue) ) |
203
|
|
|
{ |
204
|
|
|
$this->parsedRawValue = Helpers::normalizePrice( $this->cleanValue($conditionValue) ); |
205
|
|
|
|
206
|
|
|
$result = floatval($totalOrSubTotalOrPrice - $this->parsedRawValue); |
207
|
|
|
} |
208
|
|
|
else if ( $this->valueIsToBeAdded($conditionValue) ) |
209
|
|
|
{ |
210
|
|
|
$this->parsedRawValue = Helpers::normalizePrice( $this->cleanValue($conditionValue) ); |
211
|
|
|
|
212
|
|
|
$result = floatval($totalOrSubTotalOrPrice + $this->parsedRawValue); |
213
|
|
|
} |
214
|
|
|
else |
215
|
|
|
{ |
216
|
|
|
$this->parsedRawValue = Helpers::normalizePrice($conditionValue); |
217
|
|
|
|
218
|
|
|
$result = floatval($totalOrSubTotalOrPrice + $this->parsedRawValue); |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
// Do not allow items with negative prices. |
223
|
|
|
return $result < 0 ? 0.00 : $result; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* check if value is a percentage |
228
|
|
|
* |
229
|
|
|
* @param $value |
230
|
|
|
* @return bool |
231
|
|
|
*/ |
232
|
|
|
protected function valueIsPercentage($value) |
233
|
|
|
{ |
234
|
|
|
return (preg_match('/%/', $value) == 1); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* check if value is a subtract |
239
|
|
|
* |
240
|
|
|
* @param $value |
241
|
|
|
* @return bool |
242
|
|
|
*/ |
243
|
|
|
protected function valueIsToBeSubtracted($value) |
244
|
|
|
{ |
245
|
|
|
return (preg_match('/\-/', $value) == 1); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* check if value is to be added |
250
|
|
|
* |
251
|
|
|
* @param $value |
252
|
|
|
* @return bool |
253
|
|
|
*/ |
254
|
|
|
protected function valueIsToBeAdded($value) |
255
|
|
|
{ |
256
|
|
|
return (preg_match('/\+/', $value) == 1); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* removes some arithmetic signs (%,+,-) only |
261
|
|
|
* |
262
|
|
|
* @param $value |
263
|
|
|
* @return mixed |
264
|
|
|
*/ |
265
|
|
|
protected function cleanValue($value) |
266
|
|
|
{ |
267
|
|
|
return str_replace(array('%','-','+'),'',$value); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* validates condition arguments |
272
|
|
|
* |
273
|
|
|
* @param $args |
274
|
|
|
* @throws InvalidConditionException |
275
|
|
|
*/ |
276
|
|
View Code Duplication |
protected function validate($args) |
277
|
|
|
{ |
278
|
|
|
$rules = array( |
279
|
|
|
'name' => 'required', |
280
|
|
|
'type' => 'required', |
281
|
|
|
'value' => 'required', |
282
|
|
|
); |
283
|
|
|
|
284
|
|
|
$validator = CartConditionValidator::make($args, $rules); |
285
|
|
|
|
286
|
|
|
if( $validator->fails() ) |
287
|
|
|
{ |
288
|
|
|
throw new InvalidConditionException($validator->messages()->first()); |
289
|
|
|
} |
290
|
|
|
} |
291
|
|
|
} |
292
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.