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

IntegerValue   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 13
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 6
dl 0
loc 13
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A value() 0 10 4
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