seboettg /
Collection
| 1 | <?php |
||
| 2 | declare(strict_types=1); |
||
| 3 | /* |
||
| 4 | * Copyright (C) 2018 Sebastian Böttger <[email protected]> |
||
| 5 | * You may use, distribute and modify this code under the |
||
| 6 | * terms of the MIT license. |
||
| 7 | * |
||
| 8 | * You should have received a copy of the MIT license with |
||
| 9 | * this file. If not, please visit: https://opensource.org/licenses/mit-license.php |
||
| 10 | */ |
||
| 11 | |||
| 12 | namespace Seboettg\Collection\Stack; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Trait StackTrait |
||
| 16 | * @package Seboettg\Collection |
||
| 17 | * @author Sebastian Böttger <[email protected]> |
||
| 18 | * @property $array Base array of this data structure |
||
| 19 | */ |
||
|
0 ignored issues
–
show
Documentation
Bug
introduced
by
Loading history...
|
|||
| 20 | trait StackTrait |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * {@inheritdoc} |
||
| 24 | */ |
||
| 25 | 5 | public function push($item): StackInterface |
|
| 26 | { |
||
| 27 | 5 | $this->array[] = $item; |
|
|
0 ignored issues
–
show
|
|||
| 28 | 5 | return $this; |
|
|
0 ignored issues
–
show
|
|||
| 29 | } |
||
| 30 | |||
| 31 | /** |
||
| 32 | * {@inheritdoc} |
||
| 33 | */ |
||
| 34 | 2 | public function pop() |
|
| 35 | { |
||
| 36 | 2 | return array_pop($this->array); |
|
| 37 | } |
||
| 38 | |||
| 39 | /** |
||
| 40 | * {@inheritdoc} |
||
| 41 | */ |
||
| 42 | 1 | public function peek() |
|
| 43 | { |
||
| 44 | 1 | return end($this->array); |
|
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * {@inheritdoc} |
||
| 49 | */ |
||
| 50 | 1 | public function search($element) |
|
| 51 | { |
||
| 52 | 1 | $pos = array_search($element, $this->array); |
|
| 53 | 1 | if ($pos === false) { |
|
| 54 | 1 | return 0; |
|
| 55 | } |
||
| 56 | 1 | $count = $this->count(); |
|
| 57 | 1 | return $count - $pos; |
|
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * {@inheritdoc} |
||
| 62 | */ |
||
| 63 | 5 | public function count(): int |
|
| 64 | { |
||
| 65 | 5 | return count($this->array); |
|
| 66 | } |
||
| 67 | } |
||
| 68 |