Skip to content

Commit 986c0ef

Browse files
committed
add new core files
1 parent 16d7b32 commit 986c0ef

2 files changed

Lines changed: 105 additions & 0 deletions

File tree

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
namespace TypeRocket\Utility\Traits;
3+
4+
trait ArrayAccessible
5+
{
6+
/**
7+
* @var array
8+
*/
9+
protected array $_items = [];
10+
11+
/**
12+
* @param int|string $offset
13+
* @param mixed $value
14+
* @return void
15+
*/
16+
public function offsetSet($offset, $value) : void
17+
{
18+
$location = $this->_location ?? '_items';
19+
20+
if (is_null($offset)) {
21+
$this->{$location}[] = $value;
22+
} else {
23+
$this->{$location}[$offset] = $value;
24+
}
25+
}
26+
27+
/**
28+
* @param int|string $offset
29+
* @return bool
30+
*/
31+
public function offsetExists($offset) : bool
32+
{
33+
$location = $this->_location ?? '_items';
34+
35+
return isset($this->{$location}[$offset]);
36+
}
37+
38+
/**
39+
* @param int|string $offset
40+
* @return void
41+
*/
42+
public function offsetUnset($offset) : void
43+
{
44+
$location = $this->_location ?? '_items';
45+
46+
unset($this->{$location}[$offset]);
47+
}
48+
49+
/**
50+
* @param int|string $offset
51+
* @return mixed|null
52+
*/
53+
public function offsetGet($offset)
54+
{
55+
$location = $this->_location ?? '_items';
56+
57+
return $this->{$location}[$offset] ?? null;
58+
}
59+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace TypeRocket\Utility\Traits;
4+
5+
trait ArrayIterable
6+
{
7+
/**
8+
* @var array
9+
*/
10+
protected array $_items = [];
11+
12+
function rewind()
13+
{
14+
$location = $this->_location ?? '_items';
15+
16+
reset($this->{$location});
17+
}
18+
19+
function current()
20+
{
21+
$location = $this->_location ?? '_items';
22+
23+
return current($this->{$location});
24+
}
25+
26+
function key()
27+
{
28+
$location = $this->_location ?? '_items';
29+
30+
return key($this->{$location});
31+
}
32+
33+
function next()
34+
{
35+
$location = $this->_location ?? '_items';
36+
37+
next($this->{$location});
38+
}
39+
40+
function valid()
41+
{
42+
$location = $this->_location ?? '_items';
43+
44+
return key($this->{$location}) !== null;
45+
}
46+
}

0 commit comments

Comments
 (0)