InvalidTypeDtoException::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 7
ccs 0
cts 5
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
1
<?php
2
3
namespace Larapie\DataTransferObject\Exceptions;
4
5
use TypeError;
6
use Larapie\DataTransferObject\Contracts\PropertyContract;
7
8
class InvalidTypeDtoException extends TypeError
9
{
10
    public function __construct(PropertyContract $property, $value)
11
    {
12
        $value = $this->resolveTypeFromValue($value);
13
14
        $expectedTypes = $this->resolveExpectedTypes($property);
15
16
        parent::__construct("Invalid type: expected {$property->getFqn()} to be of type {$expectedTypes}, instead got value `{$value}`.");
17
    }
18
19
    protected function resolveTypeFromValue($value): string
20
    {
21
        if ($value === null) {
22
            $value = 'null';
23
        }
24
25
        if (is_object($value)) {
26
            $value = get_class($value);
27
        }
28
29
        if (is_array($value)) {
30
            $value = 'array';
31
        }
32
33
        return $value;
34
    }
35
36
    protected function resolveExpectedTypes(PropertyContract $property)
37
    {
38
        return implode(', ', $property->getTypes());
39
    }
40
}
41