ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
ArrayCollection.php
Go to the documentation of this file.
1 <?php
2 
3 namespace SAML2\Utilities;
4 
6 
12 class ArrayCollection implements Collection
13 {
17  protected $elements;
18 
19  public function __construct(array $elements = array())
20  {
21  $this->elements = $elements;
22  }
23 
24  public function add($element)
25  {
26  $this->elements[] = $element;
27  }
28 
29  public function get($key)
30  {
31  return isset($this->elements[$key]) ? $this->elements[$key] : null;
32  }
33 
34  public function filter(\Closure $f)
35  {
36  return new self(array_filter($this->elements, $f));
37  }
38 
39  public function set($key, $value)
40  {
41  $this->elements[$key] = $value;
42  }
43 
44  public function remove($element)
45  {
46  $key = array_search($element, $this->elements);
47 
48  if ($key === false) {
49  return false;
50  }
51 
52  $removed = $this->elements[$key];
53  unset($this->elements[$key]);
54 
55  return $removed;
56  }
57 
58  public function getOnlyElement()
59  {
60  if ($this->count() !== 1) {
61  throw new RuntimeException(sprintf(
62  __CLASS__ . '::' . __METHOD__ . ' requires that the collection has exactly one element, '
63  . '"%d" elements found',
64  $this->count()
65  ));
66  }
67 
68  return reset($this->elements);
69  }
70 
71  public function first()
72  {
73  return reset($this->elements);
74  }
75 
76  public function last()
77  {
78  return end($this->elements);
79  }
80 
81  public function map(\Closure $function)
82  {
83  return new self(array_map($function, $this->elements));
84  }
85 
86  public function count()
87  {
88  return count($this->elements);
89  }
90 
91  public function getIterator()
92  {
93  return new \ArrayIterator($this->elements);
94  }
95 
96  public function offsetExists($offset)
97  {
98  return isset($this->elements[$offset]);
99  }
100 
101  public function offsetGet($offset)
102  {
103  return $this->elements[$offset];
104  }
105 
106  public function offsetSet($offset, $value)
107  {
108  $this->elements[$offset] = $value;
109  }
110 
111  public function offsetUnset($offset)
112  {
113  unset($this->elements[$offset]);
114  }
115 }
add($element)
Add an element to the collection.
first()
Return the first element from the collection.
getOnlyElement()
Shorthand for getting a single element that also must be the only element in the collection.
__construct(array $elements=array())
last()
Return the last element from the collection.
Simple Array implementation of Collection.
Create styles array
The data for the language used.
$function
Definition: cas.php:28
map(\Closure $function)
Applies the given function to each element in the collection and returns a new collection with the el...
$key
Definition: croninfo.php:18