Skip to content

Commit 0fb430b

Browse files
committed
AccessTrait added and used in ArrayView.
1 parent 9df60a6 commit 0fb430b

File tree

3 files changed

+136
-95
lines changed

3 files changed

+136
-95
lines changed

src/Interfaces/ArrayViewInterface.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,14 @@ public function count(): int;
8585

8686
/**
8787
* @param numeric|string|ArraySelectorInterface $offset
88+
*
8889
* @return bool
8990
*/
9091
public function offsetExists($offset): bool;
9192

9293
/**
9394
* @param numeric|string|ArraySelectorInterface $offset
95+
*
9496
* @return T|array<T>
9597
*/
9698
#[\ReturnTypeWillChange]
@@ -99,12 +101,14 @@ public function offsetGet($offset);
99101
/**
100102
* @param numeric|string|ArraySelectorInterface $offset
101103
* @param T|array<T>|ArrayViewInterface<T> $value
104+
*
102105
* @return void
103106
*/
104107
public function offsetSet($offset, $value): void;
105108

106109
/**
107110
* @param numeric|string|ArraySelectorInterface $offset
111+
*
108112
* @return void
109113
*/
110114
public function offsetUnset($offset): void;
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
<?php
2+
3+
namespace Smoren\ArrayView\Traits;
4+
5+
use Smoren\ArrayView\Exceptions\IndexError;
6+
use Smoren\ArrayView\Exceptions\KeyError;
7+
use Smoren\ArrayView\Exceptions\NotSupportedError;
8+
use Smoren\ArrayView\Exceptions\ReadonlyError;
9+
use Smoren\ArrayView\Interfaces\ArraySelectorInterface;
10+
use Smoren\ArrayView\Interfaces\ArrayViewInterface;
11+
use Smoren\ArrayView\Selectors\SliceSelector;
12+
use Smoren\ArrayView\Structs\Slice;
13+
14+
/**
15+
* @template T
16+
*/
17+
trait ArrayViewAccessTrait
18+
{
19+
/**
20+
* @param numeric|string|ArraySelectorInterface $offset
21+
*
22+
* @return bool
23+
*
24+
* {@inheritDoc}
25+
*/
26+
public function offsetExists($offset): bool
27+
{
28+
if (\is_numeric($offset)) {
29+
return $this->numericOffsetExists($offset);
30+
}
31+
32+
if (\is_string($offset) && Slice::isSlice($offset)) {
33+
return true;
34+
}
35+
36+
if ($offset instanceof ArraySelectorInterface) {
37+
return true;
38+
}
39+
40+
return false;
41+
}
42+
43+
/**
44+
* @param numeric|string|ArraySelectorInterface $offset
45+
*
46+
* @return T|array<T>
47+
*
48+
* {@inheritDoc}
49+
*/
50+
#[\ReturnTypeWillChange]
51+
public function offsetGet($offset)
52+
{
53+
/** @var mixed $offset */
54+
if (\is_numeric($offset)) {
55+
if (!$this->numericOffsetExists($offset)) {
56+
throw new IndexError("Index {$offset} is out of range.");
57+
}
58+
return $this->source[$this->convertIndex(\intval($offset))];
59+
}
60+
61+
if (\is_string($offset) && Slice::isSlice($offset)) {
62+
return $this->subview(new SliceSelector($offset))->toArray();
63+
}
64+
65+
if ($offset instanceof ArraySelectorInterface) {
66+
return $this->subview($offset)->toArray();
67+
}
68+
69+
$strOffset = \is_scalar($offset) ? \strval($offset) : \gettype($offset);
70+
throw new KeyError("Invalid key: \"{$strOffset}\".");
71+
}
72+
73+
/**
74+
* @param numeric|string|ArraySelectorInterface $offset
75+
* @param T|array<T>|ArrayViewInterface<T> $value
76+
*
77+
* @return void
78+
*
79+
* {@inheritDoc}
80+
*/
81+
public function offsetSet($offset, $value): void
82+
{
83+
/** @var mixed $offset */
84+
if ($this->isReadonly()) {
85+
throw new ReadonlyError("Cannot modify a readonly view.");
86+
}
87+
88+
if (\is_numeric($offset)) {
89+
if (!$this->numericOffsetExists($offset)) {
90+
throw new IndexError("Index {$offset} is out of range.");
91+
}
92+
93+
// @phpstan-ignore-next-line
94+
$this->source[$this->convertIndex(\intval($offset))] = $value;
95+
return;
96+
}
97+
98+
if (\is_string($offset) && Slice::isSlice($offset)) {
99+
/** @var array<T>|ArrayViewInterface<T> $value */
100+
$this->subview(new SliceSelector($offset))->set($value);
101+
return;
102+
}
103+
104+
if ($offset instanceof ArraySelectorInterface) {
105+
$this->subview($offset)->set($value);
106+
return;
107+
}
108+
109+
$strOffset = \is_scalar($offset) ? \strval($offset) : \gettype($offset);
110+
throw new KeyError("Invalid key: \"{$strOffset}\".");
111+
}
112+
113+
/**
114+
* @param numeric|string|ArraySelectorInterface $offset
115+
*
116+
* @return void
117+
*
118+
* @throws NotSupportedError
119+
*
120+
* {@inheritDoc}
121+
*/
122+
public function offsetUnset($offset): void
123+
{
124+
throw new NotSupportedError();
125+
}
126+
}

