| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\Receipt; |
| 4 |
|
| 5 |
use ArrayAccess; |
| 6 |
use Give\Framework\Exceptions\Primitives\InvalidArgumentException; |
| 7 |
use Iterator; |
| 8 |
|
| 9 |
/** |
| 10 |
* Class Section |
| 11 |
* |
| 12 |
* This class represent receipt section as object and you can add ass many as you want line items. |
| 13 |
* |
| 14 |
* @package Give\Receipt |
| 15 |
* @since 2.7.0 |
| 16 |
*/ |
| 17 |
class Section implements Iterator, ArrayAccess |
| 18 |
{ |
| 19 |
/** |
| 20 |
* Iterator initial position. |
| 21 |
* |
| 22 |
* @var int |
| 23 |
*/ |
| 24 |
private $position = 0; |
| 25 |
|
| 26 |
/** |
| 27 |
* Array of line item ids to use in Iterator. |
| 28 |
* Note: this property helps to iterate over associative array. |
| 29 |
* |
| 30 |
* @var int |
| 31 |
*/ |
| 32 |
private $lineItemIds = []; |
| 33 |
|
| 34 |
/** |
| 35 |
* Section heading. |
| 36 |
* |
| 37 |
* @since 2.7.0 |
| 38 |
* @var string |
| 39 |
*/ |
| 40 |
public $label = ''; |
| 41 |
|
| 42 |
/** |
| 43 |
* Section ID. |
| 44 |
* |
| 45 |
* @since 2.7.0 |
| 46 |
* @var string |
| 47 |
*/ |
| 48 |
public $id = ''; |
| 49 |
|
| 50 |
/** |
| 51 |
* Array of detail item class names. |
| 52 |
* |
| 53 |
* @since 2.7.0 |
| 54 |
* @var LineItem[] |
| 55 |
*/ |
| 56 |
private $lineItems = []; |
| 57 |
|
| 58 |
/** |
| 59 |
* Section constructor. |
| 60 |
* |
| 61 |
* @param string $id |
| 62 |
* @param string $label |
| 63 |
*/ |
| 64 |
public function __construct($id, $label) |
| 65 |
{ |
| 66 |
$this->id = $id; |
| 67 |
$this->label = $label; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Get line items. |
| 72 |
* |
| 73 |
* @since 2.7.0 |
| 74 |
* @return LineItem[] |
| 75 |
*/ |
| 76 |
public function getLineItems() |
| 77 |
{ |
| 78 |
return $this->lineItems; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Add detail group. |
| 83 |
* |
| 84 |
* @since 2.7.0 |
| 85 |
* |
| 86 |
* @param string $position Position can be set either "before" or "after" to insert line item at specific position. |
| 87 |
* @param string $lineItemId |
| 88 |
* |
| 89 |
* @param array $lineItem |
| 90 |
* |
| 91 |
* @return LineItem |
| 92 |
*/ |
| 93 |
public function addLineItem($lineItem, $position = '', $lineItemId = '') |
| 94 |
{ |
| 95 |
$this->validateLineItem($lineItem); |
| 96 |
|
| 97 |
$icon = isset($lineItem['icon']) ? $lineItem['icon'] : ''; |
| 98 |
|
| 99 |
$lineItemObj = new LineItem($lineItem['id'], $lineItem['label'], $lineItem['value'], $icon); |
| 100 |
|
| 101 |
if (isset($this->lineItems[$lineItemId]) && in_array($position, ['before', 'after'])) { |
| 102 |
// Insert line item at specific position. |
| 103 |
$tmp = []; |
| 104 |
$tmpIds = []; |
| 105 |
|
| 106 |
foreach ($this->lineItems as $id => $data) { |
| 107 |
if ('after' === $position) { |
| 108 |
$tmp[$id] = $data; |
| 109 |
$tmpIds[] = $id; |
| 110 |
} |
| 111 |
|
| 112 |
if ($id === $lineItemId) { |
| 113 |
$tmp[$lineItemObj->id] = $lineItemObj; |
| 114 |
$tmpIds[] = $lineItemObj->id; |
| 115 |
} |
| 116 |
|
| 117 |
if ('before' === $position) { |
| 118 |
$tmp[$id] = $data; |
| 119 |
$tmpIds[] = $id; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
$this->lineItems = $tmp; |
| 124 |
$this->lineItemIds = $tmpIds; |
| 125 |
} else { |
| 126 |
$this->lineItems[$lineItemObj->id] = $lineItemObj; |
| 127 |
$this->lineItemIds[] = $lineItemObj->id; |
| 128 |
} |
| 129 |
|
| 130 |
return $lineItemObj; |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Remove line item. |
| 135 |
* |
| 136 |
* @since 2.7.0 |
| 137 |
* |
| 138 |
* @param string $lineItemId |
| 139 |
* |
| 140 |
*/ |
| 141 |
public function removeLineItem($lineItemId) |
| 142 |
{ |
| 143 |
$this->offsetUnset($lineItemId); |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Validate line item. |
| 148 |
* |
| 149 |
* @since 2.7.0 |
| 150 |
* |
| 151 |
* @param array $array |
| 152 |
* |
| 153 |
*/ |
| 154 |
protected function validateLineItem($array) |
| 155 |
{ |
| 156 |
$required = ['id', 'label', 'value']; |
| 157 |
$array = array_filter($array); // Remove empty values. |
| 158 |
|
| 159 |
if (array_diff($required, array_keys($array))) { |
| 160 |
throw new InvalidArgumentException( |
| 161 |
esc_html__( |
| 162 |
'Invalid receipt section line item. Please provide valid line item id, label, and value.', |
| 163 |
'give' |
| 164 |
) |
| 165 |
); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Return current data. |
| 171 |
* |
| 172 |
* @since 2.7.0 |
| 173 |
* @return mixed |
| 174 |
*/ |
| 175 |
public function current() |
| 176 |
{ |
| 177 |
return $this->lineItems[$this->lineItemIds[$this->position]]; |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Update iterator position. |
| 182 |
* |
| 183 |
* @since 2.7.0 |
| 184 |
*/ |
| 185 |
public function next() |
| 186 |
{ |
| 187 |
++$this->position; |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* Return iterator position. |
| 192 |
* |
| 193 |
* @since 2.7.0 |
| 194 |
* @return int |
| 195 |
*/ |
| 196 |
public function key() |
| 197 |
{ |
| 198 |
return $this->position; |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* Return whether or not valid array position. |
| 203 |
* |
| 204 |
* @since 2.7.0 |
| 205 |
* @return bool|void |
| 206 |
*/ |
| 207 |
public function valid() |
| 208 |
{ |
| 209 |
return isset($this->lineItemIds[$this->position]); |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Set iterator position to zero when rewind. |
| 214 |
* |
| 215 |
* @since 2.7.0 |
| 216 |
*/ |
| 217 |
public function rewind() |
| 218 |
{ |
| 219 |
$this->position = 0; |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
* Set line item. |
| 224 |
* |
| 225 |
* @since 2.7.0 |
| 226 |
* |
| 227 |
* @param array $value LineItem Data. |
| 228 |
* |
| 229 |
* @param string $offset LineItem ID. |
| 230 |
*/ |
| 231 |
public function offsetSet($offset, $value) |
| 232 |
{ |
| 233 |
$this->addLineItem($value); |
| 234 |
} |
| 235 |
|
| 236 |
/** |
| 237 |
* Return whether or not line item id exist in line. |
| 238 |
* |
| 239 |
* @since 2.7.0 |
| 240 |
* |
| 241 |
* @param string $offset LineItem ID. |
| 242 |
* |
| 243 |
* @return bool |
| 244 |
*/ |
| 245 |
public function offsetExists($offset) |
| 246 |
{ |
| 247 |
return isset($this->lineItems[$offset]); |
| 248 |
} |
| 249 |
|
| 250 |
/** |
| 251 |
* Remove line item from line. |
| 252 |
* |
| 253 |
* @since 2.7.0 |
| 254 |
* |
| 255 |
* @param string $offset LineItem ID. |
| 256 |
* |
| 257 |
*/ |
| 258 |
public function offsetUnset($offset) |
| 259 |
{ |
| 260 |
if ($this->offsetExists($offset)) { |
| 261 |
unset($this->lineItems[$offset]); |
| 262 |
$this->lineItemIds = array_keys($this->lineItems); |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
/** |
| 267 |
* Get line item. |
| 268 |
* |
| 269 |
* @since 2.7.0 |
| 270 |
* |
| 271 |
* @param string $offset LineItem ID. |
| 272 |
* |
| 273 |
* @return LineItem|null |
| 274 |
*/ |
| 275 |
public function offsetGet($offset) |
| 276 |
{ |
| 277 |
return isset($this->lineItems[$offset]) ? $this->lineItems[$offset] : null; |
| 278 |
} |
| 279 |
} |
| 280 |
|