Skip to content
Merged
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
106 changes: 106 additions & 0 deletions src/Filter/Model/FilterData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\AdminBundle\Filter\Model;

/**
* @psalm-immutable
*/
final class FilterData
{
/**
* @var ?int
*/
private $type;

/**
* @var mixed
*/
private $value;

/**
* @var bool
*/
private $hasValue;

private function __construct()
{
$this->hasValue = false;
}

/**
* @psalm-pure
*
* @phpstan-param array{type?: int|numeric-string|null, value?: mixed} $data
*/
public static function fromArray(array $data): self
{
$filterData = new self();

if (isset($data['type'])) {
if (!\is_int($data['type']) && (!\is_string($data['type']) || !is_numeric($data['type']))) {
throw new \InvalidArgumentException(sprintf(
'The "type" parameter MUST be of type "integer" or "null", %s given.',
\is_object($data['type']) ? 'instance of "'.\get_class($data['type']).'"' : '"'.\gettype($data['type']).'"'
));
}

$filterData->type = (int) $data['type'];
}

if (\array_key_exists('value', $data)) {
$filterData->value = $data['value'];
$filterData->hasValue = true;
}

return $filterData;
}

/**
* @return mixed
*/
public function getValue()
{
if (!$this->hasValue) {
throw new \LogicException('The FilterData object does not have a value.');
}

return $this->value;
}

/**
* @param mixed $value
*/
public function changeValue($value): self
{
return self::fromArray([
'type' => $this->getType(),
'value' => $value,
]);
}

public function getType(): ?int
{
return $this->type;
}

public function isType(int $type): bool
{
return $this->type === $type;
}

public function hasValue(): bool
{
return $this->hasValue;
}
}
121 changes: 121 additions & 0 deletions tests/Filter/Model/FilterDataTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\AdminBundle\Tests\Filter\Model;

use PHPUnit\Framework\TestCase;
use Sonata\AdminBundle\Filter\Model\FilterData;

final class FilterDataTest extends TestCase
{
/**
* @dataProvider getInvalidTypes
*
* @param mixed $type
*/
public function testTypeMustBeNumericOrNull($type): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage(sprintf(
'The "type" parameter MUST be of type "integer" or "null", %s given.',
\is_object($type) ? 'instance of "'.\get_class($type).'"' : '"'.\gettype($type).'"'
));

FilterData::fromArray(['type' => $type]);
}

/**
* @return iterable<array<mixed>>
*/
public function getInvalidTypes(): iterable
{
yield ['string'];
yield [new \stdClass()];
yield [[]];
yield [42.0];
}

public function testEmptyArray(): void
{
$filterData = FilterData::fromArray([]);
$this->assertFalse($filterData->hasValue());
$this->assertNull($filterData->getType());
}

public function testHasValue(): void
{
$this->assertFalse(FilterData::fromArray([])->hasValue());
$this->assertTrue(FilterData::fromArray(['value' => ''])->hasValue());
$this->assertTrue(FilterData::fromArray(['value' => null])->hasValue());
}

/**
* @dataProvider getTypes
*
* @param int|string|null $type
*/
public function testGetType(?int $expected, $type): void
{
$this->assertSame($expected, FilterData::fromArray(['type' => $type])->getType());
}

/**
* @return iterable<array<string|int|null>>
*/
public function getTypes(): iterable
{
yield 'nullable' => [null, null];
yield 'int' => [3, 3];
yield 'numeric string' => [3, '3'];
}

/**
* @dataProvider getValues
*
* @param mixed $value
*/
public function testGetValue($value): void
{
$this->assertSame($value, FilterData::fromArray(['value' => $value])->getValue());
}

/**
* @return iterable<array<mixed>>
*/
public function getValues(): iterable
{
yield [null];
yield [new \stdClass()];
yield [3];
yield ['3'];
}

public function testSetValue(): void
{
$filterData = FilterData::fromArray(['type' => 1, 'value' => 'value']);
$newFilterData = $filterData->changeValue('new_value');

$this->assertSame(1, $newFilterData->getType());
$this->assertSame('new_value', $newFilterData->getValue());
}

public function testGetValueThrowsExceptionIfValueNotPresent(): void
{
$filterData = FilterData::fromArray([]);

$this->expectException(\LogicException::class);
$this->expectExceptionMessage('The FilterData object does not have a value.');

$filterData->getValue();
}
}