ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
ilBuddySystemArrayCollection Class Reference

Class ilBuddySystemArrayCollection A generic array based collection class. More...

+ Inheritance diagram for ilBuddySystemArrayCollection:
+ Collaboration diagram for ilBuddySystemArrayCollection:

Public Member Functions

 __construct (private array $elements=[])
 
 getIterator ()
 
 offsetExists (mixed $offset)
 
 offsetGet (mixed $offset)
 
 offsetSet (mixed $offset, mixed $value)
 
 offsetUnset (mixed $offset)
 
 count ()
 
 add (mixed $element)
 Adds an element at the end of the collection. More...
 
 remove (string|int $key)
 
 removeElement (mixed $element)
 
 containsKey (string|int $key)
 isset is used for performance reasons (array_key_exists is much slower). More...
 
 getKey ($element)
 
 clear ()
 Clears the list. More...
 
 contains (mixed $element)
 @phpstan-param T $element More...
 
 get (string|int $key)
 
 set (string|int $key, mixed $value)
 
 isEmpty ()
 
 getKeys ()
 Gets all indices of the collection. More...
 
 getValues ()
 Gets all values of the collection. More...
 
 filter (callable $callable)
 Returns all the elements of this collection that satisfy the predicate $callable. More...
 
 slice (int $offset, ?int $length=null)
 Extracts a slice of $length elements starting at position $offset from the Collection. More...
 
 toArray ()
 
 equals (mixed $other)
 
- Public Member Functions inherited from ilBuddySystemCollection
 add (mixed $element)
 Adds an element at the end of the collection. More...
 
 remove (string|int $key)
 
 removeElement (mixed $element)
 
 containsKey (string|int $key)
 
 getKey (mixed $element)
 
 clear ()
 Clears the list. More...
 
 contains (mixed $element)
 @phpstan-param T $element More...
 
 get (string|int $key)
 
 set (string|int $key, mixed $value)
 
 isEmpty ()
 
 getKeys ()
 Gets all indices of the collection. More...
 
 getValues ()
 Gets all values of the collection. More...
 
 filter (callable $callable)
 Returns all the elements of this collection that satisfy the predicate $callable. More...
 
 slice (int $offset, ?int $length=null)
 Extracts a slice of $length elements starting at position $offset from the Collection. More...
 
 toArray ()
 
 equals (mixed $other)
 

Detailed Description

Class ilBuddySystemArrayCollection A generic array based collection class.

Author
Michael Jansen mjans.nosp@m.en@d.nosp@m.ataba.nosp@m.y.de @template T @template TKey @template-implements ilBuddySystemCollection<TKey,T>

Definition at line 29 of file class.ilBuddySystemArrayCollection.php.

Constructor & Destructor Documentation

◆ __construct()

ilBuddySystemArrayCollection::__construct ( private array  $elements = [])
Parameters
array<string|int,mixed>$elements @phpstan-param array<TKey, T> $elements

Definition at line 35 of file class.ilBuddySystemArrayCollection.php.

36 {
37 }

Member Function Documentation

◆ add()

ilBuddySystemArrayCollection::add ( mixed  $element)

Adds an element at the end of the collection.

@phpstan-param T $element

Implements ilBuddySystemCollection.

Definition at line 75 of file class.ilBuddySystemArrayCollection.php.

75 : void
76 {
77 $this->elements[] = $element;
78 }

Referenced by offsetSet().

+ Here is the caller graph for this function:

◆ clear()

ilBuddySystemArrayCollection::clear ( )

Clears the list.

Implements ilBuddySystemCollection.

Definition at line 113 of file class.ilBuddySystemArrayCollection.php.

113 : void
114 {
115 $this->elements = [];
116 }

◆ contains()

ilBuddySystemArrayCollection::contains ( mixed  $element)

@phpstan-param T $element

Returns
bool true if the collection contains the element, false otherwise.

Implements ilBuddySystemCollection.

Definition at line 118 of file class.ilBuddySystemArrayCollection.php.

