Passed
Push — master ( 2cda12...3e0ae5 )
by Melech
02:04 queued 37s
created

Indexable::getMappedArrayFromIndexedArray()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 1
dl 0
loc 23
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Valkyrja Framework package.
7
 *
8
 * (c) Melech Mizrachi <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Valkyrja\Type\Model\Trait;
15
16
/**
17
 * Trait Indexable.
18
 *
19
 * @author Melech Mizrachi
20
 */
21
trait Indexable
22
{
23
    /**
24
     * Local cache for reversed indexes.
25
     *
26
     * <code>
27
     *      static::class => [
28
     *           0 => 'property_name',
29
     *           1 => 'other_property_name',
30
     *      ]
31
     * </code>
32
     *
33
     * @var array<string, array<int, string>>
34
     */
35
    protected static array $indexes = [];
36
37
    /**
38
     * @inheritDoc
39
     *
40
     * @return array<string, int>
41
     */
42
    public static function getIndexes(): array
43
    {
44
        return [];
45
    }
46
47
    /**
48
     * @inheritDoc
49
     *
50
     * @return array<int, string>
51
     */
52
    public static function getReversedIndexes(): array
53
    {
54
        return self::$indexes[static::class]
55
            ??= array_flip(static::getIndexes());
56
    }
57
58
    /**
59
     * @inheritDoc
60
     *
61
     * @param array<int, mixed> $properties The properties
62
     *
63
     * @return array<string, mixed>
64
     */
65
    public static function getMappedArrayFromIndexedArray(array $properties = []): array
66
    {
67
        $mappedProperties = [];
68
        $indexes          = static::getIndexes();
69
        $reversedIndexes  = static::getReversedIndexes();
70
71
        /**
72
         * @var int   $index
73
         * @var mixed $value
74
         */
75
        foreach ($properties as $index => $value) {
76
            $name = $reversedIndexes[$index] ?? null;
77
78
            if ($name !== null) {
79
                /** @psalm-suppress MixedAssignment */
80
                $mappedProperties[$name] = $value;
81
            }
82
        }
83
84
        // Sort the array by index
85
        uksort($mappedProperties, static fn (string $a, string $b): int => $indexes[$a] <=> $indexes[$b]);
86
87
        return $mappedProperties;
88
    }
89
90
    /**
91
     * @inheritDoc
92
     *
93
     * @param array<string, mixed> $properties The properties
94
     *
95
     * @return array<int, mixed>
96
     */
97
    public static function getIndexedArrayFromMappedArray(array $properties = []): array
98
    {
99
        $indexedArray = [];
100
        $indexes      = static::getIndexes();
101
102
        /**
103
         * @var string $name
104
         * @var mixed  $value
105
         */
106
        foreach ($properties as $name => $value) {
107
            $index = $indexes[$name] ?? null;
108
109
            if ($index !== null) {
110
                /** @psalm-suppress MixedAssignment */
111
                $indexedArray[$index] = $value;
112
            }
113
        }
114
115
        // Sort the array by index
116
        ksort($indexedArray);
117
118
        return $indexedArray;
119
    }
120
121
    /**
122
     * @inheritDoc
123
     *
124
     * @param array<int, mixed> $properties The properties
125
     */
126
    public static function fromIndexedArray(array $properties): static
127
    {
128
        /** @var static $model */
129
        $model = static::fromArray(static::getMappedArrayFromIndexedArray($properties));
130
131
        return $model;
132
    }
133
134
    /**
135
     * @inheritDoc
136
     *
137
     * @param array<int, mixed> $properties The properties
138
     */
139
    public function updateIndexedProperties(array $properties): void
140
    {
141
        $this->updateProperties(static::getMappedArrayFromIndexedArray($properties));
0 ignored issues
show
Bug introduced by
It seems like updateProperties() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

141
        $this->/** @scrutinizer ignore-call */ 
142
               updateProperties(static::getMappedArrayFromIndexedArray($properties));
Loading history...
142
    }
143
144
    /**
145
     * @inheritDoc
146
     *
147
     * @param array<int, mixed> $properties The properties to modify
148
     */
149
    public function withIndexedProperties(array $properties): static
150
    {
151
        /** @var static $model */
152
        $model = $this->withProperties(static::getMappedArrayFromIndexedArray($properties));
0 ignored issues
show
Bug introduced by
It seems like withProperties() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

152
        /** @scrutinizer ignore-call */ 
153
        $model = $this->withProperties(static::getMappedArrayFromIndexedArray($properties));
Loading history...
153
154
        return $model;
155
    }
156
157
    /**
158
     * @inheritDoc
159
     *
160
     * @param string ...$properties [optional] An array of properties to return
161
     *
162
     * @return array<int, mixed>
163
     */
164
    public function asIndexedArray(string ...$properties): array
165
    {
166
        return static::getIndexedArrayFromMappedArray($this->asArray(...$properties));
0 ignored issues
show
Bug introduced by
It seems like asArray() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

166
        return static::getIndexedArrayFromMappedArray($this->/** @scrutinizer ignore-call */ asArray(...$properties));
Loading history...
167
    }
168
169
    /**
170
     * @inheritDoc
171
     *
172
     * @return array<int, mixed>
173
     */
174
    public function asChangedIndexedArray(): array
175
    {
176
        return static::getIndexedArrayFromMappedArray($this->asChangedArray());
0 ignored issues
show
Bug introduced by
The method asChangedArray() does not exist on Valkyrja\Type\Model\Trait\Indexable. Did you maybe mean asChangedIndexedArray()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

176
        return static::getIndexedArrayFromMappedArray($this->/** @scrutinizer ignore-call */ asChangedArray());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
177
    }
178
179
    /**
180
     * @inheritDoc
181
     *
182
     * @return array<int, mixed>
183
     */
184
    public function asOriginalIndexedArray(): array
185
    {
186
        return static::getIndexedArrayFromMappedArray($this->asOriginalArray());
0 ignored issues
show
Bug introduced by
The method asOriginalArray() does not exist on Valkyrja\Type\Model\Trait\Indexable. Did you maybe mean asOriginalIndexedArray()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

186
        return static::getIndexedArrayFromMappedArray($this->/** @scrutinizer ignore-call */ asOriginalArray());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
187
    }
188
}
189