Skip to content

Commit

Permalink
Merge pull request #145 from acelaya-forks/feature/input-factory
Browse files Browse the repository at this point in the history
Convert InputFactoryTrait into a class with static factory methods
  • Loading branch information
acelaya authored Feb 21, 2024
2 parents 257eccf + 8d1070e commit 3e5bf59
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 30 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com), and this
### Changed
* Update dependencies
* Update to PHPUnit 11
* Inputs created with `InputFactory` are now not required by default. Also, the `required` param is now always the last one.

### Deprecated
* *Nothing*
Expand All @@ -25,6 +26,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com), and this
* Remove support for openswoole.
* Remove support for Doctrine ORM 2.x
* Remove dependency on injection and mutation tests.
* Remove `InputFactoryTrait` in favor of `InputFactory` class with static factory methods.

### Fixed
* *Nothing*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Shlinkio\Shlink\Common\Validation;

use DateTime;
use DateTimeInterface;
use Laminas\Filter;
use Laminas\InputFilter\ArrayInput;
use Laminas\InputFilter\Input;
Expand All @@ -13,9 +13,9 @@
use function array_map;
use function is_numeric;

trait InputFactoryTrait
final class InputFactory
{
private function createInput(string $name, bool $required = true): Input
public static function basic(string $name, bool $required = false): Input
{
$input = new Input($name);
$input->setRequired($required)
Expand All @@ -24,9 +24,9 @@ private function createInput(string $name, bool $required = true): Input
return $input;
}

private function createBooleanInput(string $name, bool $required = true): Input
public static function boolean(string $name, bool $required = false): Input
{
$input = $this->createInput($name, $required);
$input = self::basic($name, $required);
$input->getFilterChain()->attach(new Filter\Boolean([
'type' => Filter\Boolean::TYPE_PHP | Filter\Boolean::TYPE_FALSE_STRING,
]));
Expand All @@ -41,29 +41,29 @@ private function createBooleanInput(string $name, bool $required = true): Input
return $input;
}

private function createDateInput(
public static function date(
string $name,
bool $required = true,
array $formats = [DateTime::ATOM, 'Y-m-d'],
array $formats = [DateTimeInterface::ATOM, 'Y-m-d'],
bool $required = false,
): Input {
$input = $this->createInput($name, $required);
$input = self::basic($name, $required);
$input->getValidatorChain()->attach(new ExcludingValidatorChain(...array_map(
fn (string $format) => new Validator\Date(['format' => $format]),
$formats,
)));
return $input;
}

private function createNumericInput(string $name, bool $required = true, ?int $min = 1): Input
public static function numeric(string $name, ?int $min = 1, bool $required = false): Input
{
$input = $this->createInput($name, $required);
$input = self::basic($name, $required);
$input->getValidatorChain()->attach(new Validator\Callback(fn ($value) => is_numeric($value)))
->attach(new Validator\GreaterThan(['min' => $min, 'inclusive' => true]));

return $input;
}

private function createArrayInput(string $name, bool $required = true): ArrayInput
public static function array(string $name, bool $required = false): ArrayInput
{
$input = new ArrayInput($name);
$input->setRequired($required)
Expand All @@ -72,9 +72,9 @@ private function createArrayInput(string $name, bool $required = true): ArrayInp
return $input;
}

private function createTagsInput(string $name, bool $required = true): ArrayInput
public static function tags(string $name, bool $required = false): ArrayInput
{
$tags = $this->createArrayInput($name, $required);
$tags = self::array($name, $required);
$tags->getFilterChain()->attach(new Filter\StringToLower())
->attach(new Filter\PregReplace(['pattern' => '/ /', 'replacement' => '-']));

Expand All @@ -84,9 +84,9 @@ private function createTagsInput(string $name, bool $required = true): ArrayInpu
/**
* @param string[] $validFields
*/
private function createOrderByInput(string $name, array $validFields, bool $required = false): Input
public static function orderBy(string $name, array $validFields, bool $required = false): Input
{
$input = $this->createInput($name, $required);
$input = self::basic($name, $required);
$input->getFilterChain()->attach(new OrderByFilter());
$input->getValidatorChain()->attach(new OrderByValidator($validFields));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use PHPUnit\Framework\TestCase;
use ReflectionObject;
use Shlinkio\Shlink\Common\Validation\ExcludingValidatorChain;
use Shlinkio\Shlink\Common\Validation\InputFactoryTrait;
use Shlinkio\Shlink\Common\Validation\InputFactory;
use Shlinkio\Shlink\Common\Validation\OrderByFilter;
use Shlinkio\Shlink\Common\Validation\OrderByValidator;

Expand All @@ -21,14 +21,12 @@
use function gettype;
use function is_object;

class InputFactoryTraitTest extends TestCase
class InputFactoryTest extends TestCase
{
use InputFactoryTrait;

#[Test, DataProvider('provideInputArgs')]
public function basicInputIsCreatedWithDefaultFilters(array $args, bool $required): void
{
$input = $this->createInput(...$args);
$input = InputFactory::basic(...$args);
$filters = $this->getFiltersFromInput($input);

self::assertEquals($required, $input->isRequired());
Expand All @@ -40,7 +38,7 @@ public function basicInputIsCreatedWithDefaultFilters(array $args, bool $require
#[Test, DataProvider('provideInputArgs')]
public function arrayInputIsCreatedWithDefaultFilters(array $args, bool $required): void
{
$input = $this->createArrayInput(...$args);
$input = InputFactory::array(...$args);
$filters = $this->getFiltersFromInput($input);

self::assertEquals($required, $input->isRequired());
Expand All @@ -52,7 +50,7 @@ public function arrayInputIsCreatedWithDefaultFilters(array $args, bool $require
#[Test, DataProvider('provideInputArgs')]
public function booleanInputIsCreatedAsExpected(array $args, bool $required): void
{
$input = $this->createBooleanInput(...$args);
$input = InputFactory::boolean(...$args);
$filters = $this->getFiltersFromInput($input);
$validators = $input->getValidatorChain()->getValidators();

Expand Down Expand Up @@ -82,7 +80,7 @@ public function booleanInputIsCreatedAsExpected(array $args, bool $required): vo
#[Test, DataProvider('provideInputArgs')]
public function dateInputIsCreatedAsExpected(array $args, bool $required): void
{
$input = $this->createDateInput(...$args);
$input = InputFactory::date(...$args);
$filters = $this->getFiltersFromInput($input);
$validators = $input->getValidatorChain()->getValidators();

Expand All @@ -108,7 +106,7 @@ public function dateInputIsCreatedAsExpected(array $args, bool $required): void
#[Test, DataProvider('provideInputArgs')]
public function numericInputIsCreatedAsExpected(array $args, bool $required): void
{
$input = $this->createNumericInput(...$args);
$input = InputFactory::numeric(...$args);
$filters = $this->getFiltersFromInput($input);
$validators = $input->getValidatorChain()->getValidators();

Expand All @@ -124,7 +122,7 @@ public function numericInputIsCreatedAsExpected(array $args, bool $required): vo
#[Test, DataProvider('provideInputArgs')]
public function tagsInputIsCreatedAsExpected(array $args, bool $required): void
{
$input = $this->createTagsInput(...$args);
$input = InputFactory::tags(...$args);
$filters = $this->getFiltersFromInput($input);
$validators = $input->getValidatorChain()->getValidators();

Expand All @@ -139,15 +137,15 @@ public function tagsInputIsCreatedAsExpected(array $args, bool $required): void

public static function provideInputArgs(): iterable
{
yield [['foo', true], true];
yield [['foo', false], false];
yield [['foo'], true];
yield [['foo', 'required' => true], true];
yield [['foo', 'required' => false], false];
yield [['foo'], false];
}

#[Test]
public function orderByInputIsCreatedAsExpected(): void
{
$input = $this->createOrderByInput('orderBy', ['foo', 'bar', 'somethingElse']);
$input = InputFactory::orderBy('orderBy', ['foo', 'bar', 'somethingElse']);
$filters = $this->getFiltersFromInput($input);
$validators = $input->getValidatorChain()->getValidators();

Expand Down

0 comments on commit 3e5bf59

Please sign in to comment.