Cleanups in XArray.

This commit is contained in:
flash 2023-11-09 13:12:34 +00:00
parent 06f0ba27bb
commit 41ac00e3e0
5 changed files with 33 additions and 19 deletions

View file

@ -1 +1 @@
0.2311.91259 0.2311.91312

View file

@ -1,36 +1,63 @@
<?php <?php
// ArrayIterator.php // ArrayIterator.php
// Created: 2022-02-03 // Created: 2022-02-03
// Updated: 2022-02-03 // Updated: 2023-11-09
namespace Index\Collections; namespace Index;
use Iterator; use Iterator;
/**
* Provides an Iterator implementation for normal arrays.
*/
class ArrayIterator implements Iterator { class ArrayIterator implements Iterator {
private array $array; private array $array;
private bool $wasValid = true; private bool $wasValid = true;
/**
* @param array $array Array to iterate upon.
*/
public function __construct(array $array) { public function __construct(array $array) {
$this->array = $array; $this->array = $array;
} }
/**
* Returns the current element.
*
* @return mixed Can return any type.
*/
public function current(): mixed { public function current(): mixed {
return current($this->array); return current($this->array);
} }
/**
* Returns the key of the current element.
*
* @return mixed Returns scalar on success, or null on failure.
*/
public function key(): mixed { public function key(): mixed {
return key($this->array); return key($this->array);
} }
/**
* Moves forward to next element.
*/
public function next(): void { public function next(): void {
$this->wasValid = next($this->array) !== false; $this->wasValid = next($this->array) !== false;
} }
/**
* Rewind the Iterator to the first element.
*/
public function rewind(): void { public function rewind(): void {
$this->wasValid = reset($this->array) !== false; $this->wasValid = reset($this->array) !== false;
} }
/**
* Checks if current position is valid.
*
* @return bool Returns true on success or false on failure.
*/
public function valid(): bool { public function valid(): bool {
return $this->wasValid; return $this->wasValid;
} }

View file

@ -1,10 +0,0 @@
<?php
// IArrayable.php
// Created: 2022-02-03
// Updated: 2022-02-03
namespace Index\Collections;
interface IArrayable {
function toArray(): array;
}

View file

@ -1,7 +1,7 @@
<?php <?php
// XArray.php // XArray.php
// Created: 2022-02-02 // Created: 2022-02-02
// Updated: 2022-02-14 // Updated: 2023-11-09
namespace Index; namespace Index;
@ -10,8 +10,6 @@ use InvalidArgumentException;
use Countable; use Countable;
use Iterator; use Iterator;
use IteratorAggregate; use IteratorAggregate;
use Index\Collections\IArrayable;
use Index\Collections\ArrayIterator;
/** /**
* Provides various helper methods for collections. * Provides various helper methods for collections.
@ -289,8 +287,6 @@ final class XArray {
public static function toArray(iterable $iterable): array { public static function toArray(iterable $iterable): array {
if(is_array($iterable)) if(is_array($iterable))
return $iterable; return $iterable;
if($iterable instanceof IArrayable)
return $iterable->toArray();
$items = []; $items = [];

View file

@ -1,11 +1,12 @@
<?php <?php
// XNumber.php // XNumber.php
// Created: 2023-09-15 // Created: 2023-09-15
// Updated: 2023-09-15 // Updated: 2023-11-09
namespace Index; namespace Index;
use InvalidArgumentException; use InvalidArgumentException;
/** /**
* Provides various helper methods for numbers. * Provides various helper methods for numbers.
*/ */