Passed
Push — master ( f1d34f...924412 )
by Jesse
02:05
created

HasManyEmbedded::inProperty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 2
b 0
f 0
nc 1
nop 4
dl 0
loc 7
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace Stratadox\Hydration\Mapping\Property\Relationship;
5
6
use Stratadox\Deserializer\Deserializer;
7
use Stratadox\Hydration\Mapping\Relation\CollectionMappingFailure;
8
use Stratadox\Hydration\Mapping\Relation\RelationMappingFailure;
9
use Stratadox\HydrationMapping\Mapping;
10
use Throwable;
11
12
/**
13
 * @deprecated
14
 * @codeCoverageIgnore
15
 */
16
final class HasManyEmbedded implements Mapping
17
{
18
    /** @var string */
19
    private $name;
20
    /** @var Deserializer */
21
    private $collection;
22
    /** @var Deserializer */
23
    private $item;
24
    /** @var string */
25
    private $key;
26
27
    private function __construct(
28
        string $name,
29
        Deserializer $collection,
30
        Deserializer $item,
31
        string $key
32
    ) {
33
        $this->name = $name;
34
        $this->collection = $collection;
35
        $this->item = $item;
36
        $this->key = $key;
37
    }
38
39
    public static function inProperty(
40
        string $name,
41
        Deserializer $collection,
42
        Deserializer $item,
43
        string $key = 'key'
44
    ): Mapping {
45
        return new self($name, $collection, $item, $key);
46
    }
47
48
    public function name(): string
49
    {
50
        return $this->name;
51
    }
52
53
    public function value(array $data, $owner = null)
54
    {
55
        $objects = [];
56
        try {
57
            foreach ($data as $value) {
58
                $objects[] = $this->item->from([$this->key => $value]);
59
            }
60
        } catch (Throwable $exception) {
61
            throw RelationMappingFailure::encountered($this, $exception);
62
        }
63
        try {
64
            return $this->collection->from($objects);
65
        } catch (Throwable $exception) {
66
            throw CollectionMappingFailure::encountered($this, $exception);
67
        }
68
    }
69
}
70