ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Stack.php
Go to the documentation of this file.
1 <?php
2 
4 
6 
7 class Stack
8 {
14  private $stack = [];
15 
21  private $count = 0;
22 
28  public function count()
29  {
30  return $this->count;
31  }
32 
45  public function push(
46  $type,
47  $value,
48  $reference = null,
49  $storeKey = null,
50  $onlyIf = null,
51  $onlyIfNot = null
52  ): void {
53  $stackItem = $this->getStackItem($type, $value, $reference, $storeKey, $onlyIf, $onlyIfNot);
54 
55  $this->stack[$this->count++] = $stackItem;
56 
57  if ($type == 'Function') {
58  $localeFunction = Calculation::localeFunc($value);
59  if ($localeFunction != $value) {
60  $this->stack[($this->count - 1)]['localeValue'] = $localeFunction;
61  }
62  }
63  }
64 
65  public function getStackItem(
66  $type,
67  $value,
68  $reference = null,
69  $storeKey = null,
70  $onlyIf = null,
71  $onlyIfNot = null
72  ) {
73  $stackItem = [
74  'type' => $type,
75  'value' => $value,
76  'reference' => $reference,
77  ];
78 
79  if (isset($storeKey)) {
80  $stackItem['storeKey'] = $storeKey;
81  }
82 
83  if (isset($onlyIf)) {
84  $stackItem['onlyIf'] = $onlyIf;
85  }
86 
87  if (isset($onlyIfNot)) {
88  $stackItem['onlyIfNot'] = $onlyIfNot;
89  }
90 
91  return $stackItem;
92  }
93 
99  public function pop()
100  {
101  if ($this->count > 0) {
102  return $this->stack[--$this->count];
103  }
104 
105  return null;
106  }
107 
115  public function last($n = 1)
116  {
117  if ($this->count - $n < 0) {
118  return null;
119  }
120 
121  return $this->stack[$this->count - $n];
122  }
123 
127  public function clear(): void
128  {
129  $this->stack = [];
130  $this->count = 0;
131  }
132 
133  public function __toString()
134  {
135  $str = 'Stack: ';
136  foreach ($this->stack as $index => $item) {
137  if ($index > $this->count - 1) {
138  break;
139  }
140  $value = $item['value'] ?? 'no value';
141  while (is_array($value)) {
142  $value = array_pop($value);
143  }
144  $str .= $value . ' |> ';
145  }
146 
147  return $str;
148  }
149 }
push( $type, $value, $reference=null, $storeKey=null, $onlyIf=null, $onlyIfNot=null)
Push a new entry onto the stack.
Definition: Stack.php:45
$type
count()
Return the number of entries on the stack.
Definition: Stack.php:28
pop()
Pop the last entry from the stack.
Definition: Stack.php:99
$index
Definition: metadata.php:60
getStackItem( $type, $value, $reference=null, $storeKey=null, $onlyIf=null, $onlyIfNot=null)
Definition: Stack.php:65
$n
Definition: RandomTest.php:85
last($n=1)
Return an entry from the stack without removing it.
Definition: Stack.php:115