Passed
Push — master ( d38468...eb5cf9 )
by Al3x
01:39
created

InvitationsStrategy::hydrate()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 7
c 1
b 0
f 1
nc 2
nop 2
dl 0
loc 11
ccs 0
cts 8
cp 0
crap 12
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace InvoiceNinjaModule\Strategy;
5
6
use InvoiceNinjaModule\Model\Interfaces\InvitationInterface;
7
use InvoiceNinjaModule\Model\Invitation;
8
use Laminas\Hydrator\Exception\BadMethodCallException;
9
use Laminas\Hydrator\HydratorInterface;
10
use Laminas\Hydrator\Strategy\StrategyInterface;
11
use function is_array;
12
13
/**
14
 * Class InvitationsStrategy
15
 */
16
final class InvitationsStrategy implements StrategyInterface
17
{
18
    private HydratorInterface $hydrator;
19
20
    /**
21
     * InvitationsStrategy constructor.
22
     *
23
     * @param HydratorInterface $hydrator
24
     */
25
    public function __construct(HydratorInterface $hydrator)
26
    {
27
        $this->hydrator = $hydrator;
28
    }
29
30
    /**
31
     * Converts the given value so that it can be extracted by the hydrator.
32
     *
33
     * @param InvitationInterface[]  $value  The original value.
34
     *
35
     * @return array Returns the value that should be extracted.
36
     * @throws BadMethodCallException for a non-object $contactObj
37
     */
38
    public function extract($value, ?object $object = null): array
39
    {
40
        $result = [];
41
        foreach ($value as $invitation) {
42
            if ($invitation instanceof InvitationInterface) {
43
                $result[] = $this->hydrator->extract($invitation);
44
            }
45
        }
46
        return $result;
47
    }
48
49
    /**
50
     * Converts the given value so that it can be hydrated by the hydrator.
51
     * @param array $value
52
     *
53
     * @return InvitationInterface[]
54
     * @throws BadMethodCallException for a non-object $contactObj
55
     */
56
    public function hydrate($value, ?array $data) :array
57
    {
58
        $result = [];
59
        if (is_array($value)) {
0 ignored issues
show
introduced by
The condition is_array($value) is always true.
Loading history...
60
            foreach ($value as $invitationArr) {
61
                $invitation = new Invitation();
62
                $this->hydrator->hydrate($invitationArr, $invitation);
63
                $result[] = $invitation;
64
            }
65
        }
66
        return $result;
67
    }
68
}
69