Skip to content

Commit

Permalink
Split StringsArray and ArrayValue
Browse files Browse the repository at this point in the history
  • Loading branch information
bronek89 committed Nov 1, 2021
1 parent 2ad4775 commit 3b37502
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 66 deletions.
1 change: 1 addition & 0 deletions src/ArrayValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
/**
* @template TValue
* @extends Collection<TValue>
* @extends Stack<TValue>
* @extends IteratorAggregate<int, TValue>
* @extends ArrayAccess<int, TValue>
*/
Expand Down
8 changes: 7 additions & 1 deletion src/AssocArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,18 +280,24 @@ public function hasElement($element): bool
return in_array($element, $this->items->toAssocArray(), true);
}

/**
* @param callable(TValue $value):bool $filter
*/
public function any(callable $filter): bool
{
return $this->values()->any($filter);
}

/**
* @param callable(TValue $value):bool $filter
*/
public function every(callable $filter): bool
{
return $this->values()->every($filter);
}

/**
* @return TValue[]
* @return array<int, TValue>
*/
public function toArray(): array
{
Expand Down
6 changes: 3 additions & 3 deletions src/IterableValueStack.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ final class IterableValueStack
/** @phpstan-var IterableValueIterator<TKey, TValue> */
private IterableValueIterator $iterable;

/** @phpstan-var array<int, (callable(iterable<TKey, TValue> $value): iterable<mixed, mixed>)> */
/** @phpstan-var array<int, (callable(iterable<TKey,TValue>): iterable<mixed,mixed>)> */
private array $modifiers = [];

/**
Expand All @@ -26,7 +26,7 @@ public function __construct(IterableValueIterator $iterable)
/**
* @template TNewKey
* @template TNewValue
* @param callable(iterable<TKey, TValue> $value): iterable<TNewKey, TNewValue> $modifier
* @param callable(iterable<TKey,TValue>):iterable<TNewKey,TNewValue> $modifier
* @phpstan-return IterableValueStack<TNewKey, TNewValue>
*/
public function push(callable $modifier): self
Expand All @@ -38,7 +38,7 @@ public function push(callable $modifier): self
}

/**
* @phpstan-param array<int, (callable(iterable<TKey, TValue> $value): iterable<TKey, TValue>)> $modifiers
* @phpstan-param array<int, (callable(iterable<TKey,TValue>): iterable<TKey,TValue>)> $modifiers
* @phpstan-param iterable<TKey, TValue> $iterable
* @phpstan-return iterable<TKey, TValue>
*/
Expand Down
6 changes: 3 additions & 3 deletions src/PlainArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public function unique(?callable $comparator = null): PlainArray
}

/**
* @phpstan-param PlainArray<TValue> $other
* @phpstan-param ArrayValue<TValue> $other
* @param (callable(TValue $valueA, TValue $valueB):int)|null $comparator
* @phpstan-return PlainArray<TValue>
*/
Expand All @@ -197,7 +197,7 @@ public function diff(ArrayValue $other, ?callable $comparator = null): PlainArra
}

/**
* @phpstan-param PlainArray<TValue> $other
* @phpstan-param ArrayValue<TValue> $other
* @param (callable(TValue $valueA, TValue $valueB):int)|null $comparator
* @phpstan-return PlainArray<TValue>
*/
Expand Down Expand Up @@ -368,7 +368,7 @@ public function count(): int
}

