Passed
Push — master ( 44dff5...2d1a70 )
by Daniel
01:20
created

ErrorDetailsResultTransformer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Selective\Validation\Transformer;
4
5
use Selective\Validation\ValidationError;
6
use Selective\Validation\ValidationResult;
7
8
/**
9
 * Transform validation result to array with error details.
10
 */
11
final class ErrorDetailsResultTransformer implements ResultTransformerInterface
12
{
13
    /**
14
     * @var string
15
     */
16
    private $detailsName;
17
18
    /**
19
     * The constructor.
20
     *
21
     * @param string $detailsName The name of the details index
22
     */
23 5
    public function __construct(string $detailsName = 'details')
24
    {
25 5
        $this->detailsName = $detailsName;
26 5
    }
27
28
    /**
29
     * Transform the given ValidationResult into an array.
30
     *
31
     * @param ValidationResult $validationResult The validation result
32
     *
33
     * @return array<mixed> The transformed result
34
     */
35 4
    public function transform(ValidationResult $validationResult): array
36
    {
37 4
        $error = [];
38
39 4
        $code = $validationResult->getCode();
40 4
        if ($code !== null) {
41
            $error['code'] = $code;
42
        }
43
44 4
        $message = $validationResult->getMessage();
45 4
        if ($message !== null) {
46 1
            $error['message'] = $message;
47
        }
48
49 4
        $errors = $validationResult->getErrors();
50 4
        if ($errors) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $errors of type Selective\Validation\ValidationError[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
51 4
            $error[$this->detailsName] = $this->getErrorDetails($errors);
52
        }
53
54 4
        return ['error' => $error];
55
    }
56
57
    /**
58
     * Get error details.
59
     *
60
     * @param ValidationError[] $errors The errors
61
     *
62
     * @return array<mixed> The details as array
63
     */
64 4
    private function getErrorDetails(array $errors): array
65
    {
66 4
        $details = [];
67
68 4
        foreach ($errors as $error) {
69
            $item = [
70 4
                'message' => $error->getMessage(),
71
            ];
72
73 4
            $fieldName = $error->getField();
74 4
            if ($fieldName !== null) {
75 4
                $item['field'] = $fieldName;
76
            }
77
78 4
            $errorCode = $error->getCode();
79 4
            if ($errorCode !== null) {
80 1
                $item['code'] = $errorCode;
81
            }
82
83 4
            $details[] = $item;
84
        }
85
86 4
        return $details;
87
    }
88
}
89