Skip to content

Commit

Permalink
PHPdoc, strict types added.
Browse files Browse the repository at this point in the history
  • Loading branch information
Smoren committed Mar 12, 2024
1 parent 24aa8fb commit 8b4f09d
Show file tree
Hide file tree
Showing 17 changed files with 106 additions and 23 deletions.
2 changes: 2 additions & 0 deletions src/Exceptions/IndexError.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Smoren\ArrayView\Exceptions;

class IndexError extends \RuntimeException
Expand Down
2 changes: 2 additions & 0 deletions src/Exceptions/KeyError.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Smoren\ArrayView\Exceptions;

class KeyError extends \RuntimeException
Expand Down
2 changes: 2 additions & 0 deletions src/Exceptions/NotSupportedError.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Smoren\ArrayView\Exceptions;

class NotSupportedError extends \Exception
Expand Down
2 changes: 2 additions & 0 deletions src/Exceptions/ReadonlyError.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Smoren\ArrayView\Exceptions;

class ReadonlyError extends \RuntimeException
Expand Down
2 changes: 2 additions & 0 deletions src/Exceptions/SizeError.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Smoren\ArrayView\Exceptions;

class SizeError extends \RuntimeException
Expand Down
2 changes: 2 additions & 0 deletions src/Exceptions/ValueError.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Smoren\ArrayView\Exceptions;

class ValueError extends \RuntimeException
Expand Down
97 changes: 74 additions & 23 deletions src/Interfaces/ArrayViewInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,80 +5,121 @@
namespace Smoren\ArrayView\Interfaces;

