Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
* text=auto

**/*Test.php export-ignore
**/*TestCase.php export-ignore
**/Fake*.php export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
Expand Down
9 changes: 1 addition & 8 deletions .styleci.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
preset: recommended

preset: psr12
enabled:
- not_operator_with_successor_space
- phpdoc_no_empty_return
- unalign_double_arrow

disabled:
- align_double_arrow
- phpdoc_align
- phpdoc_separation
- simplified_null_return
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"php": "^7.4"
},
"require-dev": {
"phpunit/phpunit": "^8"
"phpunit/phpunit": "^8",
"sempro/phpunit-pretty-print": "^1.2"
},
"autoload": {
"psr-4": {
Expand Down
6 changes: 5 additions & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php" colors="true">
<phpunit
bootstrap="vendor/autoload.php"
colors="true"
printerClass="Sempro\PHPUnitPrettyPrinter\PrettyPrinter"
>
<testsuites>
<testsuite name="Distil">
<directory suffix="Test.php">src/</directory>
Expand Down
1 change: 0 additions & 1 deletion src/CriteriaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Distil\Exceptions\CannotAddCriterion;
use Distil\Exceptions\CannotGetCriterion;
use Distil\FakeCriterion;
use PHPUnit\Framework\TestCase;

use function array_values;
Expand Down
37 changes: 37 additions & 0 deletions src/Exceptions/InvalidKeyword.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Distil\Exceptions;

use Distil\Values\BooleanKeyword;
use RuntimeException;

use function array_keys;
use function implode;

final class InvalidKeyword extends RuntimeException
{
public static function cannotBeCastedToBoolean(string $keyword): self
{
return new self(
"[$keyword] is not a valid boolean keyword. Valid keywords are: " .
implode(', ', array_keys(BooleanKeyword::CASTED_VALUES))
);
}

public static function cannotBeCastedToInteger(string $keyword): self
{
return new self(
"[$keyword] is not a valid integer keyword. It must be an string representation of an integer."
);
}

public static function cannotBeCastedToDateTime(string $keyword): self
{
return new self(
"[$keyword] is not a valid DateTime keyword. It must be a date/time string " .
'(see https://www.php.net/manual/en/datetime.formats.php).'
);
}
}
3 changes: 3 additions & 0 deletions src/FakeCriterion.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace Distil;

/**
* @internal
*/
final class FakeCriterion implements Criterion
{
use ActsAsCriteriaFactory;
Expand Down
32 changes: 6 additions & 26 deletions src/Types/BooleanCriterion.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@

use Distil\ActsAsCriteriaFactory;
use Distil\Criterion;
use Distil\Exceptions\InvalidCriterionValue;
use Distil\Keywords\HasKeywords;
use Distil\Keywords\Keyword;
use Distil\Keywords\Value;
use Distil\Values\BooleanKeyword;
use Distil\Values\ConstructsFromKeyword;

abstract class BooleanCriterion implements Criterion, HasKeywords
abstract class BooleanCriterion implements Criterion
{
use ActsAsCriteriaFactory;
use ConstructsFromKeyword;

public const KEYWORD_TRUE = 'true';
public const KEYWORD_FALSE = 'false';
Expand All @@ -28,13 +27,7 @@ public function __construct(bool $value)
*/
public static function fromString(string $value): self
{
$value = (new Keyword(static::class, $value))->value();

if (! is_bool($value)) {
throw InvalidCriterionValue::expectedBoolean(static::class);
}

return new static($value);
return self::fromKeyword(new BooleanKeyword($value));
}

public function value(): bool
Expand All @@ -49,19 +42,6 @@ public function isTruthy(): bool

public function isFalsy(): bool
{
return ! $this->isTruthy();
}

public static function keywords(): array
{
return [
static::KEYWORD_TRUE => true,
static::KEYWORD_FALSE => false,
];
}

public function __toString(): string
{
return (new Value($this, $this->value))->keyword();
return ! $this->value;
}
}
36 changes: 15 additions & 21 deletions src/Types/DateTimeCriterion.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,41 @@

namespace Distil\Types;

use DateTime;
use DateTimeImmutable;
use DateTimeInterface;
use Distil\ActsAsCriteriaFactory;
use Distil\Criterion;
use Distil\Exceptions\InvalidCriterionValue;
use Distil\Keywords\Keyword;
use Distil\Keywords\Value;
use Distil\Values\ConstructsFromKeyword;
use Distil\Values\DateTimeKeyword;

abstract class DateTimeCriterion implements Criterion
{
use ActsAsCriteriaFactory;
use ConstructsFromKeyword;

private DateTimeInterface $value;
private string $format;

public function __construct(DateTimeInterface $value, string $format = DateTime::ATOM)
public function __construct(DateTimeInterface $value, string $format = DateTimeInterface::ATOM)
{
$this->value = $value;
$this->format = $format;
}

public function __toString(): string
{
if ($this->keyword) {
return (string) $this->keyword;
}

return $this->value->format($this->format);
}

/**
* @return static
*/
public static function fromString(string $value, string $format = DateTime::ATOM): self
public static function fromString(string $value): self
{
$value = (new Keyword(static::class, $value))->value();

if ($value instanceof DateTimeInterface) {
return new static($value, $format);
} elseif (strtotime($value)) {
return new static(new DateTimeImmutable($value), $format);
}

throw InvalidCriterionValue::expectedTimeString(static::class);
return self::fromKeyword(new DateTimeKeyword($value));
}

public function value(): DateTimeInterface
Expand All @@ -49,9 +48,4 @@ public function format(): string
{
return $this->format;
}

public function __toString(): string
{
return (new Value($this, $this->value))->keyword() ?: $this->value->format($this->format);
}
}
16 changes: 4 additions & 12 deletions src/Types/IntegerCriterion.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
use Distil\ActsAsCriteriaFactory;
use Distil\Criterion;
use Distil\Exceptions\InvalidCriterionValue;
use Distil\Keywords\Keyword;
use Distil\Keywords\Value;
use Distil\Values\ConstructsFromKeyword;

use function is_numeric;

abstract class IntegerCriterion implements Criterion
{
use ActsAsCriteriaFactory;
use ConstructsFromKeyword;

private int $value;

Expand All @@ -19,13 +21,8 @@ public function __construct(int $value)
$this->value = $value;
}

/**
* @return static
*/
public static function fromString(string $value): self
{
$value = (new Keyword(static::class, $value))->value();

if (! is_numeric($value)) {
throw InvalidCriterionValue::expectedNumeric(static::class);
}
Expand All @@ -37,9 +34,4 @@ public function value(): int
{
return $this->value;
}

public function __toString(): string
{
return (new Value($this, $this->value))->keyword() ?: (string) $this->value;
}
}
21 changes: 2 additions & 19 deletions src/Types/StringCriterion.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,10 @@

use Distil\ActsAsCriteriaFactory;
use Distil\Criterion;
use Distil\Keywords\Keyword;
use Distil\Keywords\Value;
use Distil\Values\ConstructsFromKeyword;

abstract class StringCriterion implements Criterion
{
use ActsAsCriteriaFactory;

private string $value;

public function __construct(string $value)
{
$this->value = (new Keyword(static::class, $value))->value();
}

public function value(): string
{
return $this->value;
}

public function __toString(): string
{
return (new Value($this, $this->value))->keyword() ?: $this->value;
}
use ConstructsFromKeyword;
}
54 changes: 54 additions & 0 deletions src/Values/BooleanKeyword.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

namespace Distil\Values;

use Distil\Exceptions\InvalidKeyword;

use function array_key_exists;

final class BooleanKeyword implements Keyword
{
use ConstructsNullableKeyword;

public const CASTED_VALUES = [
'true' => true,
'1' => true,
'false' => false,
'0' => false,
];

private string $stringValue;
private bool $castedValue;

public function __construct(string $keyword)
{
$this->guardAgainstInvalidValues($keyword);

$this->stringValue = $keyword;
$this->castedValue = self::CASTED_VALUES[$keyword];
}

private function guardAgainstInvalidValues(string $keyword): void
{
if ($this->isNotAKeyword($keyword)) {
throw InvalidKeyword::cannotBeCastedToBoolean($keyword);
}
}

public function __toString(): string
{
return $this->stringValue;
}

public function castedValue(): bool
{
return $this->castedValue;
}

private function isNotAKeyword(string $keyword): bool
{
return ! array_key_exists($keyword, self::CASTED_VALUES);
}
}
Loading