src/Views/ArrayView.php

Lines changed: 6 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,14 @@
55
namespace Smoren\ArrayView\Views;
66

77
use Smoren\ArrayView\Exceptions\IndexError;
8-
use Smoren\ArrayView\Exceptions\KeyError;
98
use Smoren\ArrayView\Exceptions\SizeError;
10-
use Smoren\ArrayView\Exceptions\NotSupportedError;
119
use Smoren\ArrayView\Exceptions\ReadonlyError;
1210
use Smoren\ArrayView\Exceptions\ValueError;
13-
use Smoren\ArrayView\Interfaces\ArraySelectorInterface;
1411
use Smoren\ArrayView\Interfaces\ArrayViewInterface;
1512
use Smoren\ArrayView\Interfaces\MaskSelectorInterface;
1613
use Smoren\ArrayView\Selectors\MaskSelector;
1714
use Smoren\ArrayView\Selectors\SliceSelector;
18-
use Smoren\ArrayView\Structs\Slice;
15+
use Smoren\ArrayView\Traits\ArrayViewAccessTrait;
1916
use Smoren\ArrayView\Util;
2017

2118
/**
@@ -25,6 +22,11 @@
2522
*/
2623
class ArrayView implements ArrayViewInterface
2724
{
25+
/**
26+
* @use ArrayViewAccessTrait<T>
27+
*/
28+
use ArrayViewAccessTrait;
29+
2830
/**
2931
* @var array<T>|ArrayViewInterface<T>
3032
*/
@@ -215,97 +217,6 @@ public function isReadonly(): bool
215217
return $this->readonly;
216218
}
217219

218-
/**
219-
* {@inheritDoc}
220-
*/
221-
public function offsetExists($offset): bool
222-
{
223-
if (\is_numeric($offset)) {
224-
return $this->numericOffsetExists($offset);
225-
}
226-
227-
if (\is_string($offset) && Slice::isSlice($offset)) {
228-
return true;
229-
}
230-
231-
if ($offset instanceof ArraySelectorInterface) {
232-
return true;
233-
}
234-
235-
return false;
236-
}
237-
238-
/**
239-
* {@inheritDoc}
240-
*/
241-
#[\ReturnTypeWillChange]
242-
public function offsetGet($offset)
243-
{
244-
/** @var mixed $offset */
245-
if (\is_numeric($offset)) {
246-
if (!$this->numericOffsetExists($offset)) {
247-
throw new IndexError("Index {$offset} is out of range.");
248-
}
249-
return $this->source[$this->convertIndex(\intval($offset))];
250-
}
251-
252-
if (\is_string($offset) && Slice::isSlice($offset)) {
253-
return $this->subview(new SliceSelector($offset))->toArray();
254-
}
255-
256-
if ($offset instanceof ArraySelectorInterface) {
257-
return $this->subview($offset)->toArray();
258-
}
259-
260-
$strOffset = \is_scalar($offset) ? \strval($offset) : \gettype($offset);
261-
throw new KeyError("Invalid key: \"{$strOffset}\".");
262-
}
263-
264-
/**
265-
* {@inheritDoc}
266-
*/
267-
public function offsetSet($offset, $value): void
268-
{
269-
/** @var mixed $offset */
270-
if ($this->isReadonly()) {
271-
throw new ReadonlyError("Cannot modify a readonly view.");
272-
}
273-
274-
if (\is_numeric($offset)) {
275-
if (!$this->numericOffsetExists($offset)) {
276-
throw new IndexError("Index {$offset} is out of range.");
277-
}
278-
279-
// @phpstan-ignore-next-line
280-
$this->source[$this->convertIndex(\intval($offset))] = $value;
281-
return;
282-
}
283-
284-
if (\is_string($offset) && Slice::isSlice($offset)) {
285-
/** @var array<T>|ArrayViewInterface<T> $value */
286-
$this->subview(new SliceSelector($offset))->set($value);
287-
return;
288-
}
289-
290-
if ($offset instanceof ArraySelectorInterface) {
291-
$this->subview($offset)->set($value);
292-
return;
293-
}
294-
295-
$strOffset = \is_scalar($offset) ? \strval($offset) : \gettype($offset);
296-
throw new KeyError("Invalid key: \"{$strOffset}\".");
297-
}
298-
299-
/**
300-
* @throws NotSupportedError
301-
*
302-
* {@inheritDoc}
303-
*/
304-
public function offsetUnset($offset): void
305-
{
306-
throw new NotSupportedError();
307-
}
308-
309220
/**
310221
* {@inheritDoc}
311222
*/

0 commit comments

Comments
 (0)