| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\Receipt; |
| 4 |
|
| 5 |
use ArrayAccess; |
| 6 |
use Iterator; |
| 7 |
|
| 8 |
/** |
| 9 |
* Class Receipt |
| 10 |
* |
| 11 |
* This class represent receipt as object. |
| 12 |
* Receipt can have multiple sections and sections can have multiple line items. |
| 13 |
* |
| 14 |
* @package Give\Receipt |
| 15 |
* @since 2.7.0 |
| 16 |
*/ |
| 17 |
abstract class Receipt implements Iterator, ArrayAccess |
| 18 |
{ |
| 19 |
/** |
| 20 |
* Iterator initial position. |
| 21 |
* |
| 22 |
* @var int |
| 23 |
*/ |
| 24 |
protected $position = 0; |
| 25 |
|
| 26 |
/** |
| 27 |
* Receipt Heading. |
| 28 |
* |
| 29 |
* @since 2.7.0 |
| 30 |
* @var string $heading |
| 31 |
*/ |
| 32 |
public $heading = ''; |
| 33 |
|
| 34 |
/** |
| 35 |
* Receipt message. |
| 36 |
* |
| 37 |
* @since 2.7.0 |
| 38 |
* @var string $message |
| 39 |
*/ |
| 40 |
public $message = ''; |
| 41 |
|
| 42 |
/** |
| 43 |
* Receipt details group class names. |
| 44 |
* |
| 45 |
* @since 2.7.0 |
| 46 |
* @var array |
| 47 |
*/ |
| 48 |
protected $sectionList = []; |
| 49 |
|
| 50 |
/** |
| 51 |
* Array of section ids to use for Iterator. |
| 52 |
* Note: this property helps to iterate over associative array. |
| 53 |
* |
| 54 |
* @var int |
| 55 |
*/ |
| 56 |
protected $sectionIds = []; |
| 57 |
|
| 58 |
/** |
| 59 |
* Get receipt sections. |
| 60 |
* |
| 61 |
* @since 2.7.0 |
| 62 |
* @return array |
| 63 |
*/ |
| 64 |
public function getSections() |
| 65 |
{ |
| 66 |
return $this->sectionList; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Add receipt section. |
| 71 |
* |
| 72 |
* @since 2.7.0 |
| 73 |
* |
| 74 |
* @param string $position Position can be set either "before" or "after" to insert section at specific position. |
| 75 |
* @param string $sectionId |
| 76 |
* |
| 77 |
* @param array $section |
| 78 |
* |
| 79 |
* @return Section |
| 80 |
* |
| 81 |
*/ |
| 82 |
public function addSection($section, $position = '', $sectionId = '') |
| 83 |
{ |
| 84 |
$this->validateSection($section); |
| 85 |
|
| 86 |
// Add default label. |
| 87 |
$label = isset($section['label']) ? $section['label'] : ''; |
| 88 |
|
| 89 |
$sectionObj = new Section($section['id'], $label); |
| 90 |
|
| 91 |
if (isset($this->sectionList[$sectionId]) && in_array($position, ['before', 'after'])) { |
| 92 |
// Insert line item at specific position. |
| 93 |
$tmp = []; |
| 94 |
$tmpIds = []; |
| 95 |
|
| 96 |
foreach ($this->sectionList as $id => $data) { |
| 97 |
if ('after' === $position) { |
| 98 |
$tmp[$id] = $data; |
| 99 |
$tmpIds[] = $id; |
| 100 |
} |
| 101 |
|
| 102 |
if ($id === $sectionId) { |
| 103 |
$tmp[$sectionObj->id] = $sectionObj; |
| 104 |
$tmpIds[] = $sectionObj->id; |
| 105 |
} |
| 106 |
|
| 107 |
if ('before' === $position) { |
| 108 |
$tmp[$id] = $data; |
| 109 |
$tmpIds[] = $id; |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
$this->sectionList = $tmp; |
| 114 |
$this->sectionIds = $tmpIds; |
| 115 |
} else { |
| 116 |
$this->sectionList[$sectionObj->id] = $sectionObj; |
| 117 |
$this->sectionIds[] = $sectionObj->id; |
| 118 |
} |
| 119 |
|
| 120 |
return $sectionObj; |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Validate section. |
| 125 |
* |
| 126 |
* @param array $array |
| 127 |
* |
| 128 |
* @since 2.23.1 Method added to abstract base class as abstract method. |
| 129 |
*/ |
| 130 |
abstract protected function validateSection($array); |
| 131 |
|
| 132 |
/** |
| 133 |
* Remove receipt section. |
| 134 |
* |
| 135 |
* @since 2.7.0 |
| 136 |
* |
| 137 |
* @param string $sectionId |
| 138 |
* |
| 139 |
*/ |
| 140 |
public function removeSection($sectionId) |
| 141 |
{ |
| 142 |
$this->offsetUnset($sectionId); |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Set section. |
| 147 |
* |
| 148 |
* @since 2.7.0 |
| 149 |
* |
| 150 |
* @param array $value Section Data. |
| 151 |
* @param string $offset Section ID. |
| 152 |
*/ |
| 153 |
public function offsetSet($offset, $value) |
| 154 |
{ |
| 155 |
$this->addSection($value); |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Return whether or not session id exist in list. |
| 160 |
* |
| 161 |
* @since 2.7.0 |
| 162 |
* |
| 163 |
* @param string $offset Section ID. |
| 164 |
* |
| 165 |
* @return bool |
| 166 |
*/ |
| 167 |
public function offsetExists($offset) |
| 168 |
{ |
| 169 |
return isset($this->sectionList[$offset]); |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Remove section from list. |
| 174 |
* |
| 175 |
* @since 2.7.0 |
| 176 |
* |
| 177 |
* @param string $offset Section ID. |
| 178 |
*/ |
| 179 |
public function offsetUnset($offset) |
| 180 |
{ |
| 181 |
if ($this->offsetExists($offset)) { |
| 182 |
unset($this->sectionList[$offset]); |
| 183 |
$this->sectionIds = array_keys($this->sectionList); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Get section. |
| 189 |
* |
| 190 |
* @since 2.7.0 |
| 191 |
* |
| 192 |
* @param string $offset Session ID. |
| 193 |
* |
| 194 |
* @return Section|null |
| 195 |
*/ |
| 196 |
public function offsetGet($offset) |
| 197 |
{ |
| 198 |
return isset($this->sectionList[$offset]) ? $this->sectionList[$offset] : null; |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* Return current data when iterate or data. |
| 203 |
* |
| 204 |
* @since 2.7.0 |
| 205 |
* @return mixed |
| 206 |
*/ |
| 207 |
public function current() |
| 208 |
{ |
| 209 |
return $this->sectionList[$this->sectionIds[$this->position]]; |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Update iterator position. |
| 214 |
* |
| 215 |
* @since 2.7.0 |
| 216 |
*/ |
| 217 |
public function next() |
| 218 |
{ |
| 219 |
++$this->position; |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
* Return iterator position. |
| 224 |
* |
| 225 |
* @since 2.7.0 |
| 226 |
* @return int |
| 227 |
*/ |
| 228 |
public function key() |
| 229 |
{ |
| 230 |
return $this->position; |
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* Return whether or not valid array position. |
| 235 |
* |
| 236 |
* @since 2.7.0 |
| 237 |
* @return bool|void |
| 238 |
*/ |
| 239 |
public function valid() |
| 240 |
{ |
| 241 |
return isset($this->sectionIds[$this->position]); |
| 242 |
} |
| 243 |
} |
| 244 |
|