Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
/**
4
 * This file is part of the ramsey/collection library
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 *
9
 * @copyright Copyright (c) Ben Ramsey <ben@benramsey.com>
10
 * @license http://opensource.org/licenses/MIT MIT
11
 */
12
 
13
declare(strict_types=1);
14
 
15
namespace Ramsey\Collection;
16
 
17
use ArrayIterator;
18
use Traversable;
19
 
20
use function count;
21
 
22
/**
23
 * This class provides a basic implementation of `ArrayInterface`, to minimize
24
 * the effort required to implement this interface.
25
 *
26
 * @template T
27
 * @implements ArrayInterface<T>
28
 */
29
abstract class AbstractArray implements ArrayInterface
30
{
31
    /**
32
     * The items of this array.
33
     *
34
     * @var array<array-key, T>
35
     */
36
    protected array $data = [];
37
 
38
    /**
39
     * Constructs a new array object.
40
     *
41
     * @param array<array-key, T> $data The initial items to add to this array.
42
     */
43
    public function __construct(array $data = [])
44
    {
45
        // Invoke offsetSet() for each value added; in this way, sub-classes
46
        // may provide additional logic about values added to the array object.
47
        foreach ($data as $key => $value) {
48
            $this[$key] = $value;
49
        }
50
    }
51
 
52
    /**
53
     * Returns an iterator for this array.
54
     *
55
     * @link http://php.net/manual/en/iteratoraggregate.getiterator.php IteratorAggregate::getIterator()
56
     *
57
     * @return Traversable<array-key, T>
58
     */
59
    public function getIterator(): Traversable
60
    {
61
        return new ArrayIterator($this->data);
62
    }
63
 
64
    /**
65
     * Returns `true` if the given offset exists in this array.
66
     *
67
     * @link http://php.net/manual/en/arrayaccess.offsetexists.php ArrayAccess::offsetExists()
68
     *
69
     * @param array-key $offset The offset to check.
70
     */
71
    public function offsetExists(mixed $offset): bool
72
    {
73
        return isset($this->data[$offset]);
74
    }
75
 
76
    /**
77
     * Returns the value at the specified offset.
78
     *
79
     * @link http://php.net/manual/en/arrayaccess.offsetget.php ArrayAccess::offsetGet()
80
     *
81
     * @param array-key $offset The offset for which a value should be returned.
82
     *
83
     * @return T the value stored at the offset, or null if the offset
84
     *     does not exist.
85
     */
86
    public function offsetGet(mixed $offset): mixed
87
    {
88
        return $this->data[$offset];
89
    }
90
 
91
    /**
92
     * Sets the given value to the given offset in the array.
93
     *
94
     * @link http://php.net/manual/en/arrayaccess.offsetset.php ArrayAccess::offsetSet()
95
     *
96
     * @param array-key | null $offset The offset to set. If `null`, the value
97
     *     may be set at a numerically-indexed offset.
98
     * @param T $value The value to set at the given offset.
99
     */
100
    public function offsetSet(mixed $offset, mixed $value): void
101
    {
102
        if ($offset === null) {
103
            $this->data[] = $value;
104
        } else {
105
            $this->data[$offset] = $value;
106
        }
107
    }
108
 
109
    /**
110
     * Removes the given offset and its value from the array.
111
     *
112
     * @link http://php.net/manual/en/arrayaccess.offsetunset.php ArrayAccess::offsetUnset()
113
     *
114
     * @param array-key $offset The offset to remove from the array.
115
     */
116
    public function offsetUnset(mixed $offset): void
117
    {
118
        unset($this->data[$offset]);
119
    }
120
 
121
    /**
122
     * Returns data suitable for PHP serialization.
123
     *
124
     * @link https://www.php.net/manual/en/language.oop5.magic.php#language.oop5.magic.serialize
125
     * @link https://www.php.net/serialize
126
     *
127
     * @return array<array-key, T>
128
     */
129
    public function __serialize(): array
130
    {
131
        return $this->data;
132
    }
133
 
134
    /**
135
     * Adds unserialized data to the object.
136
     *
137
     * @param array<array-key, T> $data
138
     */
139
    public function __unserialize(array $data): void
140
    {
141
        $this->data = $data;
142
    }
143
 
144
    /**
145
     * Returns the number of items in this array.
146
     *
147
     * @link http://php.net/manual/en/countable.count.php Countable::count()
148
     */
149
    public function count(): int
150
    {
151
        return count($this->data);
152
    }
153
 
154
    public function clear(): void
155
    {
156
        $this->data = [];
157
    }
158
 
159
    /**
160
     * @inheritDoc
161
     */
162
    public function toArray(): array
163
    {
164
        return $this->data;
165
    }
166
 
167
    public function isEmpty(): bool
168
    {
169
        return $this->data === [];
170
    }
171
}