-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathServerSideValidator.php
More file actions
97 lines (83 loc) · 2.39 KB
/
Copy pathServerSideValidator.php
File metadata and controls
97 lines (83 loc) · 2.39 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
<?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\Fortress\Validator\ServerSideValidator as Validator;
use UserFrosting\I18n\Translator;
/**
* Loads validation rules from a schema and validates a target array of data.
*
* @deprecated 5.1 Use `\UserFrosting\Fortress\Validator\ServerSideValidator` instead
*/
class ServerSideValidator implements ServerSideValidatorInterface
{
/** @var mixed[] */
protected array $errors = [];
/** @var mixed[] */
protected array $data = [];
/**
* Create a new server-side validator.
*
* @param RequestSchemaInterface $schema A RequestSchemaInterface object, containing the validation rules.
* @param Translator $translator A Translator to be used to translate message ids found in the schema.
*/
public function __construct(
protected RequestSchemaInterface $schema,
protected Translator $translator
) {
}
/**
* {@inheritdoc}
*/
public function setSchema(RequestSchemaInterface $schema): void
{
$this->schema = $schema;
}
/**
* {@inheritdoc}
*/
public function setTranslator(Translator $translator): void
{
$this->translator = $translator;
}
/**
* {@inheritdoc}
*/
public function validate(array $data = []): bool
{
$validator = new Validator($this->translator);
$this->data = $data;
$this->errors = $validator->validate($this->schema, $data);
return count($this->errors) === 0;
}
/**
* Get array of fields and data.
*
* @return mixed[]
*/
public function data(): array
{
return $this->data;
}
/**
* Get array of error messages.
*
* @param null|string $field
*
* @return mixed[]|bool
*/
public function errors(?string $field = null)
{
if ($field !== null) {
return isset($this->errors[$field]) ? $this->errors[$field] : false;
}
return $this->errors;
}
}