|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace kalanis\kw_input\Traits; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use ArrayIterator; |
|
7
|
|
|
use kalanis\kw_input\Entries\Entry; |
|
8
|
|
|
use kalanis\kw_input\Interfaces; |
|
9
|
|
|
use Traversable; |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Trait TInputEntries |
|
14
|
|
|
* @package kalanis\kw_input\Traits |
|
15
|
|
|
*/ |
|
16
|
|
|
trait TInputEntries |
|
17
|
|
|
{ |
|
18
|
|
|
use TNullBytes; |
|
19
|
|
|
|
|
20
|
|
|
/** @var array<string, Interfaces\IEntry> */ |
|
21
|
|
|
protected array $input = []; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @param string|int $offset |
|
25
|
|
|
* @return bool |
|
26
|
|
|
*/ |
|
27
|
7 |
|
public function offsetExists($offset): bool |
|
28
|
|
|
{ |
|
29
|
7 |
|
return isset($this->input[$this->removeNullBytes(strval($offset))]); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param string|int $offset |
|
34
|
|
|
* @return Interfaces\IEntry|null |
|
35
|
|
|
*/ |
|
36
|
|
|
#[\ReturnTypeWillChange] |
|
37
|
7 |
|
public function offsetGet($offset) |
|
38
|
|
|
{ |
|
39
|
7 |
|
return $this->offsetExists($offset) ? $this->input[$this->removeNullBytes(strval($offset))] : null; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param string|int $offset |
|
44
|
|
|
* @param mixed|null $value |
|
45
|
|
|
*/ |
|
46
|
4 |
|
public function offsetSet($offset, $value): void |
|
47
|
|
|
{ |
|
48
|
4 |
|
$offset = $this->removeNullBytes(strval($offset)); |
|
49
|
4 |
|
if ($this->offsetExists($offset)) { |
|
50
|
3 |
|
$current = $this->offsetGet($offset); |
|
51
|
3 |
|
$source = $current && !empty($current->getSource()) |
|
52
|
2 |
|
? $current->getSource() |
|
53
|
3 |
|
: $this->defaultSource() |
|
54
|
3 |
|
; |
|
55
|
3 |
|
$entry = new Entry(); |
|
56
|
3 |
|
if (is_object($value) && ($value instanceof Interfaces\IEntry)) { |
|
57
|
1 |
|
$entry->setEntry(strval($source), $offset, $value->getValue()); |
|
58
|
|
|
} else { |
|
59
|
2 |
|
$entry->setEntry(strval($source), $offset, $value); |
|
60
|
|
|
} |
|
61
|
3 |
|
$this->input[$offset] = $entry; |
|
62
|
4 |
|
} elseif ($value instanceof Interfaces\IEntry) { |
|
63
|
2 |
|
$entry = new Entry(); |
|
64
|
2 |
|
$entry->setEntry($this->defaultSource(), $offset, $value->getValue()); |
|
65
|
2 |
|
$this->input[$offset] = $value; |
|
66
|
|
|
} else { |
|
67
|
3 |
|
$entry = new Entry(); |
|
68
|
3 |
|
$entry->setEntry($this->defaultSource(), $offset, $value); |
|
69
|
3 |
|
$this->input[$offset] = $entry; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param string|int $offset |
|
75
|
|
|
*/ |
|
76
|
2 |
|
public final function offsetUnset($offset): void |
|
77
|
|
|
{ |
|
78
|
2 |
|
if ($this->offsetExists($offset)) { |
|
79
|
2 |
|
unset($this->input[$this->removeNullBytes(strval($offset))]); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Return all inputs as array iterator |
|
85
|
|
|
* @return Traversable<string, Interfaces\IEntry> |
|
86
|
|
|
*/ |
|
87
|
3 |
|
public function getIterator(): Traversable |
|
88
|
|
|
{ |
|
89
|
3 |
|
return new ArrayIterator( |
|
90
|
3 |
|
$this->input, |
|
91
|
3 |
|
ArrayIterator::STD_PROP_LIST | ArrayIterator::ARRAY_AS_PROPS |
|
92
|
3 |
|
); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
abstract protected function defaultSource(): string; |
|
96
|
|
|
} |
|
97
|
|
|
|