RawResult   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 21
eloc 36
dl 0
loc 143
ccs 44
cts 44
cp 1
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getField() 0 7 2
A __construct() 0 5 1
A getId() 0 3 1
A normalizeBooleanValue() 0 3 3
A assert() 0 4 2
A normalizePropertyValue() 0 8 3
A getData() 0 3 1
A normalizeNumericValue() 0 3 2
A normalizeDateTimeValue() 0 3 1
A normalizeValue() 0 20 4
A getInfoBlockId() 0 3 1
1
<?php
2
3
namespace BitrixToolkit\BitrixEntityMapper\Query;
4
5
use DateTime;
6
use Exception;
7
use InvalidArgumentException;
8
use BitrixToolkit\BitrixEntityMapper\Annotation\Property\Property;
9
use BitrixToolkit\BitrixEntityMapper\Map\PropertyMap;
10
11
class RawResult
12
{
13
    /** @var int */
14
    protected $id;
15
16
    /** @var int */
17
    protected $infoBlockId;
18
19
    /** @var array */
20
    protected $data = [];
21
22
    /**
23
     * RawResult constructor.
24
     * @param int $id
25
     * @param int $infoBlockId
26
     * @param array $data
27
     */
28 20
    public function __construct($id, $infoBlockId, array $data)
29
    {
30 20
        $this->id = $id;
31 20
        $this->infoBlockId = $infoBlockId;
32 20
        $this->data = $data;
33
    }
34
35
    /**
36
     * @return int
37
     */
38 5
    public function getId()
39
    {
40 5
        return $this->id;
41
    }
42
43
    /**
44
     * @return int
45
     */
46 4
    public function getInfoBlockId()
47
    {
48 4
        return $this->infoBlockId;
49
    }
50
51
    /**
52
     * @return array
53
     */
54 19
    public function getData()
55
    {
56 19
        return $this->data;
57
    }
58
59
    /**
60
     * @param string $code
61
     * @return mixed
62
     */
63 2
    public function getField($code)
64
    {
65 2
        if (!array_key_exists($code, $this->data)) {
66 1
            throw new InvalidArgumentException("Поле $code не найдено в массиве данных.");
67
        }
68
69 1
        return $this->data[$code];
70
    }
71
72
    /**
73
     * @param mixed $term
74
     * @param string $msg
75
     */
76 2
    protected static function assert($term, $msg)
77
    {
78 2
        if (!$term) {
79 1
            throw new InvalidArgumentException($msg);
80
        }
81
    }
82
83
    /**
84
     * @param PropertyMap $property
85
     * @param mixed $rawValue
86
     * @return mixed
87
     * @throws Exception
88
     */
89 18
    public static function normalizePropertyValue(PropertyMap $property, $rawValue)
90
    {
91 18
        if ($property->getAnnotation()->isMultiple()) {
92 18
            return array_map(function ($value) use ($property) {
93 18
                return self::normalizeValue($property, $value);
94 18
            }, is_array($rawValue) ? $rawValue : []);
95
        } else {
96 18
            return self::normalizeValue($property, $rawValue);
97
        }
98
    }
99
100
    /**
101
     * @param PropertyMap $property
102
     * @param mixed $rawValue
103
     * @return mixed
104
     * @throws Exception
105
     */
106 19
    public static function normalizeValue(PropertyMap $property, $rawValue)
107
    {
108 19
        if ($rawValue === null) {
109 1
            return null;
110
        }
111
112 19
        $type = $property->getAnnotation()->getType();
113
114 19
        $map = [
115 19
            Property::TYPE_ENTITY => function ($value) use ($property) {
116 18
                $entity = $property->getAnnotation()->getEntity();
117 18
                return $value ? Select::from($entity)->whereRaw('ID', $value)->fetch() : null;
118 19
            },
119 19
            Property::TYPE_BOOLEAN => [self::class, 'normalizeBooleanValue'],
120 19
            Property::TYPE_INTEGER => [self::class, 'normalizeNumericValue'],
121 19
            Property::TYPE_FLOAT => [self::class, 'normalizeNumericValue'],
122 19
            Property::TYPE_DATETIME => [self::class, 'normalizeDateTimeValue'],
123 19
        ];
124
125 19
        return array_key_exists($type, $map) ? call_user_func($map[$type], $rawValue) : $rawValue;
126
    }
127
128
    /**
129
     * @param mixed $value
130
     * @return bool
131
     */
132 19
    protected static function normalizeBooleanValue($value)
133
    {
134 19
        return $value && $value !== 'N' ? true : false;
135
    }
136
137
    /**
138
     * @param mixed $value
139
     * @return int|float
140
     */
141 19
    protected static function normalizeNumericValue($value)
142
    {
143 19
        return strstr($value, '.') ? (float)$value : (int)$value;
144
    }
145
146
    /**
147
     * @param mixed $value
148
     * @return DateTime
149
     * @throws Exception
150
     */
151 19
    protected static function normalizeDateTimeValue($value)
152
    {
153 19
        return new DateTime($value);
154
    }
155
}