-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from chubbyphp/add-sort-to-collection
add-sort-to-collection
- Loading branch information
Showing
17 changed files
with
261 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Mapping\Validation\Constraint; | ||
|
||
use Chubbyphp\Validation\Constraint\ConstraintInterface; | ||
use Chubbyphp\Validation\ValidatorContextInterface; | ||
use Chubbyphp\Validation\ValidatorInterface; | ||
use Chubbyphp\Validation\ValidatorLogicException; | ||
use Chubbyphp\Validation\Error\ErrorInterface; | ||
use Chubbyphp\Validation\Error\Error; | ||
|
||
final class SortConstraint implements ConstraintInterface | ||
{ | ||
/** | ||
* @var string[] | ||
*/ | ||
private $allowedFields; | ||
|
||
const ALLOWED_ORDERS = ['asc', 'desc']; | ||
|
||
/** | ||
* @param array $allowedFields | ||
*/ | ||
public function __construct(array $allowedFields) | ||
{ | ||
$this->allowedFields = $allowedFields; | ||
} | ||
|
||
/** | ||
* @param string $path | ||
* @param mixed $sort | ||
* @param ValidatorContextInterface $context | ||
* @param ValidatorInterface|null $validator | ||
* | ||
* @return ErrorInterface[] | ||
* | ||
* @throws ValidatorLogicException | ||
*/ | ||
public function validate( | ||
string $path, | ||
$sort, | ||
ValidatorContextInterface $context, | ||
ValidatorInterface $validator = null | ||
): array { | ||
if (!is_array($sort)) { | ||
return [new Error( | ||
$path, | ||
'constraint.sort.invalidtype', | ||
['type' => is_object($sort) ? get_class($sort) : gettype($sort)] | ||
)]; | ||
} | ||
|
||
$errors = []; | ||
|
||
foreach ($sort as $field => $order) { | ||
if (!in_array($field, $this->allowedFields, true)) { | ||
$errors[] = new Error( | ||
$path, | ||
'constraint.sort.field.notallowed', | ||
['field' => $field, 'allowedFields' => $this->allowedFields] | ||
); | ||
} | ||
|
||
if (!is_string($order)) { | ||
$errors[] = new Error( | ||
$path, | ||
'constraint.sort.order.invalidtype', | ||
['field' => $field, 'type' => is_object($order) ? get_class($order) : gettype($order)] | ||
); | ||
|
||
continue; | ||
} | ||
|
||
if (!in_array($order, self::ALLOWED_ORDERS, true)) { | ||
$errors[] = new Error( | ||
$path, | ||
'constraint.sort.order.notallowed', | ||
['field' => $field, 'order' => $order, 'allowedOrders' => self::ALLOWED_ORDERS] | ||
); | ||
} | ||
} | ||
|
||
return $errors; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
tests/Unit/Mapping/Validation/Constraint/SortConstraintTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Tests\Unit\Mapping\Validation\Constraint; | ||
|
||
use App\Mapping\Validation\Constraint\SortConstraint; | ||
use Chubbyphp\Mock\MockByCallsTrait; | ||
use Chubbyphp\Validation\ValidatorContextInterface; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
use Chubbyphp\Validation\Error\Error; | ||
|
||
/** | ||
* @covers \App\Mapping\Validation\Constraint\SortConstraint | ||
*/ | ||
final class SortConstraintTest extends TestCase | ||
{ | ||
use MockByCallsTrait; | ||
|
||
public function testValidateWithString() | ||
{ | ||
/** @var ValidatorContextInterface|MockObject $context */ | ||
$context = $this->getMockByCalls(ValidatorContextInterface::class); | ||
|
||
$constraint = new SortConstraint(['name']); | ||
|
||
self::assertEquals( | ||
[new Error('path', 'constraint.sort.invalidtype', ['type' => 'string'])], | ||
$constraint->validate('path', 'name', $context) | ||
); | ||
} | ||
|
||
public function testValidateWithStdClass() | ||
{ | ||
/** @var ValidatorContextInterface|MockObject $context */ | ||
$context = $this->getMockByCalls(ValidatorContextInterface::class); | ||
|
||
$constraint = new SortConstraint(['name']); | ||
|
||
self::assertEquals( | ||
[new Error('path', 'constraint.sort.invalidtype', ['type' => \stdClass::class])], | ||
$constraint->validate('path', new \stdClass(), $context) | ||
); | ||
} | ||
|
||
public function testValidateWithUnsupportedFieldAndUnsupportedOrder() | ||
{ | ||
/** @var ValidatorContextInterface|MockObject $context */ | ||
$context = $this->getMockByCalls(ValidatorContextInterface::class); | ||
|
||
$constraint = new SortConstraint(['name']); | ||
|
||
self::assertEquals( | ||
[ | ||
new Error( | ||
'path', | ||
'constraint.sort.field.notallowed', | ||
['field' => 'unknown', 'allowedFields' => ['name']] | ||
), | ||
new Error( | ||
'path', | ||
'constraint.sort.order.notallowed', | ||
['field' => 'unknown', 'order' => 'test', 'allowedOrders' => ['asc', 'desc']] | ||
), | ||
], | ||
$constraint->validate('path', ['name' => 'asc', 'unknown' => 'test'], $context) | ||
); | ||
} | ||
|
||
public function testValidateWithUnsupportedFieldAndUnsupportedOrderType() | ||
{ | ||
/** @var ValidatorContextInterface|MockObject $context */ | ||
$context = $this->getMockByCalls(ValidatorContextInterface::class); | ||
|
||
$constraint = new SortConstraint(['name']); | ||
|
||
self::assertEquals( | ||
[ | ||
new Error( | ||
'path', | ||
'constraint.sort.field.notallowed', | ||
['field' => 'unknown', 'allowedFields' => ['name']] | ||
), | ||
new Error( | ||
'path', | ||
'constraint.sort.order.invalidtype', | ||
['field' => 'unknown', 'type' => \stdClass::class] | ||
), | ||
], | ||
$constraint->validate('path', ['name' => 'asc', 'unknown' => new \stdClass()], $context) | ||
); | ||
} | ||
|
||
public function testValidate() | ||
{ | ||
/** @var ValidatorContextInterface|MockObject $context */ | ||
$context = $this->getMockByCalls(ValidatorContextInterface::class); | ||
|
||
$constraint = new SortConstraint(['name']); | ||
|
||
self::assertSame([], $constraint->validate('path', ['name' => 'asc'], $context)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.