Completed
Pull Request — master (#20)
by Woody
02:17 queued 12s
created

Parameter::serialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 7
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php declare(strict_types=1);
2
3
namespace ElevenLabs\Api\Definition;
4
5
use stdClass;
6
7
class Parameter implements \Serializable
8
{
9
    const LOCATIONS = ['path', 'header', 'query', 'body', 'formData'];
10
    const BODY_LOCATIONS = ['formData', 'body'];
11
    const BODY_LOCATIONS_TYPES = ['formData' => 'application/x-www-form-urlencoded', 'body'  => 'application/json'];
12
13
    /** @var string */
14
    private $location;
15
16
    /** @var string */
17
    private $name;
18
19
    /** @var bool */
20
    private $required;
21
22
    /** @var ?stdClass */
23
    private $schema;
24
25
    /**
26
     * @throws \InvalidArgumentException If the location is not supported.
27
     */
28 2
    public function __construct(
29
        string $location,
30
        string $name,
31
        bool $required = false,
32
        ?stdClass $schema = null
33
    ) {
34 2
        if (! in_array($location, self::LOCATIONS, true)) {
35 1
            throw new \InvalidArgumentException(
36 1
                sprintf(
37 1
                    '%s is not a supported parameter location, supported: %s',
38 1
                    $location,
39 1
                    implode(', ', self::LOCATIONS)
40
                )
41
            );
42
        }
43
44 1
        $this->location = $location;
45 1
        $this->name = $name;
46 1
        $this->required = $required;
47 1
        $this->schema = $schema;
48 1
    }
49
50
    public function getLocation(): string
51
    {
52
        return $this->location;
53
    }
54
55
    public function getName(): string
56
    {
57
        return $this->name;
58
    }
59
60
    public function isRequired(): bool
61
    {
62
        return $this->required;
63
    }
64
65
    public function getSchema(): ?stdClass
66
    {
67
        return $this->schema;
68
    }
69
70 1
    public function hasSchema(): bool
71
    {
72 1
        return $this->schema !== null;
73
    }
74
75 1
    public function serialize()
76
    {
77 1
        return serialize([
78 1
            'location' => $this->location,
79 1
            'name' => $this->name,
80 1
            'required' => $this->required,
81 1
            'schema' => $this->schema
82
        ]);
83
    }
84
85 1
    public function unserialize($serialized)
86
    {
87 1
        $data = unserialize($serialized);
88 1
        $this->location = $data['location'];
89 1
        $this->name = $data['name'];
90 1
        $this->required  = $data['required'];
91 1
        $this->schema  = $data['schema'];
92 1
    }
93
}
94