Skip to content

Commit

Permalink
add floats to array accessor
Browse files Browse the repository at this point in the history
  • Loading branch information
mlebkowski committed Dec 6, 2024
1 parent 025a906 commit e908c2c
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 9 deletions.
41 changes: 41 additions & 0 deletions src/Accessor/ArrayAccessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,22 @@ function ($value, $key) {
);
}

/**
* @return float[]
*/
public function allFloat(): array {
return map(
$this->payload,
function ($value, $key) {
try {
return StringValue::of($value)->toFloat();
} catch (StringValueException $e) {
$this->throw("Value at $key is not a float");
}
},
);
}

/**
* @return bool[]
*/
Expand Down Expand Up @@ -158,6 +174,31 @@ public function maybeInt(string $name): ?int {
}
}

public function float(string $name, float $default = 0.0): float {
return $this->maybeFloat($name) ?? $default;
}

public function requireFloat(string $name): float {
$raw = $this->maybeFloat($name);
if (null === $raw) {
$this->throw("Required field named $name not found");
}
return $raw;
}

public function maybeFloat(string $name): ?float {
$raw = $this->payload[$name] ?? null;
if (null === $raw) {
return null;
}

try {
return StringValue::of($raw)->toFloat();
} catch (StringValueException $e) {
$this->throw("Required field named $name is not a float");
}
}

public function bool(string $name, bool $default = false): bool {
return $this->maybeBool($name) ?? $default;
}
Expand Down
12 changes: 12 additions & 0 deletions src/Accessor/StringValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@ public function toInt(): int {
return (int) $this->value;
}

public function toFloat(): float {
if (is_int($this->raw)) {
return $this->raw;
}

if (false === is_numeric($this->value)) {
throw new StringValueException('Can’t convert non-numeric value to a float');
}

return (float) $this->value;
}

/**
* @throws StringValueException
*/
Expand Down
39 changes: 30 additions & 9 deletions tests/Accessor/ArrayAccessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function testStringOfBoolean(): void {
}

public function testStringBailsOnArray(): void {
self::expectException(ArrayAccessorException::class);
$this->expectException(ArrayAccessorException::class);
ArrayAccessor::of(['foo' => ['a', 'b']])->string('foo');
}
// endregion
Expand Down Expand Up @@ -76,19 +76,19 @@ public function testMaybeStringOfBoolean(): void {
}

public function testMaybeStringBailsOnArray(): void {
self::expectException(ArrayAccessorException::class);
$this->expectException(ArrayAccessorException::class);
ArrayAccessor::of(['foo' => ['a', 'b']])->maybeString('foo');
}
// endregion

// region requireString
public function testRequireStringBailsNoKey(): void {
self::expectException(ArrayAccessorException::class);
$this->expectException(ArrayAccessorException::class);
ArrayAccessor::of([])->requireString('foo');
}

public function testRequireStringBailsOnNull(): void {
self::expectException(ArrayAccessorException::class);
$this->expectException(ArrayAccessorException::class);
ArrayAccessor::of(['foo' => null])->requireString('foo');
}

Expand All @@ -107,7 +107,7 @@ public function testRequireStringOfBoolean(): void {
}

public function testRequireStringBailsOnArray(): void {
self::expectException(ArrayAccessorException::class);
$this->expectException(ArrayAccessorException::class);
ArrayAccessor::of(['foo' => ['a', 'b']])->requireString('foo');
}
// endregion
Expand All @@ -128,11 +128,32 @@ public function testIntCastsBoolean(): void {
}

public function testIntBailsOnInvalidString(): void {
self::expectException(ArrayAccessorException::class);
$this->expectException(ArrayAccessorException::class);
ArrayAccessor::of(['foo' => "bar"])->int("foo");
}
// endregion

// region float
public function testFloatCastsString(): void {
self::assertSame(
1.0,
ArrayAccessor::of(['foo' => "1"])->float("foo"),
);
}

public function testFloatCastsStringWithDotForDecimalSeparator(): void {
self::assertSame(
1.123,
ArrayAccessor::of(['foo' => "1.123"])->float("foo"),
);
}

public function testFloatBailsOnInvalidString(): void {
$this->expectException(ArrayAccessorException::class);
ArrayAccessor::of(['foo' => "bar"])->float("foo");
}
// endregion

// region bool
public function testBoolCastsStringTrue(): void {
self::assertTrue(
Expand All @@ -159,12 +180,12 @@ public function testBoolCastsIntFalse(): void {
}

public function testBoolBailsOnOtherNumbers(): void {
self::expectException(ArrayAccessorException::class);
$this->expectException(ArrayAccessorException::class);
ArrayAccessor::of(['foo' => 10])->bool("foo");
}

public function testBoolBailsOnInvalidString(): void {
self::expectException(ArrayAccessorException::class);
$this->expectException(ArrayAccessorException::class);
ArrayAccessor::of(['foo' => "bar"])->bool("foo");
}
// endregion
Expand Down Expand Up @@ -198,7 +219,7 @@ public function testMaybeAt(): void {
}

public function testMaybeAtBailsOnStringKey(): void {
self::expectException(ArrayAccessorException::class);
$this->expectException(ArrayAccessorException::class);
ArrayAccessor::of(['weekdays' => 'string'])->maybeAt('weekdays');
}

Expand Down

0 comments on commit e908c2c

Please sign in to comment.