ArrayProcessor::setItemsProcessor()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
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\ArraySchema;
12
13
/**
14
 * @author John Kleijn <[email protected]>
15
 */
16
class ArrayProcessor extends Processor
17
{
18
    /**
19
     * @var Processor
20
     */
21
    protected $itemsProcessor;
22
23
    /**
24
     * ArrayHydrator constructor.
25
     * @param ArraySchema $schema
26
     */
27
    public function __construct(ArraySchema $schema)
28
    {
29
        parent::__construct($schema);
30
    }
31
32
    /**
33
     * @param Processor $itemsHydrator
34
     * @return ArrayProcessor
35
     */
36
    public function setItemsProcessor(Processor $itemsHydrator): ArrayProcessor
37
    {
38
        $this->itemsProcessor = $itemsHydrator;
39
40
        return $this;
41
    }
42
43
    /**
44
     * @param null|array $value
45
     * @return null|array
46
     */
47
    public function hydrate($value)
48
    {
49
        if ($value === null) {
50
            if (null == ($value = $this->schema->getDefault())) {
51
                return null;
52
            }
53
        }
54
55
        return array_map(function ($value) {
56
            return $this->itemsProcessor->hydrate($value);
57
        }, $value);
58
    }
59
60
    /**
61
     * @param mixed $value
62
     * @return mixed
63
     */
64
    public function dehydrate($value)
65
    {
66
        return array_map(function ($value) {
67
            return $this->itemsProcessor->dehydrate($value);
68
        }, $value);
69
    }
70
}
71