|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LukePOLO\LaraCart\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use LukePOLO\LaraCart\CartItem; |
|
6
|
|
|
use LukePOLO\LaraCart\Exceptions\InvalidPrice; |
|
7
|
|
|
use LukePOLO\LaraCart\Exceptions\InvalidQuantity; |
|
8
|
|
|
use LukePOLO\LaraCart\Exceptions\InvalidTaxableValue; |
|
9
|
|
|
use LukePOLO\LaraCart\LaraCart; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class CartOptionsMagicMethodsTrait |
|
13
|
|
|
* |
|
14
|
|
|
* @package LukePOLO\LaraCart\Traits |
|
15
|
|
|
*/ |
|
16
|
|
|
trait CartOptionsMagicMethodsTrait |
|
17
|
|
|
{ |
|
18
|
|
|
public $options = []; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Magic Method allows for user input as an object |
|
22
|
|
|
* |
|
23
|
|
|
* @param $option |
|
24
|
|
|
* |
|
25
|
|
|
* @return mixed | null |
|
26
|
|
|
*/ |
|
27
|
|
|
public function __get($option) |
|
28
|
|
|
{ |
|
29
|
|
|
try { |
|
30
|
|
|
return $this->$option; |
|
31
|
|
|
} catch (\ErrorException $e) { |
|
|
|
|
|
|
32
|
|
|
return array_get($this->options, $option); |
|
33
|
|
|
} |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Magic Method allows for user input to set a value inside the options array |
|
38
|
|
|
* |
|
39
|
|
|
* @param $option |
|
40
|
|
|
* @param $value |
|
41
|
|
|
* |
|
42
|
|
|
* @throws InvalidPrice |
|
43
|
|
|
* @throws InvalidQuantity |
|
44
|
|
|
* @throws InvalidTaxableValue |
|
45
|
|
|
*/ |
|
46
|
|
|
public function __set($option, $value) |
|
47
|
|
|
{ |
|
48
|
|
|
switch ($option) { |
|
49
|
|
|
case CartItem::ITEM_QTY: |
|
50
|
|
|
if (!is_numeric($value) || $value < 0) { |
|
51
|
|
|
throw new InvalidQuantity('The quantity must be a valid number'); |
|
52
|
|
|
} |
|
53
|
|
|
break; |
|
54
|
|
|
case CartItem::ITEM_PRICE: |
|
55
|
|
|
if (!is_numeric($value)) { |
|
56
|
|
|
throw new InvalidPrice('The price must be a valid number'); |
|
57
|
|
|
} |
|
58
|
|
|
break; |
|
59
|
|
|
case CartItem::ITEM_TAX: |
|
60
|
|
|
if (!is_numeric($value) || $value > 1) { |
|
61
|
|
|
throw new InvalidTaxableValue('The tax must be a float less than 1'); |
|
62
|
|
|
} |
|
63
|
|
|
break; |
|
64
|
|
|
case CartItem::ITEM_TAXABLE: |
|
65
|
|
|
if(!is_bool($value)) { |
|
|
|
|
|
|
66
|
|
|
throw new InvalidTaxableValue('The taxable option must be a boolean'); |
|
67
|
|
|
} |
|
68
|
|
|
break; |
|
69
|
|
|
} |
|
70
|
|
|
array_set($this->options, $option, $value); |
|
71
|
|
|
|
|
72
|
|
|
if (is_callable(array($this, 'generateHash'))) { |
|
73
|
|
|
$this->generateHash(); |
|
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Magic Method allows for user to check if an option isset |
|
79
|
|
|
* |
|
80
|
|
|
* @param $option |
|
81
|
|
|
* |
|
82
|
|
|
* @return bool |
|
83
|
|
|
*/ |
|
84
|
|
|
public function __isset($option) |
|
85
|
|
|
{ |
|
86
|
|
|
if (!empty($this->options[$option])) { |
|
87
|
|
|
return true; |
|
88
|
|
|
} else { |
|
89
|
|
|
return false; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return,dieorexitstatements that have been added for debug purposes.In the above example, the last
return falsewill never be executed, because a return statement has already been met in every possible execution path.