ImmutableCollection::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stratadox\ImmutableCollection;
6
7
use function count;
8
use function is_null;
9
use SplFixedArray;
10
use Stratadox\Collection\Collection;
11
use Stratadox\Collection\NotAllowed;
12
13
/**
14
 * Immutable implementation of the @see Collection interface.
15
 *
16
 * Extends the SplFixedArray class (rather than keeping a private array property)
17
 * because it is faster, less memory consuming and truly immutable.
18
 * Methods that would have provided mutability to the SplFixedArray class are
19
 * overwritten and finalised, thus prohibiting set, unset and resize operations.
20
 * This way, neither an inheritance nor reflection can mutate the data.
21
 *
22
 * @package Stratadox\Collection
23
 * @author  Stratadox
24
 */
25
abstract class ImmutableCollection extends SplFixedArray implements Collection
26
{
27
    /**
28
     * Make a new immutable collection.
29
     *
30
     * @param array ...$ofTheItems The items to use.
31
     */
32
    public function __construct(...$ofTheItems)
33
    {
34
        parent::__construct(count($ofTheItems));
35
36
        foreach ($ofTheItems as $thePosition => $theItem) {
37
            parent::offsetSet($thePosition, $theItem);
38
        }
39
    }
40
41
    /**
42
     * Imports a collection from an array.
43
     *
44
     * @param array     $array        The array to import.
45
     * @param bool|null $save_indexes Ignored parameter.
46
     * @return self|static
47
     */
48
    final public static function fromArray($array, $save_indexes = null): self
49
    {
50
        return new static(...$array);
51
    }
52
53
    /**
54
     * Disallows use of the offsetSet method.
55
     *
56
     * @param int|mixed $index The index that may not be mutated.
57
     * @param mixed     $value The value that will not be written.
58
     * @throws NotAllowed      Whenever called upon.
59
     */
60
    final public function offsetSet($index, $value): void
61
    {
62
        if (is_null($index)) {
63
            throw CannotAlterCollection::byAddingTo($this);
64
        }
65
        throw CannotAlterCollection::byOverWriting($this, $index);
66
    }
67
68
    /**
69
     * Disallows use of the offsetUnset method.
70
     *
71
     * @param int|mixed $index The index that may not be unset.
72
     * @throws NotAllowed      Whenever called upon.
73
     */
74
    final public function offsetUnset($index): void
75
    {
76
        throw CannotAlterCollection::byRemoving($this, $index);
77
    }
78
79
    /**
80
     * Disallows use of the setSize method.
81
     *
82
     * @param int $size   The new size that will not be assigned.
83
     * @return bool       In case false === true.
84
     * @throws NotAllowed Whenever called upon.
85
     */
86
    final public function setSize($size): bool
87
    {
88
        throw CannotAlterCollection::byResizingTo($this, $size);
89
    }
90
91
    /**
92
     * Yields an array copy of the collection.
93
     *
94
     * @return array An array representation of the items in the collection.
95
     */
96
    public function items(): array
97
    {
98
        return $this->toArray();
99
    }
100
101
    /**
102
     * Produces a new copy of this collection, using a new set of values.
103
     *
104
     * @param array $items       The items to include in the new copy.
105
     * @return static The altered copy.
106
     */
107
    protected function newCopy(array $items): Collection
108
    {
109
        return new static(...$items);
110
    }
111
}
112