Fragment::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 4
Ratio 100 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 4
loc 4
ccs 3
cts 3
cp 1
rs 10
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
namespace Subreality\Dilmun\Anshar\Http\UriParts;
3
4
/**
5
 * Class Fragment
6
 * @package Subreality\Dilmun\Anshar\Http\UriParts
7
 */
8 View Code Duplication
class Fragment extends AbstractUriPart
9
{
10
    protected static $unencoded_characters = array("/", "?");
11
12
    protected static $part_pattern;
13
14
    /**
15
     * Fragment constructor. Accepts a string representing a URI fragment. Construction will throw an exception if the
16
     * fragment is not a string.
17
     *
18
     * Construction accepts strings that have been percent-encoded as well as strings that have not been percent-encoded
19
     * and will encode invalid characters.
20
     *
21
     * Construction with a string that includes both encoded and decoded characters will be assumed to be an encoded
22
     * string, resulting in double-encoding.
23
     *
24
     * fragment    = *( pchar / "/" / "?" )
25
     * pchar       = unreserved / pct-encoded / sub-delims / ":" / "@"
26
     * unreserved  = ALPHA / DIGIT / "-" / "." / "_" / "~"
27
     * pct-encoded = "%" HEXDIG HEXDIG
28
     * sub-delims  = "!" / "$" / "&" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "="
29
     *
30
     * @see https://tools.ietf.org/html/rfc3986#appendix-A
31
     *
32
     * @throws \InvalidArgumentException
33
     *
34
     * @param string $fragment     A string representing a URI fragment
35
     */
36 75
    public function __construct($fragment)
37
    {
38 75
        parent::__construct($fragment, "Fragment");
39 69
    }
40
41
    /**
42
     * Returns a string representation of the fragment formatted so that it can be compiled into a complete URI string
43
     * per the Uri object's string specification.
44
     *
45
     * If the fragment is empty, toUriString returns an empty string; if the fragment is not empty, toUriString returns
46
     * the fragment prefixed with a octothorpe(#).
47
     *
48
     * @see Uri::__toString
49
     *
50
     * @return string   A string representation of the fragment formatted for a complete URI string
51
     */
52 18
    public function toUriString()
53
    {
54 18
        $uri_fragment = parent::toUriString(); //returns data string
55
56 18
        if (!empty($uri_fragment)) {
57 15
            $uri_fragment = "#" . $uri_fragment;
58 15
        }
59
60 18
        return $uri_fragment;
61
    }
62
}
63