Completed
Pull Request — master (#40)
by ignace nyamagana
11:26
created

Data::isValid()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 6
Bugs 0 Features 0
Metric Value
c 6
b 0
f 0
dl 0
loc 9
ccs 4
cts 4
cp 1
rs 9.6666
cc 3
eloc 5
nc 3
nop 0
crap 3
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.0
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 28
     * @param FragmentInterface $fragment
62
     */
63
    public function __construct(
64
        SchemeInterface $scheme,
65
        UserInfoInterface $userInfo,
66
        HostInterface $host,
67
        PortInterface $port,
68
        PathInterface $path,
69
        QueryInterface $query,
70 28
        FragmentInterface $fragment
71 28
    ) {
72 28
        $this->scheme = $scheme;
73 28
        $this->userInfo = $userInfo;
74 28
        $this->host = $host;
75 28
        $this->port = $port;
76 28
        $this->path = $path;
77 28
        $this->query = $query;
78 22
        $this->fragment = $fragment;
79
        $this->assertValidObject();
80
    }
81
82
    /**
83 36
     * @inheritdoc
84
     */
85 36
    protected function isValid()
86 2
    {
87
        if ('data:' !== $this->scheme->getUriComponent()) {
88
            throw new InvalidArgumentException('The submitted scheme is invalid for the class '.get_class($this));
89 34
        }
90 34
91
        return $this->isValidGenericUri()
92
            && $this->__toString() === 'data:'.$this->path->getUriComponent();
93
    }
94
95
    /**
96
     * Create a new instance from a string
97
     *
98
     * @param string $uri
99
     *
100 38
     * @return static
101
     */
102 38
    public static function createFromString($uri = '')
103
    {
104
        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 40
     * @return static
113
     */
114 40
    public static function createFromComponents(array $components)
115
    {
116 40
        $components = self::normalizeUriHash($components);
117 40
118 40
        return new static(
119 40
            new Scheme($components['scheme']),
120 40
            new UserInfo(new User($components['user']), new Pass($components['pass'])),
121 40
            new Host($components['host']),
122 24
            new Port($components['port']),
123 24
            new Path($components['path']),
124 24
            new Query($components['query']),
125
            new Fragment($components['fragment'])
126
        );
127
    }
128
129
    /**
130
     * Create a new instance from a file path
131
     *
132
     * @param string $path
133
     *
134 10
     * @return static
135
     */
136 10
    public static function createFromPath($path)
137 10
    {
138 10
        return new static(
139 10
            new Scheme('data'),
140 10
            new UserInfo(),
141 10
            new Host(),
142 4
            new Port(),
143 4
            Path::createFromPath($path),
144 4
            new Query(),
145
            new Fragment()
146
        );
147
    }
148
149
    /**
150
     * @inheritdoc
151
     */
152
    public static function __set_state(array $components)
153
    {
154
        return new static(
155
            $components['scheme'],
156
            $components['userInfo'],
157
            $components['host'],
158
            $components['port'],
159
            $components['path'],
160
            $components['query'],
161
            $components['fragment']
162
        );
163
    }
164
}
165