Completed
Pull Request — master (#5)
by John
01:37
created

ArrayProcessor   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 55
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setItemsProcessor() 0 6 1
A hydrate() 0 12 3
A dehydrate() 0 6 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