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

FilterResponse::toArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace TraderInteractive;
4
5
/**
6
 * This object contains the various data returned by a filter action.
7
 *
8
 * @property-read bool        $success       TRUE if the filter was successful or FALSE if errors were encountered.
9
 * @property-read mixed       $filteredValue The input values after being filtered.
10
 * @property-read array       $errors        Any errors encountered during the filter process.
11
 * @property-read string|null $errorMessage  An error message generated from the errors. NULL if no errors.
12
 * @property-read mixed       $unknowns      The values that were unknown during filtering.
13
 */
14
final class FilterResponse
15
{
16
    /**
17
     * @param array $filteredValue The input values after being filtered.
18
     * @param array $errors        Any errors encountered during the filter process.
19
     * @param array $unknowns      The values that were unknown during filtering.
20
     */
21
    public function __construct(
22
        array $filteredValue,
23
        array $errors = [],
24
        array $unknowns = []
25
    ) {
26
        $this->success = count($errors) === 0;
27
        $this->filteredValue = $filteredValue;
28
        $this->errors = $errors;
29
        $this->errorMessage = $this->success ? null : implode("\n", $errors);
30
        $this->unknowns = $unknowns;
31
    }
32
33
    /**
34
     * Converts the response to an array.
35
     *
36
     * @return array
37
     */
38
    public function toArray() : array
39
    {
40
        $filteredValue = $this->success ? $this->filteredValue : null;
41
42
        return [
43
            $this->success,
44
            $filteredValue,
45
            $this->errorMessage,
46
            $this->unknowns
47
        ];
48
    }
49
}
50