FilterResponse   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
eloc 15
c 2
b 0
f 0
dl 0
loc 43
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __set() 0 7 2
A __construct() 0 12 2
A __get() 0 7 2
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
    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
    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