Cleanups in XArray.

This commit is contained in:
Pachira 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
// ArrayIterator.php
// Created: 2022-02-03
// Updated: 2022-02-03
// Updated: 2023-11-09
namespace Index\Collections;
namespace Index;
use Iterator;
/**
* Provides an Iterator implementation for normal arrays.
*/
class ArrayIterator implements Iterator {
private array $array;
private bool $wasValid = true;
/**
* @param array $array Array to iterate upon.
*/
public function __construct(array $array) {
$this->array = $array;
}
/**
* Returns the current element.
*
* @return mixed Can return any type.
*/
public function current(): mixed {
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 {
return key($this->array);
}
/**
* Moves forward to next element.
*/
public function next(): void {
$this->wasValid = next($this->array) !== false;
}
/**
* Rewind the Iterator to the first element.
*/
public function rewind(): void {
$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 {
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
// XArray.php
// Created: 2022-02-02
// Updated: 2022-02-14
// Updated: 2023-11-09
namespace Index;
@ -10,8 +10,6 @@ use InvalidArgumentException;
use Countable;
use Iterator;
use IteratorAggregate;
use Index\Collections\IArrayable;
use Index\Collections\ArrayIterator;
/**
* Provides various helper methods for collections.
@ -289,8 +287,6 @@ final class XArray {
public static function toArray(iterable $iterable): array {
if(is_array($iterable))
return $iterable;
if($iterable instanceof IArrayable)
return $iterable->toArray();
$items = [];

View file

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