/**
* @template T
* Interface for a view of an array with additional methods
* for filtering, mapping, and transforming the data.
*
* @template T The type of elements in the array
*
* @extends \ArrayAccess<int, T|array<T>>
* @extends \IteratorAggregate<int, T>
*/
interface ArrayViewInterface extends \ArrayAccess, \IteratorAggregate, \Countable
{
/**
* @param array<T>|ArrayViewInterface<T> $source
* @param bool|null $readonly
* @return ArrayViewInterface<T>
* Creates an ArrayView instance from the given source array or ArrayView.
*
* * If the source is not an ArrayView, a new ArrayView is created with the provided source.
* * If the source is an ArrayView and the `readonly` parameter is specified as `true`,
* a new readonly ArrayView is created.
* * If the source is an ArrayView and it is already readonly, the same ArrayView is returned.
*
* @param array<T>|ArrayViewInterface<T> $source The source array or ArrayView to create a view from.
* @param bool|null $readonly Optional flag to indicate whether the view should be readonly.
*
* @return ArrayViewInterface<T> An ArrayView instance based on the source array or ArrayView.
*/
public static function toView(&$source, ?bool $readonly = null): ArrayViewInterface;

/**
* @param array<T>|ArrayViewInterface<T> $source
* @param bool|null $readonly
* @return ArrayViewInterface<T>
* Creates an unlinked from source ArrayView instance from the given source array or ArrayView.
*
* * If the source is not an ArrayView, a new ArrayView is created with the provided source.
* * If the source is an ArrayView and the `readonly` parameter is specified as `true`,
* a new readonly ArrayView is created.
* * If the source is an ArrayView and it is already readonly, the same ArrayView is returned.
*
* @param array<T>|ArrayViewInterface<T> $source The source array or ArrayView to create a view from.
* @param bool|null $readonly Optional flag to indicate whether the view should be readonly.
*
* @return ArrayViewInterface<T> An ArrayView instance based on the source array or ArrayView.
*/
public static function toUnlinkedView($source, ?bool $readonly = null): ArrayViewInterface;

/**
* @return array<T>
* Returns the array representation of the view.
*
* @return array<T> The array representation of the view.
*/
public function toArray(): array;

/**
* @param callable(T): bool $predicate
* @return ArrayViewInterface<T>
* Filters the elements in the view based on a predicate function.
*
* @param callable(T): bool $predicate Function that returns a boolean value for each element.
*
* @return ArrayViewInterface<T> A new view with elements that satisfy the predicate.
*/
public function filter(callable $predicate): ArrayViewInterface;

/**
* @param callable(T): bool $predicate
* @return MaskSelectorInterface
* Checks if all elements in the view satisfy a given predicate function.
*
* @param callable(T): bool $predicate Function that returns a boolean value for each element.
*
* @return MaskSelectorInterface Boolean mask for selecting elements that satisfy the predicate.
*/
public function is(callable $predicate): MaskSelectorInterface;

/**
* @param ArraySelectorInterface|string $selector
* @param bool|null $readonly
* @return ArrayViewInterface<T>
* Returns a subview of this view based on a selector or string slice.
*
* @param ArraySelectorInterface|string $selector The selector or string to filter the subview.
* @param bool|null $readonly Flag indicating if the subview should be read-only.
*
* @return ArrayViewInterface<T> A new view representing the subview of this view.
*/
public function subview($selector, bool $readonly = null): ArrayViewInterface;

/**
* @param callable(T, int): T $mapper
* Applies a transformation function to each element in the view.
*
* @param callable(T, int): T $mapper Function to transform each element.
*
* @return ArrayViewInterface<T>
* @return ArrayViewInterface<T> this view.
*/
public function apply(callable $mapper): self;

/**
* @template U
* Applies a transformation function using another array or view as rhs values for a binary operation.
*
* @template U The type rhs of a binary operation.
*
* @param array<U>|ArrayViewInterface<U> $data
* @param callable(T, U, int): T $mapper
* @param array<U>|ArrayViewInterface<U> $data The rhs values for a binary operation.
* @param callable(T, U, int): T $mapper Function to transform each pair of elements.
*
* @return ArrayViewInterface<T>
* @return ArrayViewInterface<T> this view.
*/
public function applyWith($data, callable $mapper): self;

/**
* @param array<T>|ArrayViewInterface<T>|T $newValues
* Sets new values for the elements in the view.
*
* @return ArrayViewInterface<T>
* @param array<T>|ArrayViewInterface<T>|T $newValues The new values to set.
*
* @return ArrayViewInterface<T> this view.
*/
public function set($newValues): self;

/**
* Return true if view is readonly, otherwise false.
*
* @return bool
*/
public function isReadonly(): bool;

/**
* Return size of the view.
*
* @return int
*/
public function count(): int;
Expand All @@ -87,13 +128,17 @@ public function count(): int;
* @param numeric|string|ArraySelectorInterface $offset
*
* @return bool
*
* {@inheritDoc}
*/
public function offsetExists($offset): bool;

/**
* @param numeric|string|ArraySelectorInterface $offset
*
* @return T|array<T>
*
* {@inheritDoc}
*/
#[\ReturnTypeWillChange]
public function offsetGet($offset);
Expand All @@ -103,17 +148,23 @@ public function offsetGet($offset);
* @param T|array<T>|ArrayViewInterface<T> $value
*
* @return void
*
* {@inheritDoc}
*/
public function offsetSet($offset, $value): void;

/**
* @param numeric|string|ArraySelectorInterface $offset
*
* @return void
*
* {@inheritDoc}
*/
public function offsetUnset($offset): void;

/**
* Return iterator to iterate the view elements.
*
* @return \Generator<int, T>
*/
public function getIterator(): \Generator;
Expand Down
2 changes: 2 additions & 0 deletions src/Selectors/IndexListSelector.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Smoren\ArrayView\Selectors;

use Smoren\ArrayView\Interfaces\ArrayViewInterface;
Expand Down
2 changes: 2 additions & 0 deletions src/Selectors/MaskSelector.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Smoren\ArrayView\Selectors;

use Smoren\ArrayView\Interfaces\ArrayViewInterface;
Expand Down
2 changes: 2 additions & 0 deletions src/Selectors/SliceSelector.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Smoren\ArrayView\Selectors;

use Smoren\ArrayView\Interfaces\ArraySelectorInterface;
Expand Down
2 changes: 2 additions & 0 deletions src/Structs/NormalizedSlice.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Smoren\ArrayView\Structs;

use Smoren\ArrayView\Util;
Expand Down
2 changes: 2 additions & 0 deletions src/Structs/Slice.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Smoren\ArrayView\Structs;

use Smoren\ArrayView\Exceptions\IndexError;
Expand Down
2 changes: 2 additions & 0 deletions src/Traits/ArrayViewAccessTrait.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Smoren\ArrayView\Traits;

use Smoren\ArrayView\Exceptions\IndexError;
Expand Down
2 changes: 2 additions & 0 deletions src/Util.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Smoren\ArrayView;

use Smoren\ArrayView\Exceptions\IndexError;
Expand Down
2 changes: 2 additions & 0 deletions src/Views/ArrayIndexListView.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Smoren\ArrayView\Views;

use Smoren\ArrayView\Exceptions\ReadonlyError;
Expand Down
2 changes: 2 additions & 0 deletions src/Views/ArrayMaskView.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Smoren\ArrayView\Views;

use Smoren\ArrayView\Exceptions\SizeError;
Expand Down
2 changes: 2 additions & 0 deletions src/Views/ArraySliceView.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Smoren\ArrayView\Views;

use Smoren\ArrayView\Interfaces\ArrayViewInterface;
Expand Down

0 comments on commit 8b4f09d

Please sign in to comment.