Passed
Push — master ( c2faf8...afa411 )
by Al3x
11:51
created

ContactsStrategy::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace InvoiceNinjaModule\Strategy;
5
6
use InvoiceNinjaModule\Model\Contact;
7
use InvoiceNinjaModule\Model\Interfaces\ContactInterface;
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 ContactsStrategy
15
 */
16
final class ContactsStrategy implements StrategyInterface
17
{
18
    private HydratorInterface $hydrator;
19
20
    /**
21
     * ContactsStrategy constructor.
22
     *
23
     * @param HydratorInterface $hydrator
24
     */
25 6
    public function __construct(HydratorInterface $hydrator)
26
    {
27 6
        $this->hydrator = $hydrator;
28 6
    }
29
30
    /**
31
     * Converts the given value so that it can be extracted by the hydrator.
32
     *
33
     * @param ContactInterface[]  $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 2
    public function extract($value, ?object $object = null) : array
39
    {
40 2
        $result = [];
41 2
        foreach ($value as $contactObj) {
42 1
            if ($contactObj instanceof ContactInterface) {
43 1
                $result[] = $this->hydrator->extract($contactObj);
44
            }
45
        }
46 2
        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 ContactInterface[]
54
     * @throws BadMethodCallException for a non-object $contactObj
55
     */
56 2
    public function hydrate($value, ?array $data) : array
57
    {
58 2
        $result = [];
59 2
        if (is_array($value)) {
0 ignored issues
show
introduced by
The condition is_array($value) is always true.
Loading history...
60 2
            foreach ($value as $contact) {
61 1
                $contactObj = new Contact();
62 1
                $this->hydrator->hydrate($contact, $contactObj);
63 1
                $result[] = $contactObj;
64
            }
65
        }
66 2
        return $result;
67
    }
68
}
69