Completed
Push — master ( 638b8d...de83b5 )
by John
01:47 queued 17s
created

IntegerProcessor   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 0
loc 40
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 4
A hydrate() 0 10 4
1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of the KleijnWeb\PhpApi\Hydrator package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace KleijnWeb\PhpApi\Hydrator\Processors\Scalar;
10
11
use KleijnWeb\PhpApi\Descriptions\Description\Schema\ScalarSchema;
12
use KleijnWeb\PhpApi\Descriptions\Description\Schema\Schema;
13
use KleijnWeb\PhpApi\Hydrator\Exception\UnsupportedException;
14
15
/**
16
 * @author John Kleijn <[email protected]>
17
 */
18
class IntegerProcessor extends ScalarProcessor
19
{
20
    /**
21
     * @var bool
22
     */
23
    protected $is32BitSystem;
24
25
    /**
26
     * IntegerHydrator constructor.
27
     * @param ScalarSchema $schema
28
     * @param bool         $force32Bit
29
     */
30
    public function __construct(ScalarSchema $schema, $force32Bit = false)
31
    {
32
        parent::__construct($schema);
33
        $this->is32BitSystem = $force32Bit === true ? true : PHP_INT_SIZE === 4;
34
35
        /** @var ScalarSchema $scalarSchema */
36
        $scalarSchema = $this->schema;
37
38
        if ($this->is32BitSystem && $scalarSchema->hasFormat(Schema::FORMAT_INT64)) {
39
            throw new UnsupportedException("Schema unsupported: Operating system does not support 64 bit integers");
40
        }
41
    }
42
43
    /**
44
     * @param $value
45
     * @return int
46
     */
47
    public function hydrate($value)
48
    {
49
        if ($this->is32BitSystem && $value > (float)PHP_INT_MAX) {
50
            throw new UnsupportedException("Value unsupported: Operating system does not support 64 bit integers");
51
        }
52
53
        return $value === null
54
            ? $this->schema->getDefault()
55
            : (int)$value;
56
    }
57
}
58