Completed
Push — master ( d8ac6c...cd03ce )
by Jesse
02:57
created

HasManyEmbedded::value()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stratadox\Hydration\Mapping\Property\Relationship;
6
7
use Stratadox\Hydration\Hydrates;
8
use Stratadox\Hydration\MapsProperty;
9
10
/**
11
 * Maps a list of scalars to a collection of objects.
12
 *
13
 * @package Stratadox\Hydrate
14
 * @author Stratadox
15
 */
16
class HasManyEmbedded implements MapsProperty
17
{
18
    private $name;
19
    private $collection;
20
    private $item;
21
    private $key;
22
23
    public function __construct(
24
        string $name,
25
        Hydrates $collection,
26
        Hydrates $item,
27
        string $key
28
    ) {
29
        $this->name = $name;
30
        $this->collection = $collection;
31
        $this->item = $item;
32
        $this->key = $key;
33
    }
34
35
    public static function inProperty(
36
        string $property,
37
        Hydrates $collection,
38
        Hydrates $item,
39
        string $key = 'key'
40
    ) : MapsProperty
41
    {
42
        return new static($property, $collection, $item, $key);
43
    }
44
45
    public function name() : string
46
    {
47
        return $this->name;
48
    }
49
50
    /** @inheritdoc @return mixed|object */
51
    public function value(array $data, $owner = null)
52
    {
53
        $objects = [];
54
        foreach ($data as $value) {
55
            $objects[] = $this->item->fromArray([$this->key => $value]);
56
        }
57
        return $this->collection->fromArray($objects);
58
    }
59
}
60