Passed
Push — develop ( f95c4c...97a737 )
by Fabian
01:53
created

AbstractMessage::getParameter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cakasim\Payone\Sdk\Api\Message;
6
7
/**
8
 * Base class for API messages.
9
 *
10
 * @author Fabian Böttcher <[email protected]>
11
 * @since 0.1.0
12
 */
13
abstract class AbstractMessage implements MessageInterface
14
{
15
    /**
16
     * @var array The message parameters.
17
     */
18
    protected $parameters = [];
19
20
    /**
21
     * Returns the value of a parameter.
22
     *
23
     * @param string $name The parameter name.
24
     * @return string|null The parameter value or null if no such parameter exists.
25
     */
26
    protected function getParameter(string $name): ?string
27
    {
28
        return $this->parameters[$name] ?? null;
29
    }
30
31
    /**
32
     * Sets the value of a parameter.
33
     *
34
     * @param string $name The parameter name.
35
     * @param string $value The parameter value.
36
     */
37
    protected function setParameter(string $name, string $value): void
38
    {
39
        $this->parameters[$name] = $value;
40
    }
41
42
    /**
43
     * Generates a representation of the current state for serializing.
44
     *
45
     * @return array A serializable representation of the current state.
46
     */
47
    protected function generateState(): array
48
    {
49
        return $this->parameters;
50
    }
51
52
    /**
53
     * Restores the state from the provided data.
54
     *
55
     * @param array $data The data to restore the state from.
56
     */
57
    protected function restoreState(array $data): void
58
    {
59
        $this->parameters = $data;
60
    }
61
62
    /**
63
     * @inheritDoc
64
     */
65
    public function serialize(): string
66
    {
67
        return \serialize($this->generateState());
68
    }
69
70
    /**
71
     * @inheritDoc
72
     */
73
    public function unserialize($serialized): void
74
    {
75
        $this->restoreState(\unserialize($serialized));
76
    }
77
}
78