118 : bool
119 {
120 return in_array($element, $this->elements, true);
121 }

◆ containsKey()

ilBuddySystemArrayCollection::containsKey ( string|int  $key)

isset is used for performance reasons (array_key_exists is much slower).

array_key_exists is only used in case of a null value (see https://www.php.net/manual/en/function.array-key-exists.php Example #2 array_key_exists() vs isset()).

@inheritDoc

Implements ilBuddySystemCollection.

Definition at line 103 of file class.ilBuddySystemArrayCollection.php.

103 : bool
104 {
105 return isset($this->elements[$key]) || array_key_exists($key, $this->elements);
106 }

Referenced by offsetExists(), and remove().

+ Here is the caller graph for this function:

◆ count()

ilBuddySystemArrayCollection::count ( )

Definition at line 70 of file class.ilBuddySystemArrayCollection.php.

70 : int
71 {
72 return count($this->elements);
73 }

References count().

Referenced by count().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ equals()

ilBuddySystemArrayCollection::equals ( mixed  $other)

Implements ilBuddySystemCollection.

Definition at line 163 of file class.ilBuddySystemArrayCollection.php.

163 : bool
164 {
165 if (!($other instanceof self)) {
166 return false;
167 }
168
169 $self = $this->toArray();
170 $other = $other->toArray();
171
172 sort($self);
173 sort($other);
174
175 return $self == $other;
176 }

References ILIAS\UI\examples\Symbol\Glyph\Sort\sort(), and toArray().

+ Here is the call graph for this function:

◆ filter()

ilBuddySystemArrayCollection::filter ( callable  $callable)

Returns all the elements of this collection that satisfy the predicate $callable.

Implements ilBuddySystemCollection.

Definition at line 148 of file class.ilBuddySystemArrayCollection.php.

148 : self
149 {
150 return new static(array_filter($this->elements, $callable));
151 }

◆ get()

ilBuddySystemArrayCollection::get ( string|int  $key)
Parameters
string | int$keyThe index of the element to get. @phpstan-param TKey $key @phpstan-return T|null

Implements ilBuddySystemCollection.

Definition at line 123 of file class.ilBuddySystemArrayCollection.php.

123 : mixed
124 {
125 return $this->elements[$key] ?? null;
126 }

◆ getIterator()

ilBuddySystemArrayCollection::getIterator ( )

Definition at line 39 of file class.ilBuddySystemArrayCollection.php.

39 : ArrayIterator
40 {
41 return new ArrayIterator($this->elements);
42 }

◆ getKey()

ilBuddySystemArrayCollection::getKey (   $element)

Definition at line 108 of file class.ilBuddySystemArrayCollection.php.

108 : string|int
109 {
110 return array_search($element, $this->elements, true);
111 }

Referenced by ILIAS\Contact\BuddySystem\Tables\RelationsTable\filter().

+ Here is the caller graph for this function:

◆ getKeys()

ilBuddySystemArrayCollection::getKeys ( )

Gets all indices of the collection.

Returns
string[]|int[] The indices of the collection, in the order of the corresponding elements in the collection. @phpstan-return list<TKey>

Implements ilBuddySystemCollection.

Definition at line 138 of file class.ilBuddySystemArrayCollection.php.

138 : array
139 {
140 return array_keys($this->elements);
141 }

◆ getValues()

ilBuddySystemArrayCollection::getValues ( )

Gets all values of the collection.

Returns
list<mixed> The values of all elements in the collection, in the order they appear in the collection. @phpstan-return list<T>

Implements ilBuddySystemCollection.

Definition at line 143 of file class.ilBuddySystemArrayCollection.php.

143 : array
144 {
145 return array_values($this->elements);
146 }

◆ isEmpty()

ilBuddySystemArrayCollection::isEmpty ( )
Returns
bool true if the collection is empty, false otherwise.

Implements ilBuddySystemCollection.

Definition at line 133 of file class.ilBuddySystemArrayCollection.php.

133 : bool
134 {
135 return empty($this->elements);
136 }

◆ offsetExists()

ilBuddySystemArrayCollection::offsetExists ( mixed  $offset)

Definition at line 44 of file class.ilBuddySystemArrayCollection.php.

44 : bool
45 {
46 return $this->containsKey($offset);
47 }
containsKey(string|int $key)
isset is used for performance reasons (array_key_exists is much slower).

References containsKey().

+ Here is the call graph for this function:

◆ offsetGet()

ilBuddySystemArrayCollection::offsetGet ( mixed  $offset)

Definition at line 49 of file class.ilBuddySystemArrayCollection.php.

49 : mixed
50 {
51 return $this->get($offset);
52 }

◆ offsetSet()

ilBuddySystemArrayCollection::offsetSet ( mixed  $offset,
mixed  $value 
)

Definition at line 54 of file class.ilBuddySystemArrayCollection.php.

54 : void
55 {
56 if (!isset($offset)) {
57 $this->add($value);
58
59 return;
60 }
61
62 $this->set($offset, $value);
63 }
add(mixed $element)
Adds an element at the end of the collection.

References add().

+ Here is the call graph for this function:

◆ offsetUnset()

ilBuddySystemArrayCollection::offsetUnset ( mixed  $offset)

Definition at line 65 of file class.ilBuddySystemArrayCollection.php.

65 : void
66 {
67 $this->remove($offset);
68 }

◆ remove()

ilBuddySystemArrayCollection::remove ( string|int  $key)
Parameters
string | int$keyThe index of the element to remove. @phpstan-param TKey $key
Exceptions
InvalidArgumentException

Implements ilBuddySystemCollection.

Definition at line 80 of file class.ilBuddySystemArrayCollection.php.

80 : void
81 {
82 if (!$this->containsKey($key)) {
83 throw new InvalidArgumentException(sprintf('Could not find an element for key: %s', $key));
84 }
85 unset($this->elements[$key]);
86 }

References containsKey().

+ Here is the call graph for this function:

◆ removeElement()

ilBuddySystemArrayCollection::removeElement ( mixed  $element)
Parameters
mixed$elementThe element to remove. @phpstan-param T $element
Exceptions
InvalidArgumentException

Implements ilBuddySystemCollection.

Definition at line 88 of file class.ilBuddySystemArrayCollection.php.

88 : void
89 {
90 $key = array_search($element, $this->elements, true);
91 if (false === $key) {
92 throw new InvalidArgumentException('Could not find an key for the passed element.');
93 }
94 unset($this->elements[$key]);
95 }

◆ set()

ilBuddySystemArrayCollection::set ( string|int  $key,
mixed  $value 
)
Parameters
string | int$keyThe index of the element to set. @phpstan-param TKey $key @phpstan-param T $value

Implements ilBuddySystemCollection.

Definition at line 128 of file class.ilBuddySystemArrayCollection.php.

128 : void
129 {
130 $this->elements[$key] = $value;
131 }

◆ slice()

ilBuddySystemArrayCollection::slice ( int  $offset,
?int  $length = null 
)

Extracts a slice of $length elements starting at position $offset from the Collection.

If $length is null it returns all elements from $offset to the end of the Collection. Calling this method will only return the selected slice and NOT change the elements contained in the collection slice is called on.

Parameters
int$offsetThe offset to start from.
int | null$lengthThe maximum number of elements to return, or null for no limit.

Implements ilBuddySystemCollection.

Definition at line 153 of file class.ilBuddySystemArrayCollection.php.

153 : self
154 {
155 return new static(array_slice($this->elements, $offset, $length, true));
156 }

◆ toArray()

ilBuddySystemArrayCollection::toArray ( )
Returns
array<int|string, mixed> @psalm-return array<TKey, T>

Implements ilBuddySystemCollection.

Definition at line 158 of file class.ilBuddySystemArrayCollection.php.

158 : array
159 {
160 return $this->elements;
161 }

Referenced by equals().

+ Here is the caller graph for this function:

The documentation for this class was generated from the following file: