Completed
Pull Request — master (#84)
by
unknown
01:16
created

FilterResponse   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 54
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 2
A __get() 0 4 1
A __set() 0 4 1
A toArray() 0 11 2
1
<?php
2
3
namespace TraderInteractive;
4
5
use TraderInteractive\Exceptions\ReadOnlyViolationException;
6
7
/**
8
 * This object contains the various data returned by a filter action.
9
 *
10
 * @property-read bool        $success       TRUE if the filter was successful or FALSE if errors were encountered.
11
 * @property-read mixed       $filteredValue The input values after being filtered.
12
 * @property-read array       $errors        Any errors encountered during the filter process.
13
 * @property-read string|null $errorMessage  An error message generated from the errors. NULL if no errors.
14
 * @property-read mixed       $unknowns      The values that were unknown during filtering.
15
 */
16
final class FilterResponse
17
{
18
    /**
19
     * @var array
20
     */
21
    private $response;
22
23
    /**
24
     * @param array $filteredValue The input values after being filtered.
25
     * @param array $errors        Any errors encountered during the filter process.
26
     * @param array $unknowns      The values that were unknown during filtering.
27
     */
28
    public function __construct(
29
        array $filteredValue,
30
        array $errors = [],
31
        array $unknowns = []
32
    ) {
33
        $success = count($errors) === 0;
34
        $this->response = [
35
            'success' => $success,
36
            'filteredValue' => $filteredValue,
37
            'errors' => $errors,
38
            'errorMessage' => $success ? null : implode("\n", $errors),
39
            'unknowns' => $unknowns,
40
        ];
41
    }
42
43
    public function __get($name)
44
    {
45
        return $this->response[$name];
46
    }
47
48
    public function __set($name, $value)
49
    {
50
        throw new ReadOnlyViolationException("Property {$name} is read-only");
51
    }
52
53
    /**
54
     * Converts the response to an array.
55
     *
56
     * @return array
57
     */
58
    public function toArray() : array
59
    {
60
        $filteredValue = $this->success ? $this->filteredValue : null;
61
62
        return [
63
            $this->success,
64
            $filteredValue,
65
            $this->errorMessage,
66
            $this->unknowns
67
        ];
68
    }
69
}
70