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