Skip to content

Commit

Permalink
RequestSchema accept both a path and an array of item
Browse files Browse the repository at this point in the history
Remove duplicate between RequestSchemaRepository and RequestSchema
  • Loading branch information
lcharette committed Jan 14, 2024
1 parent 77edb60 commit 9be7585
Show file tree
Hide file tree
Showing 14 changed files with 395 additions and 458 deletions.
87 changes: 71 additions & 16 deletions src/Fortress/RequestSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@

namespace UserFrosting\Fortress;

use UserFrosting\Fortress\RequestSchema\RequestSchemaRepository;
use UserFrosting\Support\Repository\Loader\FileRepositoryLoader;
use UserFrosting\Fortress\RequestSchema\RequestSchemaInterface;
use UserFrosting\Support\Repository\Loader\YamlFileLoader;
use UserFrosting\Support\Repository\Repository;

/**
* Represents a schema for an HTTP request, compliant with the WDVSS standard
Expand All @@ -21,29 +21,84 @@
* Same as \UserFrosting\Fortress\RequestSchema\RequestSchemaRepository, but
* loads the schema from a file instead of an array.
*/
class RequestSchema extends RequestSchemaRepository
class RequestSchema extends Repository implements RequestSchemaInterface
{
/**
* @var FileRepositoryLoader
* Loads the request schema from a file.
*
* @param string|mixed[]|null $input Either the full path to the file containing the [WDVSS schema](https://github.com/alexweissman/wdvss),
* the schema itself, or null to load an empty schema.
*
* @throws \UserFrosting\Support\Exception\FileNotFoundException If $input is string, and the file does not exist.
*/
protected FileRepositoryLoader $loader;
public function __construct(string|array|null $input = null)
{
$items = match (true) {
is_string($input) => (new YamlFileLoader($input))->load(false),
is_array($input) => $input,
default => [],
};

parent::__construct($items);
}

/**
* Loads the request schema from a file.
*
* @param string|null $path The full path to the file containing the [WDVSS schema](https://github.com/alexweissman/wdvss).
* {@inheritdoc}
*/
// TODO : Loader should be injected/injectable
// TODO : Could accept both a path and an array of item, making it more flexible and Repository redundant?
public function __construct(?string $path = null)
public function setDefault(string $field, string $value): static
{
$items = [];
if (!isset($this->items[$field])) {
$this->items[$field] = [];
}

$this->items[$field]['default'] = $value;

if (!is_null($path)) {
$this->loader = new YamlFileLoader($path);
$items = $this->loader->load(false);
return $this;
}

/**
* {@inheritdoc}
*/
public function addValidator(string $field, string $validatorName, array $parameters = []): static
{
if (!isset($this->items[$field])) {
$this->items[$field] = [];
}

parent::__construct($items);
if (!isset($this->items[$field]['validators'])) {
$this->items[$field]['validators'] = [];
}

$this->items[$field]['validators'][$validatorName] = $parameters;

return $this;
}

/**
* {@inheritdoc}
*/
public function removeValidator(string $field, string $validatorName): static
{
unset($this->items[$field]['validators'][$validatorName]);

return $this;
}

/**
* {@inheritdoc}
*/
public function setTransformations(string $field, string|array $transformations = []): static
{
if (!is_array($transformations)) {
$transformations = [$transformations];
}

if (!isset($this->items[$field])) {
$this->items[$field] = [];
}

$this->items[$field]['transformations'] = $transformations;

return $this;
}
}
4 changes: 3 additions & 1 deletion src/Fortress/RequestSchema/RequestSchemaInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@

namespace UserFrosting\Fortress\RequestSchema;

use Illuminate\Contracts\Config\Repository as ConfigContract;

/**
* Represents a schema for an HTTP request, compliant with the WDVSS standard
* (https://github.com/alexweissman/wdvss).
*/
interface RequestSchemaInterface
interface RequestSchemaInterface extends ConfigContract
{
/**
* Get all items in the schema.
Expand Down
65 changes: 4 additions & 61 deletions src/Fortress/RequestSchema/RequestSchemaRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,71 +10,14 @@

namespace UserFrosting\Fortress\RequestSchema;

use UserFrosting\Support\Repository\Repository;
use UserFrosting\Fortress\RequestSchema;

/**
* Represents a schema for an HTTP request, compliant with the WDVSS standard
* (https://github.com/alexweissman/wdvss).
*
* @deprecated 5.1 Use \UserFrosting\Fortress\RequestSchema directly instead
*/
class RequestSchemaRepository extends Repository implements RequestSchemaInterface
class RequestSchemaRepository extends RequestSchema
{
/**
* {@inheritdoc}
*/
public function setDefault(string $field, string $value): static
{
if (!isset($this->items[$field])) {
$this->items[$field] = [];
}

$this->items[$field]['default'] = $value;

return $this;
}

/**
* {@inheritdoc}
*/
public function addValidator(string $field, string $validatorName, array $parameters = []): static
{
if (!isset($this->items[$field])) {
$this->items[$field] = [];
}

if (!isset($this->items[$field]['validators'])) {
$this->items[$field]['validators'] = [];
}

$this->items[$field]['validators'][$validatorName] = $parameters;

return $this;
}

/**
* {@inheritdoc}
*/
public function removeValidator(string $field, string $validatorName): static
{
unset($this->items[$field]['validators'][$validatorName]);

return $this;
}

/**
* {@inheritdoc}
*/
public function setTransformations(string $field, string|array $transformations = []): static
{
if (!is_array($transformations)) {
$transformations = [$transformations];
}

if (!isset($this->items[$field])) {
$this->items[$field] = [];
}

$this->items[$field]['transformations'] = $transformations;

return $this;
}
}
36 changes: 18 additions & 18 deletions tests/Fortress/Adapter/FormValidationArrayAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

use PHPUnit\Framework\TestCase;
use UserFrosting\Fortress\Adapter\FormValidationArrayAdapter;
use UserFrosting\Fortress\RequestSchema\RequestSchemaRepository;
use UserFrosting\Fortress\RequestSchema;
use UserFrosting\I18n\Translator;
use UserFrosting\Tests\Fortress\DictionaryStub;

Expand All @@ -30,7 +30,7 @@ public function setUp(): void
public function testValidateEmail(): void
{
// Arrange
$schema = new RequestSchemaRepository([
$schema = new RequestSchema([
'email' => [
'validators' => [
'email' => [
Expand Down Expand Up @@ -65,7 +65,7 @@ public function testValidateEmail(): void
public function testValidateEquals(): void
{
// Arrange
$schema = new RequestSchemaRepository([
$schema = new RequestSchema([
'voles' => [
'validators' => [
'equals' => [
Expand Down Expand Up @@ -94,7 +94,7 @@ public function testValidateEquals(): void
public function testValidateRequired(): void
{
// Arrange
$schema = new RequestSchemaRepository([
$schema = new RequestSchema([
'species' => [
'validators' => [
'required' => [
Expand Down Expand Up @@ -125,7 +125,7 @@ public function testValidateRequired(): void
public function testValidateLengthBetween(): void
{
// Arrange
$schema = new RequestSchemaRepository([
$schema = new RequestSchema([
'screech' => [
'validators' => [
'length' => [
Expand Down Expand Up @@ -160,7 +160,7 @@ public function testValidateLengthBetween(): void
public function testValidateLengthMin(): void
{
// Arrange
$schema = new RequestSchemaRepository([
$schema = new RequestSchema([
'screech' => [
'validators' => [
'length' => [
Expand Down Expand Up @@ -193,7 +193,7 @@ public function testValidateLengthMin(): void
public function testValidateLengthMax(): void
{
// Arrange
$schema = new RequestSchemaRepository([
$schema = new RequestSchema([
'screech' => [
'validators' => [
'length' => [
Expand Down Expand Up @@ -226,7 +226,7 @@ public function testValidateLengthMax(): void
public function testValidateInteger(): void
{
// Arrange
$schema = new RequestSchemaRepository([
$schema = new RequestSchema([
'voles' => [
'validators' => [
'integer' => [
Expand Down Expand Up @@ -257,7 +257,7 @@ public function testValidateInteger(): void
public function testValidateNumeric(): void
{
// Arrange
$schema = new RequestSchemaRepository([
$schema = new RequestSchema([
'accuracy' => [
'validators' => [
'numeric' => [
Expand Down Expand Up @@ -288,7 +288,7 @@ public function testValidateNumeric(): void
public function testValidateRange(): void
{
// Arrange
$schema = new RequestSchemaRepository([
$schema = new RequestSchema([
'voles' => [
'validators' => [
'range' => [
Expand Down Expand Up @@ -323,7 +323,7 @@ public function testValidateRange(): void
public function testValidateRangeMin(): void
{
// Arrange
$schema = new RequestSchemaRepository([
$schema = new RequestSchema([
'voles' => [
'validators' => [
'range' => [
Expand Down Expand Up @@ -356,7 +356,7 @@ public function testValidateRangeMin(): void
public function testValidateRangeMax(): void
{
// Arrange
$schema = new RequestSchemaRepository([
$schema = new RequestSchema([
'voles' => [
'validators' => [
'range' => [
Expand Down Expand Up @@ -389,7 +389,7 @@ public function testValidateRangeMax(): void
public function testValidateArray(): void
{
// Arrange
$schema = new RequestSchemaRepository([
$schema = new RequestSchema([
'voles' => [
'validators' => [
'array' => [
Expand Down Expand Up @@ -424,7 +424,7 @@ public function testValidateArray(): void
public function testValidateMatches(): void
{
// Arrange
$schema = new RequestSchemaRepository([
$schema = new RequestSchema([
'password' => [
'validators' => [
'matches' => [
Expand Down Expand Up @@ -463,7 +463,7 @@ public function testValidateMatches(): void
public function testValidateMatchesNoFields(): void
{
// Arrange
$schema = new RequestSchemaRepository([
$schema = new RequestSchema([
'password' => [
'validators' => [
'matches' => [
Expand Down Expand Up @@ -494,7 +494,7 @@ public function testValidateMatchesNoFields(): void
public function testValidateNotMatches(): void
{
// Arrange
$schema = new RequestSchemaRepository([
$schema = new RequestSchema([
'password' => [
'validators' => [
'not_matches' => [
Expand Down Expand Up @@ -527,7 +527,7 @@ public function testValidateNotMatches(): void
public function testValidateMemberOf(): void
{
// Arrange
$schema = new RequestSchemaRepository([
$schema = new RequestSchema([
'genus' => [
'validators' => [
'member_of' => [
Expand Down Expand Up @@ -560,7 +560,7 @@ public function testValidateMemberOf(): void
public function testValidateNotMemberOf(): void
{
// Arrange
$schema = new RequestSchemaRepository([
$schema = new RequestSchema([
'genus' => [
'validators' => [
'not_member_of' => [
Expand Down
Loading

0 comments on commit 9be7585

Please sign in to comment.