Completed
Pull Request — master (#46)
by ignace nyamagana
05:56 queued 03:24
created

Data::__set_state()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
ccs 10
cts 10
cp 1
rs 9.4285
cc 1
eloc 9
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * League.Uri (http://uri.thephpleague.com)
4
 *
5
 * @package   League.uri
6
 * @author    Ignace Nyamagana Butera <[email protected]>
7
 * @copyright 2013-2015 Ignace Nyamagana Butera
8
 * @license   https://github.com/thephpleague/uri/blob/master/LICENSE (MIT License)
9
 * @version   4.1.1
10
 * @link      https://github.com/thephpleague/uri/
11
 */
12
namespace League\Uri\Schemes;
13
14
use InvalidArgumentException;
15
use League\Uri\Components\DataPath as Path;
16
use League\Uri\Components\Fragment;
17
use League\Uri\Components\Host;
18
use League\Uri\Components\Pass;
19
use League\Uri\Components\Port;
20
use League\Uri\Components\Query;
21
use League\Uri\Components\Scheme;
22
use League\Uri\Components\User;
23
use League\Uri\Components\UserInfo;
24
use League\Uri\Interfaces\DataPath as PathInterface;
25
use League\Uri\Interfaces\Fragment as FragmentInterface;
26
use League\Uri\Interfaces\Host as HostInterface;
27
use League\Uri\Interfaces\Port as PortInterface;
28
use League\Uri\Interfaces\Query as QueryInterface;
29
use League\Uri\Interfaces\Scheme as SchemeInterface;
30
use League\Uri\Interfaces\Uri;
31
use League\Uri\Interfaces\UserInfo as UserInfoInterface;
32
use League\Uri\Schemes\Generic\AbstractUri;
33
use League\Uri\UriParser;
34
35
/**
36
 * Value object representing Data Uri.
37
 *
38
 * @package League.uri
39
 * @author  Ignace Nyamagana Butera <[email protected]>
40
 * @since   4.0.0
41
 *
42
 * @property-read SchemeInterface   $scheme
43
 * @property-read UserInfoInterface $userInfo
44
 * @property-read HostInterface     $host
45
 * @property-read PortInterface     $port
46
 * @property-read PathInterface     $path
47
 * @property-read QueryInterface    $query
48
 * @property-read FragmentInterface $fragment
49
 */
50
class Data extends AbstractUri implements Uri
51
{
52
    /**
53
     * Create a new instance of URI
54
     *
55
     * @param SchemeInterface   $scheme
56
     * @param UserInfoInterface $userInfo
57
     * @param HostInterface     $host
58
     * @param PortInterface     $port
59
     * @param PathInterface     $path
60
     * @param QueryInterface    $query
61
     * @param FragmentInterface $fragment
62
     */
63 45
    public function __construct(
64
        SchemeInterface $scheme,
65
        UserInfoInterface $userInfo,
66
        HostInterface $host,
67
        PortInterface $port,
68
        PathInterface $path,
69
        QueryInterface $query,
70
        FragmentInterface $fragment
71
    ) {
72 45
        $this->scheme = $scheme;
73 45
        $this->userInfo = $userInfo;
74 45
        $this->host = $host;
75 45
        $this->port = $port;
76 45
        $this->path = $path;
77 45
        $this->query = $query;
78 45
        $this->fragment = $fragment;
79 45
        $this->assertValidObject();
80 36
    }
81
82
    /**
83
     * @inheritdoc
84
     */
85 57
    protected function isValid()
86
    {
87 57
        if ('data:' !== $this->scheme->getUriComponent()) {
88 3
            throw new InvalidArgumentException('The submitted scheme is invalid for the class '.get_class($this));
89
        }
90
91 54
        return $this->isValidGenericUri()
92 54
            && $this->__toString() === 'data:'.$this->path->getUriComponent();
93
    }
94
95
    /**
96
     * Create a new instance from a string
97
     *
98
     * @param string $uri
99
     *
100
     * @return static
101
     */
102 57
    public static function createFromString($uri = '')
103
    {
104 57
        return static::createFromComponents((new UriParser())->__invoke($uri));
105
    }
106
107
    /**
108
     * Create a new instance from a hash of parse_url parts
109
     *
110
     * @param array $components
111
     *
112
     * @return static
113
     */
114 60
    public static function createFromComponents(array $components)
115
    {
116 60
        $components = self::normalizeUriHash($components);
117
118 60
        return new static(
119 60
            new Scheme($components['scheme']),
120 60
            new UserInfo(new User($components['user']), new Pass($components['pass'])),
121 60
            new Host($components['host']),
122 60
            new Port($components['port']),
123 60
            new Path($components['path']),
124 36
            new Query($components['query']),
125 36
            new Fragment($components['fragment'])
126 24
        );
127
    }
128
129
    /**
130
     * Create a new instance from a file path
131
     *
132
     * @param string $path
133
     *
134
     * @return static
135
     */
136 18
    public static function createFromPath($path)
137
    {
138 18
        return new static(
139 18
            new Scheme('data'),
140 18
            new UserInfo(),
141 18
            new Host(),
142 18
            new Port(),
143 18
            Path::createFromPath($path),
144 9
            new Query(),
145 9
            new Fragment()
146 6
        );
147
    }
148
149
    /**
150
     * @inheritdoc
151
     */
152 3
    public static function __set_state(array $properties)
153
    {
154 3
        return new static(
155 3
            $properties['scheme'],
156 3
            $properties['userInfo'],
157 3
            $properties['host'],
158 3
            $properties['port'],
159 3
            $properties['path'],
160 3
            $properties['query'],
161 3
            $properties['fragment']
162 2
        );
163
    }
164
}
165