| 1 |
<?php |
| 2 |
/** |
| 3 |
* @file |
| 4 |
* @license https://opensource.org/licenses/Apache-2.0 Apache-2.0 |
| 5 |
*/ |
| 6 |
|
| 7 |
namespace Wikimedia\CSS\Objects; |
| 8 |
|
| 9 |
use ArrayAccess; |
| 10 |
use Countable; |
| 11 |
use InvalidArgumentException; |
| 12 |
use OutOfBoundsException; |
| 13 |
use SeekableIterator; |
| 14 |
use Wikimedia\CSS\Util; |
| 15 |
|
| 16 |
/** |
| 17 |
* Represent a list of CSS objects |
| 18 |
* @template T of CSSObject |
| 19 |
* @implements SeekableIterator<T> |
| 20 |
* @implements ArrayAccess<T> |
| 21 |
*/ |
| 22 |
class CSSObjectList implements Countable, SeekableIterator, ArrayAccess, CSSObject { |
| 23 |
|
| 24 |
/** @var class-string The specific class of object contained */ |
| 25 |
protected static $objectType; |
| 26 |
|
| 27 |
/** @var T[] The objects contained */ |
| 28 |
protected $objects; |
| 29 |
|
| 30 |
/** @var int */ |
| 31 |
protected $offset = 0; |
| 32 |
|
| 33 |
/** |
| 34 |
* Additional validation for objects |
| 35 |
* @param CSSObject[] $objects |
| 36 |
*/ |
| 37 |
protected static function testObjects( array $objects ) { |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* @param T[] $objects |
| 42 |
*/ |
| 43 |
public function __construct( array $objects = [] ) { |
| 44 |
Util::assertAllInstanceOf( $objects, static::$objectType, static::class ); |
| 45 |
// @phan-suppress-next-line PhanTypeMismatchArgument |
| 46 |
static::testObjects( $objects ); |
| 47 |
$this->objects = array_values( $objects ); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Insert one or more objects into the list |
| 52 |
* @param T|T[]|CSSObjectList<T> $objects An object to add, or an array of objects. |
| 53 |
* @param int|null $index Insert the objects at this index. If omitted, the |
| 54 |
* objects are added at the end. |
| 55 |
*/ |
| 56 |
public function add( $objects, $index = null ) { |
| 57 |
if ( $objects instanceof static ) { |
| 58 |
$objects = $objects->objects; |
| 59 |
} elseif ( is_array( $objects ) ) { |
| 60 |
Util::assertAllInstanceOf( $objects, static::$objectType, static::class ); |
| 61 |
$objects = array_values( $objects ); |
| 62 |
// @phan-suppress-next-line PhanTypeMismatchArgument |
| 63 |
static::testObjects( $objects ); |
| 64 |
} else { |
| 65 |
if ( !$objects instanceof static::$objectType ) { |
| 66 |
throw new InvalidArgumentException( |
| 67 |
static::class . ' may only contain instances of ' . static::$objectType . '.' |
| 68 |
); |
| 69 |
} |
| 70 |
$objects = [ $objects ]; |
| 71 |
static::testObjects( $objects ); |
| 72 |
} |
| 73 |
|
| 74 |
if ( $index === null ) { |
| 75 |
$index = count( $this->objects ); |
| 76 |
} elseif ( $index < 0 || $index > count( $this->objects ) ) { |
| 77 |
throw new OutOfBoundsException( 'Index is out of range.' ); |
| 78 |
} |
| 79 |
|
| 80 |
array_splice( $this->objects, $index, 0, $objects ); |
| 81 |
if ( $this->offset > $index ) { |
| 82 |
$this->offset += count( $objects ); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Remove an object from the list |
| 88 |
* @param int $index |
| 89 |
* @return T The removed object |
| 90 |
*/ |
| 91 |
public function remove( $index ) { |
| 92 |
if ( $index < 0 || $index >= count( $this->objects ) ) { |
| 93 |
throw new OutOfBoundsException( 'Index is out of range.' ); |
| 94 |
} |
| 95 |
$ret = $this->objects[$index]; |
| 96 |
array_splice( $this->objects, $index, 1 ); |
| 97 |
|
| 98 |
// This works most sanely with foreach() and removing the current index |
| 99 |
if ( $this->offset >= $index ) { |
| 100 |
$this->offset--; |
| 101 |
} |
| 102 |
|
| 103 |
return $ret; |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Extract a slice of the list |
| 108 |
* @param int $offset |
| 109 |
* @param int|null $length |
| 110 |
* @return T[] The objects in the slice |
| 111 |
*/ |
| 112 |
public function slice( $offset, $length = null ) { |
| 113 |
return array_slice( $this->objects, $offset, $length ); |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Clear the list |
| 118 |
*/ |
| 119 |
public function clear() { |
| 120 |
$this->objects = []; |
| 121 |
$this->offset = 0; |
| 122 |
} |
| 123 |
|
| 124 |
// Countable interface |
| 125 |
|
| 126 |
/** @inheritDoc */ |
| 127 |
public function count(): int { |
| 128 |
return count( $this->objects ); |
| 129 |
} |
| 130 |
|
| 131 |
// SeekableIterator interface |
| 132 |
|
| 133 |
/** @inheritDoc */ |
| 134 |
public function seek( int $offset ): void { |
| 135 |
if ( $offset < 0 || $offset >= count( $this->objects ) ) { |
| 136 |
throw new OutOfBoundsException( 'Offset is out of range.' ); |
| 137 |
} |
| 138 |
$this->offset = $offset; |
| 139 |
} |
| 140 |
|
| 141 |
/** @inheritDoc */ |
| 142 |
public function current(): mixed { |
| 143 |
return $this->objects[$this->offset] ?? null; |
| 144 |
} |
| 145 |
|
| 146 |
/** @inheritDoc */ |
| 147 |
public function key(): int { |
| 148 |
return $this->offset; |
| 149 |
} |
| 150 |
|
| 151 |
/** @inheritDoc */ |
| 152 |
public function next(): void { |
| 153 |
$this->offset++; |
| 154 |
} |
| 155 |
|
| 156 |
/** @inheritDoc */ |
| 157 |
public function rewind(): void { |
| 158 |
$this->offset = 0; |
| 159 |
} |
| 160 |
|
| 161 |
/** @inheritDoc */ |
| 162 |
public function valid(): bool { |
| 163 |
return isset( $this->objects[$this->offset] ); |
| 164 |
} |
| 165 |
|
| 166 |
// ArrayAccess interface |
| 167 |
|
| 168 |
/** @inheritDoc */ |
| 169 |
public function offsetExists( $offset ): bool { |
| 170 |
return isset( $this->objects[$offset] ); |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* @param mixed $offset |
| 175 |
* @return T |
| 176 |
*/ |
| 177 |
public function offsetGet( $offset ): CSSObject { |
| 178 |
if ( !is_numeric( $offset ) || (float)(int)$offset !== (float)$offset ) { |
| 179 |
throw new InvalidArgumentException( 'Offset must be an integer.' ); |
| 180 |
} |
| 181 |
if ( $offset < 0 || $offset > count( $this->objects ) ) { |
| 182 |
throw new OutOfBoundsException( 'Offset is out of range.' ); |
| 183 |
} |
| 184 |
return $this->objects[$offset]; |
| 185 |
} |
| 186 |
|
| 187 |
/** @inheritDoc */ |
| 188 |
public function offsetSet( $offset, $value ): void { |
| 189 |
if ( !$value instanceof static::$objectType ) { |
| 190 |
throw new InvalidArgumentException( |
| 191 |
static::class . ' may only contain instances of ' . static::$objectType . '.' |
| 192 |
); |
| 193 |
} |
| 194 |
static::testObjects( [ $value ] ); |
| 195 |
if ( !is_numeric( $offset ) || (float)(int)$offset !== (float)$offset ) { |
| 196 |
throw new InvalidArgumentException( 'Offset must be an integer.' ); |
| 197 |
} |
| 198 |
if ( $offset < 0 || $offset > count( $this->objects ) ) { |
| 199 |
throw new OutOfBoundsException( 'Offset is out of range.' ); |
| 200 |
} |
| 201 |
$this->objects[$offset] = $value; |
| 202 |
} |
| 203 |
|
| 204 |
/** @inheritDoc */ |
| 205 |
public function offsetUnset( $offset ): void { |
| 206 |
if ( isset( $this->objects[$offset] ) && $offset !== count( $this->objects ) - 1 ) { |
| 207 |
throw new OutOfBoundsException( 'Cannot leave holes in the list.' ); |
| 208 |
} |
| 209 |
unset( $this->objects[$offset] ); |
| 210 |
} |
| 211 |
|
| 212 |
// CSSObject interface |
| 213 |
|
| 214 |
/** @inheritDoc */ |
| 215 |
public function getPosition() { |
| 216 |
$ret = null; |
| 217 |
foreach ( $this->objects as $obj ) { |
| 218 |
$pos = $obj->getPosition(); |
| 219 |
if ( $pos[0] >= 0 && ( |
| 220 |
!$ret || $pos[0] < $ret[0] || ( $pos[0] === $ret[0] && $pos[1] < $ret[1] ) |
| 221 |
) ) { |
| 222 |
$ret = $pos; |
| 223 |
} |
| 224 |
} |
| 225 |
return $ret ?: [ -1, -1 ]; |
| 226 |
} |
| 227 |
|
| 228 |
/** |
| 229 |
* Return the tokens to use to separate list items |
| 230 |
* @param T $left |
| 231 |
* @param ?T $right |
| 232 |
* @return Token[] |
| 233 |
*/ |
| 234 |
protected function getSeparator( CSSObject $left, ?CSSObject $right = null ) { |
| 235 |
return []; |
| 236 |
} |
| 237 |
|
| 238 |
/** |
| 239 |
* @param string $function Function to call, toTokenArray() or toComponentValueArray() |
| 240 |
* @return Token[]|ComponentValue[] |
| 241 |
*/ |
| 242 |
private function toTokenOrCVArray( $function ) { |
| 243 |
$ret = []; |
| 244 |
$l = count( $this->objects ); |
| 245 |
foreach ( $this->objects as $i => $iValue ) { |
| 246 |
// Manually looping and appending turns out to be noticeably faster than array_merge. |
| 247 |
foreach ( $iValue->$function() as $v ) { |
| 248 |
$ret[] = $v; |
| 249 |
} |
| 250 |
// @phan-suppress-next-line PhanTemplateTypeConstraintViolation |
| 251 |
$sep = $this->getSeparator( $iValue, $i + 1 < $l ? $this->objects[$i + 1] : null ); |
| 252 |
foreach ( $sep as $v ) { |
| 253 |
$ret[] = $v; |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
return $ret; |
| 258 |
} |
| 259 |
|
| 260 |
/** @inheritDoc */ |
| 261 |
public function toTokenArray() { |
| 262 |
return $this->toTokenOrCVArray( __FUNCTION__ ); |
| 263 |
} |
| 264 |
|
| 265 |
/** @inheritDoc */ |
| 266 |
public function toComponentValueArray() { |
| 267 |
return $this->toTokenOrCVArray( __FUNCTION__ ); |
| 268 |
} |
| 269 |
|
| 270 |
public function __toString() { |
| 271 |
return Util::stringify( $this ); |
| 272 |
} |
| 273 |
} |