Completed
Push — master ( 05aed6...998c1c )
by Jesse
05:22
created

IntegerValue::value()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 10
rs 10
c 0
b 0
f 0
cc 4
nc 3
nop 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Stratadox\Hydration\Mapping\Property\Type;
5
6
use function preg_match;
7
use Stratadox\Hydration\Mapping\Property\UnmappableProperty;
8
9
/**
10
 * Maps integer-like input to an integer property in an object property.
11
 *
12
 * @package Stratadox\Hydrate
13
 * @author  Stratadox
14
 */
15
final class IntegerValue extends ScalarValue
16
{
17
    /** @inheritdoc */
18
    public function value(array $data, $owner = null): int
19
    {
20
        $value = $this->my($data);
21
        if (!preg_match('/^[-+]?\d+$/', (string) $value)) {
22
            throw UnmappableProperty::itMustBeLikeAnInteger($this, $value);
23
        }
24
        if ($value > (string) PHP_INT_MAX || $value < (string) PHP_INT_MIN) {
25
            throw UnmappableProperty::itMustBeInIntegerRange($this, $value);
26
        }
27
        return (int) $value;
28
    }
29
}
30