/**
* @phpstan-return TValue[]
* @phpstan-return array<int, TValue>
*/
public function toArray(): array
{
Expand Down
2 changes: 1 addition & 1 deletion src/PlainString.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ public function endsWith($pattern): bool

/**
* @param string|StringValue $pattern
* @return ArrayValue<string[][]>
* @return ArrayValue<array<int, string>>
*/
public function matchAllPatterns($pattern): ArrayValue
{
Expand Down
46 changes: 19 additions & 27 deletions src/PlainStringsArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use GW\Value\Stringable\ToStringValue;
use Traversable;
use InvalidArgumentException;
use Traversable;
use function in_array;
use function is_scalar;

Expand All @@ -29,43 +30,37 @@ public static function fromArray(array $strings): self
return new self(Wrap::array($strings));
}

/**
* @param StringsArray $other
*/
public function join(ArrayValue $other): PlainStringsArray
public function join(StringsArray $other): PlainStringsArray
{
return new self($this->strings->join($this->mapStringValues($other)));
return new self($this->strings->join($other->toArrayValue()));
}

public function slice(int $offset, int $length): PlainStringsArray
{
return new self($this->strings->slice($offset, $length));
}

/**
* @param StringsArray $replacement
*/
public function splice(int $offset, int $length, ?ArrayValue $replacement = null): PlainStringsArray
public function splice(int $offset, int $length, ?StringsArray $replacement = null): PlainStringsArray
{
return new self($this->strings->splice($offset, $length, $replacement));
return new self(
$this->strings->splice($offset, $length, $replacement === null ? null : $replacement->toArrayValue())
);
}

/**
* @param StringsArray $other
* @param (callable(StringValue $valueA, StringValue $valueB):int)|null $comparator
*/
public function diff(ArrayValue $other, ?callable $comparator = null): PlainStringsArray
public function diff(StringsArray $other, ?callable $comparator = null): PlainStringsArray
{
return new self($this->strings->diff($this->mapStringValues($other), $comparator));
return new self($this->strings->diff($other->toArrayValue(), $comparator));
}

/**
* @param StringsArray $other
* @param callable(StringValue $valueA, StringValue $valueB):int|null $comparator
*/
public function intersect(ArrayValue $other, ?callable $comparator = null): PlainStringsArray
public function intersect(StringsArray $other, ?callable $comparator = null): PlainStringsArray
{
return new self($this->strings->intersect($this->mapStringValues($other), $comparator));
return new self($this->strings->intersect($other->toArrayValue(), $comparator));
}

/**
Expand Down Expand Up @@ -96,25 +91,22 @@ public function flatMap(callable $transformer): PlainStringsArray
}

/**
* @param callable(StringValue $value):string $reducer
* @phpstan-return AssocValue<string, ArrayValue<StringValue>>
* @phpstan-ignore-next-line shrug
* @template TNewKey
* @param callable(StringValue $value):TNewKey $reducer
* @phpstan-return AssocValue<TNewKey, StringsArray>
*/
public function groupBy(callable $reducer): AssocValue
{
// @phpstan-ignore-next-line shrug
return $this->strings
->groupBy($reducer)
// @phpstan-ignore-next-line shrug
->map(
/** @return ArrayValue<StringValue> */
static fn(ArrayValue $value): ArrayValue => $value->toStringsArray()
/** @param ArrayValue<StringValue> $value */
static fn(ArrayValue $value): StringsArray => $value->toStringsArray()
);
}

/**
* @return ArrayValue<array<int, StringValue>>
* @phpstan-ignore-next-line shrug
*/
public function chunk(int $size): ArrayValue
{
Expand Down Expand Up @@ -266,11 +258,11 @@ public function offsetGet($offset): StringValue

/**
* @param int $offset
* @param StringValue $value
* @param StringValue|string $value
*/
public function offsetSet($offset, $value): void
{
$this->strings->offsetSet($offset, $value);
$this->strings->offsetSet($offset, Wrap::string($value));
}

/**
Expand Down Expand Up @@ -499,7 +491,7 @@ public function positionLast($needle): ?int

/**
* @param string|StringValue $pattern
* @return ArrayValue<string[][]>
* @return ArrayValue<array<int, string>>
*/
public function matchAllPatterns($pattern): ArrayValue
{
Expand Down
19 changes: 14 additions & 5 deletions src/Safe.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,27 @@ public static function filter(callable $callable): callable
return fn(...$args): bool => self::guard($callable(...$args), 'is_bool', 'Filter must return boolean value.');
}

/**
* @param callable(mixed ...$args):iterable<mixed> $callable
*/
public static function iterableTransformer(callable $callable): callable
{
return fn(...$args): iterable => self::guard(
$callable(...$args),
'is_iterable',
'Iterable transformer must return iterable value.'
);
return
/**
* @param mixed ...$args
* @return iterable<mixed>
*/
static fn(...$args): iterable => self::guard(
$callable(...$args),
'is_iterable',
'Iterable transformer must return iterable value.'
);
}

/**
* @template TValue
* @param TValue $value
* @param callable(TValue):bool $assertion
* @return TValue
*/
private static function guard($value, callable $assertion, string $message)
Expand Down
15 changes: 9 additions & 6 deletions src/Stack.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,30 @@

namespace GW\Value;

/**
* @template TValue
*/
interface Stack
{
/**
* @param mixed $value
* @return Stack
* @param TValue $value
* @return Stack<TValue>
*/
public function unshift($value): Stack;

/**
* @return Stack
* @return Stack<TValue>
*/
public function shift(): Stack;

/**
* @param mixed $value
* @return Stack
* @param TValue $value
* @return Stack<TValue>
*/
public function push($value): Stack;

/**
* @return Stack
* @return Stack<TValue>
*/
public function pop(): Stack;
}
2 changes: 1 addition & 1 deletion src/StringValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public function positionLast($needle): ?int;

/**
* @param string|StringValue $pattern
* @return ArrayValue<string[][]>
* @return ArrayValue<array<int, string>>
*/
public function matchAllPatterns($pattern): ArrayValue;

Expand Down
38 changes: 19 additions & 19 deletions src/StringsArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

namespace GW\Value;

use ArrayAccess;
use BadMethodCallException;
use IteratorAggregate;

/**
* @extends ArrayValue<StringValue>
* @extends IteratorAggregate<int, StringValue>
* @extends ArrayAccess<int, StringValue>
*/
interface StringsArray extends ArrayValue, StringValue
interface StringsArray extends Value, IteratorAggregate, ArrayAccess, StringValue
{
// Array Value

/**
* @param callable(StringValue $value): void $callback
*/
Expand Down Expand Up @@ -51,7 +52,7 @@ public function flatMap(callable $transformer): StringsArray;
/**
* @template TNewKey
* @param callable(StringValue $value):TNewKey $reducer
* @phpstan-return AssocValue<TNewKey, ArrayValue<StringValue>>
* @phpstan-return AssocValue<TNewKey, StringsArray>
*/
public function groupBy(callable $reducer): AssocValue;

Expand All @@ -70,7 +71,7 @@ public function reverse(): StringsArray;
public function unshift($value): StringsArray;

/**
* @param StringValue|string $value
* @param StringValue|null $value
*/
public function shift(&$value = null): StringsArray;

Expand All @@ -80,7 +81,7 @@ public function shift(&$value = null): StringsArray;
public function push($value): StringsArray;

/**
* @param mixed $value
* @param StringValue|null $value
*/
public function pop(&$value = null): StringsArray;

Expand Down Expand Up @@ -109,29 +110,21 @@ public function offsetSet($offset, $value): void;
*/
public function offsetUnset($offset): void;

/**
* @param StringsArray $other
*/
public function join(ArrayValue $other): StringsArray;
public function join(StringsArray $other): StringsArray;

public function slice(int $offset, int $length): StringsArray;

/**
* @param StringsArray $replacement
*/
public function splice(int $offset, int $length, ?ArrayValue $replacement = null): StringsArray;
public function splice(int $offset, int $length, ?StringsArray $replacement = null): StringsArray;

/**
* @param StringsArray $other
* @param (callable(StringValue, StringValue):int)|null $comparator
*/
public function diff(ArrayValue $other, ?callable $comparator = null): StringsArray;
public function diff(StringsArray $other, ?callable $comparator = null): StringsArray;

/**
* @param StringsArray $other
* @param (callable(StringValue $valueA, StringValue $valueB):int)|null $comparator
*/
public function intersect(ArrayValue $other, ?callable $comparator = null): StringsArray;
public function intersect(StringsArray $other, ?callable $comparator = null): StringsArray;

/**
* @template TNewValue
Expand Down Expand Up @@ -273,4 +266,11 @@ public function toArrayValue(): ArrayValue;
* @return AssocValue<int, StringValue>
*/
public function toAssocValue(): AssocValue;

/**
* @phpstan-return ArrayValue<array<int, StringValue>>
*/
public function chunk(int $size): ArrayValue;

public function toStringsArray(): StringsArray;
}

0 comments on commit 3b37502

Please sign in to comment.