Skip to content

Commit

Permalink
Add ArrayValue::toNumbersArray
Browse files Browse the repository at this point in the history
  • Loading branch information
dazet committed May 23, 2021
1 parent 45e946b commit c996770
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 9 deletions.
11 changes: 11 additions & 0 deletions spec/PlainArraySpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use GW\Value\ArrayValue;
use GW\Value\Filters;
use GW\Value\Numberable\JustInteger;
use GW\Value\NumbersArray;
use GW\Value\PlainArray;
use GW\Value\Sorts;
use GW\Value\StringsArray;
Expand Down Expand Up @@ -673,6 +675,15 @@ function it_can_be_converted_to_StringsArray()
$stringsArray->toNativeStrings()->shouldBeLike(['one', 'two', 'three']);
}

function it_can_be_converted_to_NumbersArray()
{
$this->beConstructedWith(['1', 2.1, new JustInteger(3)]);

$numbersArray = $this->toNumbersArray();
$numbersArray->shouldBeAnInstanceOf(NumbersArray::class);
$numbersArray->toNativeNumbers()->shouldBeLike([1, 2.1, 3]);
}

function it_can_tell_if_has_element_or_not()
{
$this->beConstructedWith(['one', '2', 'three']);
Expand Down
9 changes: 9 additions & 0 deletions spec/PlainStringsArraySpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,15 @@ function it_returns_PlainArray_containing_regex_matches()
$matches->shouldBeLike(new PlainArray([['Lorem', 'Lorem'], ['dolor', 'dolor']]));
}

function it_can_be_converted_to_NumbersArray()
{
$this->beConstructedWithStrings('1', '2.1', '3.0');

$this->toNumbersArray()
->toNativeNumbers()
->shouldBeLike([1, 2.1, 3.0]);
}

private function beConstructedWithStrings(string ...$strings): void
{
$this->beConstructedWith(Wrap::array($strings));
Expand Down
2 changes: 2 additions & 0 deletions src/ArrayValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,4 +191,6 @@ public function notEmpty(): ArrayValue;
public function toAssocValue(): AssocValue;

public function toStringsArray(): StringsArray;

public function toNumbersArray(): NumbersArray;
}
14 changes: 7 additions & 7 deletions src/NumberValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,42 @@ interface NumberValue extends Value, Numberable
// comparators

/**
* @param int|float|Numberable $other
* @param int|float|numeric-string|Numberable $other
* @return int {-1, 0, 1}
*/
public function compare($other): int;

/**
* @param int|float|Numberable $other
* @param int|float|numeric-string|Numberable $other
*/
public function equals($other): bool;

// basic math

/**
* @param int|float|Numberable $other
* @param int|float|numeric-string|Numberable $other
*/
public function add($other): NumberValue;

/**
* @param int|float|Numberable $other
* @param int|float|numeric-string|Numberable $other
*/
public function subtract($other): NumberValue;

/**
* @param int|float|Numberable $other
* @param int|float|numeric-string|Numberable $other
*/
public function multiply($other): NumberValue;

/**
* @param int|float|Numberable $other
* @param int|float|numeric-string|Numberable $other
*/
public function divide($other): NumberValue;

public function abs(): NumberValue;

/**
* @param int|float|Numberable $divider
* @param int|float|numeric-string|Numberable $divider
*/
public function modulo($divider): NumberValue;

Expand Down
3 changes: 3 additions & 0 deletions src/NumbersArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public function unique(?callable $comparator = null): NumbersArray;
/** @return NumberValue[] */
public function toArray(): array;

/** @return array<int|float> */
public function toNativeNumbers(): array;

/**
* @param callable(NumberValue):bool $filter
*/
Expand Down
6 changes: 6 additions & 0 deletions src/PlainArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use GW\Value\Arrayable\Splice;
use GW\Value\Arrayable\UniqueByComparator;
use GW\Value\Arrayable\UniqueByString;
use GW\Value\Numberable\ToNumberValue;
use function array_map;
use function array_reverse;
use function count;
Expand Down Expand Up @@ -449,4 +450,9 @@ public function toStringsArray(): StringsArray
{
return Wrap::stringsArray($this->items->toArray());
}

public function toNumbersArray(): NumbersArray
{
return new PlainNumbersArray($this->map(new ToNumberValue()));
}
}
7 changes: 6 additions & 1 deletion src/PlainNumbersArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public function unshift($value): NumbersArray
}

/**
* @param NumberValue $value
* @param NumberValue|null $value
*/
public function shift(&$value = null): NumbersArray
{
Expand Down Expand Up @@ -313,6 +313,11 @@ public function toStringsArray(): StringsArray
return $this->numbers->toStringsArray();
}

public function toNumbersArray(): NumbersArray
{
return $this;
}

public function first(): ?NumberValue
{
return $this->numbers->first();
Expand Down
9 changes: 8 additions & 1 deletion src/PlainStringsArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace GW\Value;

use GW\Value\Numberable\ToNumberValue;
use GW\Value\Stringable\ToNativeString;
use GW\Value\Stringable\ToStringValue;
use Traversable;
use function in_array;
Expand Down Expand Up @@ -214,7 +216,7 @@ public function toArray(): array
public function toNativeStrings(): array
{
return $this->strings
->map(fn(StringValue $item): string => $item->toString())
->map(new ToNativeString())
->toArray();
}

Expand Down Expand Up @@ -615,4 +617,9 @@ public function toStringsArray(): self
{
return $this;
}

public function toNumbersArray(): NumbersArray
{
return new PlainNumbersArray($this->strings->map(new ToNativeString())->map(new ToNumberValue()));
}
}
13 changes: 13 additions & 0 deletions src/Stringable/ToNativeString.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php declare(strict_types=1);

namespace GW\Value\Stringable;

use GW\Value\StringValue;

final class ToNativeString
{
public function __invoke(StringValue $stringValue): string
{
return $stringValue->toString();
}
}

0 comments on commit c996770

Please sign in to comment.