Skip to content

Commit

Permalink
rangeDescription
Browse files Browse the repository at this point in the history
  • Loading branch information
spawnia committed Sep 5, 2024
1 parent 0eff6c3 commit b246ab6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
16 changes: 11 additions & 5 deletions src/IntRange.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,14 @@ abstract protected function min(): int;
/** The maximum allowed value. */
abstract protected function max(): int;

public ?string $description = 'Checks if the given column is of the format 96-well column';

public function serialize($value)
{
if (is_int($value) && $this->isValueInExpectedRange($value)) {
return $value;
}

$notInRange = Utils::printSafe($value);
throw new \InvalidArgumentException("Value not in range: {$notInRange}.");
throw new \InvalidArgumentException("Value not in range {$this->rangeDescription()}: {$notInRange}.");
}

public function parseValue($value)
Expand All @@ -36,7 +34,7 @@ public function parseValue($value)
}

$notInRange = Utils::printSafe($value);
throw new Error("Value not in range: {$notInRange}.");
throw new Error("Value not in range {$this->rangeDescription()}: {$notInRange}.");
}

public function parseLiteral(Node $valueNode, ?array $variables = null)
Expand All @@ -49,11 +47,19 @@ public function parseLiteral(Node $valueNode, ?array $variables = null)
}

$notInRange = Printer::doPrint($valueNode);
throw new Error("Value not in range: {$notInRange}.", $valueNode);
throw new Error("Value not in range {$this->rangeDescription()}: {$notInRange}.", $valueNode);
}

private function isValueInExpectedRange(int $value): bool
{
return $value <= static::max() && $value >= static::min();
}

private function rangeDescription(): string
{
$min = static::min();
$max = static::max();

return "{$min}-{$max}";
}
}
6 changes: 3 additions & 3 deletions tests/IntRangeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ final class IntRangeTest extends TestCase
public function testSerializeThrowsIfNotAnInt(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Value not in range: "12".');
$this->expectExceptionMessage('Value not in range 1-12: "12".');

(new UpToADozen())->serialize('12');
}

public function testSerializeThrowsIfInvalid(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Value not in range: 13.');
$this->expectExceptionMessage('Value not in range 1-12: 13.');

(new UpToADozen())->serialize(13);
}
Expand All @@ -33,7 +33,7 @@ public function testSerializePassesWhenValid(): void
public function testParseValueThrowsIfInvalid(): void
{
$this->expectException(Error::class);
$this->expectExceptionMessage('Value not in range: 13.');
$this->expectExceptionMessage('Value not in range 1-12: 13.');

(new UpToADozen())->parseValue(13);
}
Expand Down

0 comments on commit b246ab6

Please sign in to comment.