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

AnyProcessor::hydrate()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 16
nc 7
nop 1
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;
10
11
use KleijnWeb\PhpApi\Descriptions\Description\Schema\AnySchema;
12
use KleijnWeb\PhpApi\Descriptions\Description\Schema\ScalarSchema;
13
use KleijnWeb\PhpApi\Descriptions\Description\Schema\Schema;
14
use KleijnWeb\PhpApi\Hydrator\DateTimeSerializer;
15
use KleijnWeb\PhpApi\Hydrator\Exception\UnsupportedException;
16
17
/**
18
 * @author John Kleijn <[email protected]>
19
 */
20
class AnyProcessor extends Processor
21
{
22
    /**
23
     * @var DateTimeSerializer
24
     */
25
    protected $dateTimeSerializer;
26
27
    /**
28
     * @var ScalarSchema
29
     */
30
    protected $dateSchema;
31
32
33
    /**
34
     * AnyProcessor constructor.
35
     * @param AnySchema          $schema
36
     * @param DateTimeSerializer $dateTimeSerializer
37
     */
38
    public function __construct(AnySchema $schema, DateTimeSerializer $dateTimeSerializer)
39
    {
40
        parent::__construct($schema);
41
42
        $this->dateTimeSerializer = $dateTimeSerializer;
43
44
        $this->dateSchema = new ScalarSchema(
45
            (object)['type' => Schema::TYPE_STRING, 'format' => Schema::FORMAT_DATE]
46
        );
47
    }
48
49
    /**
50
     * Cast a scalar value using the schema.
51
     *
52
     * @param mixed $value
53
     *
54
     * @return float|int|string|\DateTimeInterface
55
     * @throws UnsupportedException
56
     */
57
    public function hydrate($value)
58
    {
59
        if ($value instanceof \stdClass) {
60
            $value = clone $value;
61
            foreach ($value as $name => $propertyValue) {
0 ignored issues
show
Bug introduced by
The expression $value of type object<stdClass> is not traversable.
Loading history...
62
                $value->$name = $this->hydrate($propertyValue);
63
            }
64
65
            return $value;
66
        }
67
68
        if (is_array($value)) {
69
            return array_map(function ($itemValue) {
70
                return $this->hydrate($itemValue);
71
            }, $value);
72
        }
73
74
        if (is_string($value)) {
75
            if (preg_match('/^[0-9]{4}-[0-9]{2}-[0-9]{2}(.*)$/', $value, $matches)) {
76
                if (strlen($matches[1]) === 0) {
77
                    return $this->dateTimeSerializer->deserialize($value, $this->dateSchema);
78
                }
79
80
                return $this->dateTimeSerializer->deserialize($value, $this->schema);
81
            }
82
        }
83
84
        return $value;
85
    }
86
87
    /**
88
     * @param mixed $value
89
     * @return mixed
90
     */
91
    public function dehydrate($value)
92
    {
93
        return $value;
94
    }
95
}
96