Passed
Push — master ( cd6def...a3ee83 )
by Anatoly
02:04
created

Pass::present()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
/**
4
 * It's free open-source software released under the MIT License.
5
 *
6
 * @author Anatoly Fenric <[email protected]>
7
 * @copyright Copyright (c) 2018, Anatoly Fenric
8
 * @license https://github.com/sunrise-php/uri/blob/master/LICENSE
9
 * @link https://github.com/sunrise-php/uri
10
 */
11
12
namespace Sunrise\Uri\Component;
13
14
/**
15
 * Import classes
16
 */
17
use Sunrise\Uri\Exception\InvalidUriComponentException;
18
19
/**
20
 * URI component "pass"
21
 *
22
 * @link https://tools.ietf.org/html/rfc3986#section-3.2.1
23
 */
24
class Pass implements ComponentInterface
25
{
26
27
	/**
28
	 * The component value
29
	 *
30
	 * @var string
31
	 */
32
	protected $value = '';
33
34
	/**
35
	 * Constructor of the class
36
	 *
37
	 * @param mixed $value
38
	 *
39
	 * @throws InvalidUriComponentException
40
	 */
41
	public function __construct($value)
42
	{
43
		if ('' === $value)
44
		{
45
			return;
46
		}
47
		else if (! \is_string($value))
48
		{
49
			throw new InvalidUriComponentException('URI component "pass" must be a string');
50
		}
51
52
		$regex = '/(?:(?:%[0-9A-Fa-f]{2}|[0-9A-Za-z\-\._~\!\$&\'\(\)\*\+,;\=]+)|(.?))/u';
53
54
		$this->value = \preg_replace_callback($regex, function($match)
55
		{
56
			return isset($match[1]) ? \rawurlencode($match[1]) : $match[0];
57
58
		}, $value);
59
	}
60
61
	/**
62
	 * {@inheritDoc}
63
	 *
64
	 * @return string
65
	 */
66
	public function present() : string
67
	{
68
		return $this->value;
69
	}
70
}
71