Completed
Push — master ( 8d5bbd...3b8b9e )
by Julián
05:58
created

AbstractScalarDTO::__unserialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
/*
4
 * dto (https://github.com/phpgears/dto).
5
 * General purpose immutable Data Transfer Objects for PHP.
6
 *
7
 * @license MIT
8
 * @link https://github.com/phpgears/dto
9
 * @author Julián Gutiérrez <[email protected]>
10
 */
11
12
declare(strict_types=1);
13
14
namespace Gears\DTO;
15
16
use Gears\DTO\Exception\InvalidScalarParameterException;
17
use Gears\Immutability\ImmutabilityBehaviour;
18
19
/**
20
 * Abstract immutable and only scalar values Data Transfer Object.
21
 */
22
abstract class AbstractScalarDTO implements DTO
23
{
24
    use ImmutabilityBehaviour, ScalarPayloadBehaviour {
25
        ScalarPayloadBehaviour::__call insteadof ImmutabilityBehaviour;
26
        ScalarPayloadBehaviour::checkParameterType as private scalarCheckParameterType;
27
    }
28
29
    /**
30
     * AbstractSerializableDTO constructor.
31
     *
32
     * @param array<string, mixed> $parameters
33
     */
34
    final protected function __construct(array $parameters)
35
    {
36
        $this->assertImmutable();
37
38
        $this->setPayload($parameters);
39
    }
40
41
    /**
42
     * @return mixed[]
43
     */
44
    final public function __sleep(): array
45
    {
46
        return ['payload'];
47
    }
48
49
    final public function __wakeup(): void
50
    {
51
        $this->assertImmutable();
52
    }
53
54
    /**
55
     * @return array<string, mixed>
56
     */
57
    final public function __serialize(): array
58
    {
59
        return ['payload' => $this->payload];
60
    }
61
62
    /**
63
     * @param array<string, mixed> $data
64
     *
65
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
66
     */
67
    final public function __unserialize(array $data): void
68
    {
69
        $this->assertImmutable();
70
71
        $this->setPayload($data['payload']);
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     *
77
     * @param mixed $value
78
     */
79
    final protected function checkParameterType(&$value): void
80
    {
81
        if ($value instanceof DTOCollection) {
82
            $value = \iterator_to_array($value->getElements());
83
        }
84
85
        if (\is_array($value)) {
86
            foreach ($value as $val) {
87
                $this->checkParameterType($val);
88
            }
89
        } elseif ($value !== null && !\is_scalar($value) && !$value instanceof self) {
90
            throw new InvalidScalarParameterException(\sprintf(
91
                'Class "%s" can only accept scalar payload parameters, "%s" given',
92
                self::class,
93
                \is_object($value) ? \get_class($value) : \gettype($value)
94
            ));
95
        }
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     *
101
     * @return string[]
102
     */
103
    final protected function getAllowedInterfaces(): array
104
    {
105
        return [DTO::class, \Serializable::class];
106
    }
107
}
108