Completed
Push — master ( 1bc4b8...c77c49 )
by ignace nyamagana
02:36
created

User::validate()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 2
b 0
f 0
nc 2
nop 1
dl 0
loc 11
ccs 5
cts 5
cp 1
crap 3
rs 9.4285
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.2.0
10
 * @link      https://github.com/thephpleague/uri/
11
 */
12
namespace League\Uri\Components;
13
14
use InvalidArgumentException;
15
use League\Uri\Interfaces\User as UserInterface;
16
17
/**
18
 * Value object representing a URI user component.
19
 *
20
 * @package League.uri
21
 * @author  Ignace Nyamagana Butera <[email protected]>
22
 * @since   1.0.0
23
 */
24
class User extends AbstractComponent implements UserInterface
25
{
26
    /**
27
     * @inheritdoc
28 888
     */
29
    protected function validate($str)
30 888
    {
31 738
        if (!is_string($str) || !preg_match(',[/:@?#],', $str)) {
32
            return parent::validate($str);
33
        }
34 213
35 213
        throw new InvalidArgumentException(sprintf(
36
            'The encoded user string `%s` contains invalid characters `/:@?#`',
37 213
            $str
38
        ));
39
    }
40
41
    /**
42
     * @inheritdoc
43 2
     */
44
    public function getContent()
45 2
    {
46
        if (null === $this->data) {
47
            return $this->data;
48
        }
49
50
        $regexp = '/(?:[^'.self::$unreservedChars.self::$subdelimChars.']+
51
            |%(?!'.self::$encodedChars.'))/x';
52
53 30
        return $this->encode((string) $this->data, $regexp);
54
    }
55 30
56
    /**
57
     * @inheritdoc
58
     */
59
    public function __debugInfo()
60
    {
61
        return ['user' => $this->getContent()];
62
    }
63
64
    /**
65
     * Return the decoded string representation of the component
66
     *
67
     * @return null|string
68
     */
69
    public function getDecoded()
70
    {
71
        if (null === $this->data) {
72
            return null;
73
        }
74
75
        return $this->data;
76
    }
77
}
78