| Total Complexity | 9 |
| Total Lines | 59 |
| Duplicated Lines | 0 % |
| Coverage | 87.5% |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 11 | trait OperationsOnItemsTrait |
||
| 12 | { |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Execute a callback over each item. |
||
| 16 | * |
||
| 17 | * @param callable $callback |
||
| 18 | * @return $this |
||
| 19 | */ |
||
| 20 | 1 | public function each(callable $callback) |
|
| 21 | { |
||
| 22 | 1 | foreach ($this as $key => $item) { |
|
| 23 | 1 | if ($callback($item, $key) === false) { |
|
| 24 | break; |
||
| 25 | 1 | } |
|
| 26 | } |
||
| 27 | |||
| 28 | 1 | return $this; |
|
| 29 | 1 | } |
|
| 30 | |||
| 31 | 1 | /** |
|
| 32 | * @param $key |
||
| 33 | * @param $value |
||
| 34 | * |
||
| 35 | 1 | * @throws Exception |
|
| 36 | 1 | */ |
|
| 37 | public function valueAdd($key, $value) |
||
| 38 | { |
||
| 39 | if (!$this->has($key)) { |
||
|
|
|||
| 40 | $this->set($key, $value); |
||
| 41 | |||
| 42 | return; |
||
| 43 | } |
||
| 44 | 1 | ||
| 45 | $currentValue = $this->get($key, 0); |
||
| 46 | 1 | $currentValue = $currentValue === null ? 0 : $currentValue; |
|
| 47 | |||
| 48 | 1 | if (!is_numeric($currentValue)) { |
|
| 49 | throw new Exception("Current value for key {$key} is not numeric. [{$value}] provided. "); |
||
| 50 | } |
||
| 51 | |||
| 52 | 1 | $this->set($key, ($currentValue + $value)); |
|
| 53 | 1 | } |
|
| 54 | |||
| 55 | /** |
||
| 56 | * @param $key |
||
| 57 | * @param $value |
||
| 58 | * |
||
| 59 | * @throws Exception |
||
| 60 | */ |
||
| 61 | public function valueSubtract($key, $value) |
||
| 70 | } |
||
| 71 | } |
||
| 72 |