1
|
|
|
<?php namespace Darryldecode\Cart; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Created by PhpStorm. |
5
|
|
|
* User: darryl |
6
|
|
|
* Date: 1/17/2015 |
7
|
|
|
* Time: 11:03 AM |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
use Illuminate\Support\Collection; |
11
|
|
|
|
12
|
|
|
class ItemCollection extends Collection { |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Sets the number of decimal points. |
16
|
|
|
* |
17
|
|
|
* @var |
18
|
|
|
*/ |
19
|
|
|
protected $decimals; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Definces the decimal point delimiter type |
23
|
|
|
* |
24
|
|
|
* @var |
25
|
|
|
*/ |
26
|
|
|
protected $dec_point; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Defines the thousands point delimiter type |
30
|
|
|
* |
31
|
|
|
* @var |
32
|
|
|
*/ |
33
|
|
|
protected $thousands_sep; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* ItemCollection constructor. |
37
|
|
|
* @param array|mixed $items |
38
|
|
|
* @param $config |
39
|
|
|
*/ |
40
|
|
|
public function __construct($items, $config) |
41
|
|
|
{ |
42
|
|
|
parent::__construct($items); |
43
|
|
|
|
44
|
|
|
$this->decimals = $config['decimals']; |
45
|
|
|
$this->dec_point = $config['dec_point']; |
46
|
|
|
$this->thousands_sep = $config['thousands_sep']; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* get the sum of price |
51
|
|
|
* |
52
|
|
|
* @return mixed|null |
53
|
|
|
*/ |
54
|
|
|
public function getPriceSum() |
55
|
|
|
{ |
56
|
|
|
return $this->price * $this->quantity; |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function __get($name) |
60
|
|
|
{ |
61
|
|
|
if( $this->has($name) ) return $this->get($name); |
62
|
|
|
return null; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* check if item has conditions |
67
|
|
|
* |
68
|
|
|
* @return bool |
69
|
|
|
*/ |
70
|
|
|
public function hasConditions() |
71
|
|
|
{ |
72
|
|
|
if( ! isset($this['conditions']) ) return false; |
73
|
|
|
if( is_array($this['conditions']) ) |
74
|
|
|
{ |
75
|
|
|
return count($this['conditions']) > 0; |
76
|
|
|
} |
77
|
|
|
$conditionInstance = "Darryldecode\\Cart\\CartCondition"; |
78
|
|
|
if( $this['conditions'] instanceof $conditionInstance ) return true; |
79
|
|
|
|
80
|
|
|
return false; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* get the single price in which conditions are already applied |
85
|
|
|
* |
86
|
|
|
* @return mixed|null |
87
|
|
|
*/ |
88
|
|
|
public function getPriceWithConditions() |
89
|
|
|
{ |
90
|
|
|
$originalPrice = $this->price; |
|
|
|
|
91
|
|
|
$newPrice = 0.00; |
92
|
|
|
$processed = 0; |
93
|
|
|
|
94
|
|
|
if( $this->hasConditions() ) |
95
|
|
|
{ |
96
|
|
|
if( is_array($this->conditions) ) |
|
|
|
|
97
|
|
|
{ |
98
|
|
|
foreach($this->conditions as $condition) |
|
|
|
|
99
|
|
|
{ |
100
|
|
|
if( $condition->getTarget() === 'item' ) |
101
|
|
|
{ |
102
|
|
|
( $processed > 0 ) ? $toBeCalculated = $newPrice : $toBeCalculated = $originalPrice; |
103
|
|
|
$newPrice = $condition->applyCondition($toBeCalculated); |
104
|
|
|
$processed++; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
else |
109
|
|
|
{ |
110
|
|
|
if( $this['conditions']->getTarget() === 'item' ) |
111
|
|
|
{ |
112
|
|
|
$newPrice = $this['conditions']->applyCondition($originalPrice); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return number_format($newPrice, $this->decimals, $this->dec_point, $this->thousands_sep); |
117
|
|
|
} |
118
|
|
|
return number_format($originalPrice, $this->decimals, $this->dec_point, $this->thousands_sep); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* get the sum of price in which conditions are already applied |
123
|
|
|
* |
124
|
|
|
* @return mixed|null |
125
|
|
|
*/ |
126
|
|
|
public function getPriceSumWithConditions() |
127
|
|
|
{ |
128
|
|
|
return number_format($this->getPriceWithConditions() * $this->quantity, $this->decimals, $this->dec_point, $this->thousands_sep); |
|
|
|
|
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.