1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TraderInteractive; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use TraderInteractive\Exceptions\ReadOnlyViolationException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* This object contains the various data returned by a filter action. |
10
|
|
|
* |
11
|
|
|
* @property bool $success TRUE if the filter was successful or FALSE if errors were encountered. |
12
|
|
|
* @property mixed $filteredValue The input values after being filtered. |
13
|
|
|
* @property array $errors Any errors encountered during the filter process. |
14
|
|
|
* @property string|null $errorMessage An error message generated from the errors. NULL if no errors. |
15
|
|
|
* @property mixed $unknowns The values that were unknown during filtering. |
16
|
|
|
*/ |
17
|
|
|
final class FilterResponse |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
private $response; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param array $filteredValue The input values after being filtered. |
26
|
|
|
* @param array $errors Any errors encountered during the filter process. |
27
|
|
|
* @param array $unknowns The values that were unknown during filtering. |
28
|
|
|
*/ |
29
|
|
|
public function __construct( |
30
|
|
|
array $filteredValue, |
31
|
|
|
array $errors = [], |
32
|
|
|
array $unknowns = [] |
33
|
|
|
) { |
34
|
|
|
$success = count($errors) === 0; |
35
|
|
|
$this->response = [ |
36
|
|
|
'success' => $success, |
37
|
|
|
'filteredValue' => $filteredValue, |
38
|
|
|
'errors' => $errors, |
39
|
|
|
'errorMessage' => $success ? null : implode("\n", $errors), |
40
|
|
|
'unknowns' => $unknowns, |
41
|
|
|
]; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
View Code Duplication |
public function __get($name) |
|
|
|
|
45
|
|
|
{ |
46
|
|
|
if (array_key_exists($name, $this->response)) { |
47
|
|
|
return $this->response[$name]; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
throw new InvalidArgumentException("Property '{$name}' does not exist"); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
View Code Duplication |
public function __set($name, $value) |
|
|
|
|
54
|
|
|
{ |
55
|
|
|
if (array_key_exists($name, $this->response)) { |
56
|
|
|
throw new ReadOnlyViolationException("Property '{$name}' is read-only"); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
throw new InvalidArgumentException("Property '{$name}' does not exist"); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.