Completed
Pull Request — master (#16)
by Arnaud
48:07 queued 29:02
created

Url::serialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 6
rs 10
1
<?php
2
3
namespace LAG\SmokerBundle\Url;
4
5
use LAG\SmokerBundle\Exception\Exception;
6
7
class Url
8
{
9
    /**
10
     * @var string
11
     */
12
    protected $location;
13
14
    /**
15
     * @var string
16
     */
17
    protected $providerName;
18
19
    /**
20
     * @var array
21
     */
22
    protected $options;
23
24
    /**
25
     * Url constructor.
26
     *
27
     * @param string $location
28
     * @param string $providerName
29
     * @param array  $options
30
     */
31
    public function __construct(string $location, string $providerName, array $options = [])
32
    {
33
        $this->location = $location;
34
        $this->providerName = $providerName;
35
        $this->options = $options;
36
    }
37
38
    public function serialize(): string
39
    {
40
        return serialize([
41
            'location' => $this->location,
42
            'providerName' => $this->providerName,
43
            'options' => $this->options,
44
        ]);
45
    }
46
47
    public static function deserialize(string $serialized): Url
48
    {
49
        $data = unserialize($serialized);
50
51
        return new Url(
52
            $data['location'],
53
            $data['providerName'],
54
            $data['options']
55
        );
56
    }
57
58
    /**
59
     * @return string
60
     */
61
    public function getLocation(): string
62
    {
63
        return $this->location;
64
    }
65
66
    /**
67
     * @return string
68
     */
69
    public function getProviderName(): string
70
    {
71
        return $this->providerName;
72
    }
73
74
    /**
75
     * @return array
76
     */
77
    public function getOptions(): array
78
    {
79
        return $this->options;
80
    }
81
82
    public function hasOption(string $name): bool
83
    {
84
        return key_exists($name, $this->options);
85
    }
86
87
    public function getOption(string $name)
88
    {
89
        if (!$this->hasOption($name)) {
90
            throw new Exception('Unknown options "'.$name.'"');
91
        }
92
93
        return $this->options[$name];
94
    }
95
}
96