Skip to content

Commit e7616e7

Browse files
committed
Introduction of stricter column definitions
1 parent b80c02c commit e7616e7

25 files changed

+3107
-0
lines changed
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
<?php
2+
/**
3+
* Abstract collection.
4+
*
5+
* @since 3.0.0
6+
*
7+
* @package StellarWP\Schema\V3\Collections
8+
*/
9+
10+
declare( strict_types=1 );
11+
12+
namespace StellarWP\Schema\V3\Collections;
13+
14+
use ArrayAccess;
15+
use Countable;
16+
use Iterator;
17+
use JsonSerializable;
18+
use ReturnTypeWillChange;
19+
20+
/**
21+
* Abstract collection.
22+
*
23+
* @since 3.0.0
24+
*
25+
* @package StellarWP\Schema\V3\Collections
26+
*/
27+
abstract class Collection implements ArrayAccess, Iterator, Countable, JsonSerializable {
28+
/**
29+
* Collection of items.
30+
*
31+
* @since 3.0.0
32+
*
33+
* @var array
34+
*/
35+
protected array $resources = [];
36+
37+
/**
38+
* Constructor.
39+
*
40+
* @since 3.0.0
41+
*
42+
* @param array $resources An array of items.
43+
*/
44+
public function __construct( array $resources = [] ) {
45+
foreach ( $resources as $offset => $value ) {
46+
$this->set( (string) $offset, $value );
47+
}
48+
}
49+
50+
/**
51+
* Sets a value in the collection.
52+
*
53+
* @since 3.0.0
54+
*
55+
* @param string $offset The offset to set.
56+
* @param mixed $value The value to set.
57+
*/
58+
protected function set( string $offset, $value ): void {
59+
$this->resources[ $offset ] = $value;
60+
}
61+
62+
/**
63+
* @inheritDoc
64+
*/
65+
#[ReturnTypeWillChange]
66+
public function current() {
67+
return current( $this->resources );
68+
}
69+
70+
/**
71+
* @inheritDoc
72+
*/
73+
public function key(): ?string {
74+
return (string) key( $this->resources );
75+
}
76+
77+
/**
78+
* @inheritDoc
79+
*/
80+
public function next(): void {
81+
next( $this->resources );
82+
}
83+
84+
/**
85+
* @inheritDoc
86+
*
87+
* @param TKey $offset The offset to check.
88+
*
89+
* @return bool
90+
*/
91+
public function offsetExists( $offset ): bool {
92+
return array_key_exists( $offset, $this->resources );
93+
}
94+
95+
/**
96+
* @inheritDoc
97+
*
98+
* @param TKey $offset The offset to get.
99+
*
100+
* @return mixed|null
101+
*/
102+
#[ReturnTypeWillChange]
103+
public function offsetGet( $offset ) {
104+
return $this->resources[ $offset ] ?? null;
105+
}
106+
107+
/**
108+
* @inheritDoc
109+
*
110+
* @param TKey $offset The offset to set.
111+
* @param mixed $value The value to set.
112+
*/
113+
public function offsetSet( $offset, $value ): void {
114+
if ( ! $offset ) {
115+
$offset = (string) count( $this->resources );
116+
}
117+
$this->set( $offset, $value );
118+
}
119+
120+
/**
121+
* @inheritDoc
122+
*
123+
* @param TKey $offset The offset to unset.
124+
*/
125+
public function offsetUnset( $offset ): void {
126+
unset( $this->resources[ $offset ] );
127+
}
128+
129+
/**
130+
* @inheritDoc
131+
*/
132+
public function rewind(): void {
133+
reset( $this->resources );
134+
}
135+
136+
/**
137+
* @inheritDoc
138+
*/
139+
public function valid(): bool {
140+
return key( $this->resources ) !== null;
141+
}
142+
143+
/**
144+
* @inheritDoc
145+
*/
146+
public function count(): int {
147+
return count( $this->resources );
148+
}
149+
150+
/**
151+
* Returns the collection as an array.
152+
*
153+
* @since 3.0.0
154+
*
155+
* @return array
156+
*/
157+
public function jsonSerialize(): array {
158+
return $this->resources;
159+
}
160+
161+
/**
162+
* Maps the collection to an array.
163+
*
164+
* @since 3.0.0
165+
*
166+
* @param callable $callback The callback to map the collection to an array.
167+
*
168+
* @return array
169+
*/
170+
public function map( callable $callback ): array {
171+
return array_map( $callback, $this->resources );
172+
}
173+
174+
/**
175+
* Filters the collection.
176+
*
177+
* @since 3.0.0
178+
*
179+
* @param callable $callback The callback to filter the collection.
180+
*
181+
* @return Collection
182+
*/
183+
public function filter( callable $callback ): Collection {
184+
return new static( array_filter( $this->resources, $callback ) );
185+
}
186+
187+
/**
188+
* Gets a resource from the collection.
189+
*
190+
* @since 3.0.0
191+
*
192+
* @param TKey $key The key to get.
193+
*
194+
* @return ?mixed
195+
*/
196+
#[ReturnTypeWillChange]
197+
public function get( string $key ) {
198+
return $this->offsetGet( $key );
199+
}
200+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
/**
3+
* The interface for the auto incrementable column.
4+
*
5+
* @since TBD
6+
*
7+
* @package StellarWP\Schema\V3\Columns\Contracts
8+
*/
9+
10+
declare( strict_types=1 );
11+
12+
namespace StellarWP\Schema\V3\Columns\Contracts;
13+
14+
/**
15+
* Interface Auto_Incrementable
16+
*
17+
* @since TBD
18+
*
19+
* @package StellarWP\Schema\V3\Columns\Contracts
20+
*/
21+
interface Auto_Incrementable {
22+
/**
23+
* Get the auto increment of the column.
24+
*
25+
* @return bool Whether the column is auto increment.
26+
*/
27+
public function get_auto_increment(): bool;
28+
29+
/**
30+
* Set the auto increment of the column.
31+
*
32+
* @param bool $auto_increment Whether the column is auto increment.
33+
*
34+
* @return self
35+
*/
36+
public function set_auto_increment( bool $auto_increment ): self;
37+
}

0 commit comments

Comments
 (0)