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