-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathRequestSchema.php
More file actions
106 lines (87 loc) · 2.96 KB
/
Copy pathRequestSchema.php
File metadata and controls
106 lines (87 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
declare(strict_types=1);
/*
* UserFrosting Framework (http://www.userfrosting.com)
*
* @link https://github.com/userfrosting/framework
* @copyright Copyright (c) 2013-2024 Alexander Weissman, Louis Charette, Jordan Mele
* @license https://github.com/userfrosting/framework/blob/master/LICENSE.md (MIT License)
*/
namespace UserFrosting\Fortress;
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
* (https://github.com/alexweissman/wdvss).
*
* Same as \UserFrosting\Fortress\RequestSchema\RequestSchemaRepository, but
* loads the schema from a file instead of an array.
*/
class RequestSchema extends Repository implements RequestSchemaInterface
{
/**
* 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.
*/
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);
}
/**
* {@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;
}
}