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 Darryldecode\Cart\Helpers\Helpers; |
11
|
|
|
use Illuminate\Support\Collection; |
12
|
|
|
|
13
|
|
|
class ItemCollection extends Collection { |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Sets the config parameters. |
17
|
|
|
* |
18
|
|
|
* @var |
19
|
|
|
*/ |
20
|
|
|
protected $config; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* ItemCollection constructor. |
24
|
|
|
* @param array|mixed $items |
25
|
|
|
* @param $config |
26
|
|
|
*/ |
27
|
|
|
public function __construct($items, $config) |
28
|
|
|
{ |
29
|
|
|
parent::__construct($items); |
30
|
|
|
|
31
|
|
|
$this->config = $config; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* get the sum of price |
36
|
|
|
* |
37
|
|
|
* @return mixed|null |
38
|
|
|
*/ |
39
|
|
|
public function getPriceSum() |
40
|
|
|
{ |
41
|
|
|
return Helpers::formatValue($this->price * $this->quantity, $this->config['format_numbers'], $this->config); |
|
|
|
|
42
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function __get($name) |
46
|
|
|
{ |
47
|
|
|
if( $this->has($name) ) return $this->get($name); |
48
|
|
|
return null; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* check if item has conditions |
53
|
|
|
* |
54
|
|
|
* @return bool |
55
|
|
|
*/ |
56
|
|
|
public function hasConditions() |
57
|
|
|
{ |
58
|
|
|
if( ! isset($this['conditions']) ) return false; |
59
|
|
|
if( is_array($this['conditions']) ) |
60
|
|
|
{ |
61
|
|
|
return count($this['conditions']) > 0; |
62
|
|
|
} |
63
|
|
|
$conditionInstance = "Darryldecode\\Cart\\CartCondition"; |
64
|
|
|
if( $this['conditions'] instanceof $conditionInstance ) return true; |
65
|
|
|
|
66
|
|
|
return false; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* check if item has conditions |
71
|
|
|
* |
72
|
|
|
* @return mixed|null |
73
|
|
|
*/ |
74
|
|
|
public function getConditions() |
75
|
|
|
{ |
76
|
|
|
if(! $this->hasConditions() ) return []; |
77
|
|
|
return $this['conditions']; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* get the single price in which conditions are already applied |
82
|
|
|
* @param bool $formatted |
83
|
|
|
* @return mixed|null |
84
|
|
|
*/ |
85
|
|
|
public function getPriceWithConditions($formatted = true) |
86
|
|
|
{ |
87
|
|
|
$originalPrice = $this->price; |
|
|
|
|
88
|
|
|
$newPrice = 0.00; |
89
|
|
|
$processed = 0; |
90
|
|
|
|
91
|
|
|
if( $this->hasConditions() ) |
92
|
|
|
{ |
93
|
|
|
if( is_array($this->conditions) ) |
|
|
|
|
94
|
|
|
{ |
95
|
|
|
foreach($this->conditions as $condition) |
|
|
|
|
96
|
|
|
{ |
97
|
|
|
if( $condition->getTarget() === 'item' ) |
98
|
|
|
{ |
99
|
|
|
( $processed > 0 ) ? $toBeCalculated = $newPrice : $toBeCalculated = $originalPrice; |
100
|
|
|
$newPrice = $condition->applyCondition($toBeCalculated); |
101
|
|
|
$processed++; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
else |
106
|
|
|
{ |
107
|
|
|
if( $this['conditions']->getTarget() === 'item' ) |
108
|
|
|
{ |
109
|
|
|
$newPrice = $this['conditions']->applyCondition($originalPrice); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return Helpers::formatValue($newPrice, $formatted, $this->config); |
114
|
|
|
} |
115
|
|
|
return Helpers::formatValue($originalPrice, $formatted, $this->config); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* get the sum of price in which conditions are already applied |
120
|
|
|
* @param bool $formatted |
121
|
|
|
* @return mixed|null |
122
|
|
|
*/ |
123
|
|
|
public function getPriceSumWithConditions($formatted = true) |
124
|
|
|
{ |
125
|
|
|
return Helpers::formatValue($this->getPriceWithConditions(false) * $this->quantity, $formatted, $this->config); |
|
|
|
|
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
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.