Code Duplication    Length = 91-91 lines in 2 locations

src/Anshar/Http/UriParts/Fragment.php 1 location

@@ 8-98 (lines=91) @@
5
 * Class Fragment
6
 * @package Subreality\Dilmun\Anshar\Http\UriParts
7
 */
8
class Fragment extends AbstractUriPart
9
{
10
    protected $unencoded_characters = array("/", "?");
11
12
    /**
13
     * Fragment constructor. Accepts a string representing a URI fragment. Construction will throw an exception if the
14
     * fragment is not a string.
15
     *
16
     * Construction accepts strings that have been percent-encoded as well as strings that have not been percent-encoded
17
     * and will encode invalid characters.
18
     *
19
     * Construction with a string that includes both encoded and decoded characters will be assumed to be an encoded
20
     * string, resulting in double-encoding.
21
     *
22
     * fragment    = *( pchar / "/" / "?" )
23
     * pchar       = unreserved / pct-encoded / sub-delims / ":" / "@"
24
     * unreserved  = ALPHA / DIGIT / "-" / "." / "_" / "~"
25
     * pct-encoded = "%" HEXDIG HEXDIG
26
     * sub-delims  = "!" / "$" / "&" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "="
27
     *
28
     * @see https://tools.ietf.org/html/rfc3986#appendix-A
29
     *
30
     * @throws \InvalidArgumentException
31
     *
32
     * @param string $fragment     A string representing a URI fragment
33
     */
34
    public function __construct($fragment)
35
    {
36
        $this->compileValidPattern();
37
        $this->compileUnencodedCharacters();
38
39
        if (!is_string($fragment)) {
40
            throw new \InvalidArgumentException("Fragment must be a string");
41
        } elseif (!self::isValid($fragment)) {
42
            $fragment = $this->encode($fragment);
43
        }
44
45
        $this->data = $fragment;
46
    }
47
48
    /**
49
     * Returns a string representation of the fragment formatted so that it can be compiled into a complete URI string
50
     * per the Uri object's string specification.
51
     *
52
     * If the fragment is empty, toUriString returns an empty string; if the fragment is not empty, toUriString returns
53
     * the fragment prefixed with a octothorpe(#).
54
     *
55
     * @see Uri::__toString
56
     *
57
     * @return string   A string representation of the fragment formatted for a complete URI string
58
     */
59
    public function toUriString()
60
    {
61
        $uri_fragment = $this->data;
62
63
        if (!empty($uri_fragment)) {
64
            $uri_fragment = "#" . $uri_fragment;
65
        }
66
67
        return $uri_fragment;
68
    }
69
70
    /**
71
     * Compiles a regexp pattern based on predefined patterns that define allowed characters for a fragment.
72
     */
73
    protected function compileValidPattern()
74
    {
75
        self::$valid_pattern = '/^([\/\?' .
76
            $this->unreserved_pattern .
77
            $this->sub_delims_pattern .
78
            $this->pchar_pattern .
79
            ']|' .                          //predefined patterns or percent-encoding
80
            $this->pct_encoded_pattern .
81
            ')*$/';
82
    }
83
84
    /**
85
     * Compiles an array of legal characters for a fragment based upon the RFC3986 specification:
86
     *
87
     * fragment = *( pchar / "/" / "?" )
88
     */
89
    protected function compileUnencodedCharacters()
90
    {
91
        $this->unencoded_characters = array_merge(
92
            $this->unencoded_characters,
93
            $this->unreserved_characters,
94
            $this->pchar_characters,
95
            $this->sub_delims_characters
96
        );
97
    }
98
}
99

src/Anshar/Http/UriParts/Query.php 1 location

@@ 8-98 (lines=91) @@
5
 * Class Query
6
 * @package Subreality\Dilmun\Anshar\Http\UriParts
7
 */
8
class Query extends AbstractUriPart
9
{
10
    protected $unencoded_characters = array("/", "?");
11
12
    /**
13
     * Query constructor. Accepts a string representing a URI query. Construction will throw an exception if the
14
     * query is not a string.
15
     *
16
     * Construction accepts strings that have been percent-encoded as well as strings that have not been percent-encoded
17
     * and will encode invalid characters.
18
     *
19
     * Construction with a string that includes both encoded and decoded characters will be assumed to be an encoded
20
     * string, resulting in double-encoding.
21
     *
22
     * query       = *( pchar / "/" / "?" )
23
     * pchar       = unreserved / pct-encoded / sub-delims / ":" / "@"
24
     * unreserved  = ALPHA / DIGIT / "-" / "." / "_" / "~"
25
     * pct-encoded = "%" HEXDIG HEXDIG
26
     * sub-delims  = "!" / "$" / "&" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "="
27
     *
28
     * @see https://tools.ietf.org/html/rfc3986#appendix-A
29
     *
30
     * @throws \InvalidArgumentException
31
     *
32
     * @param string $query     A string representing a URI query
33
     */
34
    public function __construct($query)
35
    {
36
        $this->compileValidPattern();
37
        $this->compileUnencodedCharacters();
38
39
        if (!is_string($query)) {
40
            throw new \InvalidArgumentException("Query must be a string");
41
        } elseif (!self::isValid($query)) {
42
            $query = $this->encode($query);
43
        }
44
45
        $this->data = $query;
46
    }
47
48
    /**
49
     * Returns a string representation of the query formatted so that it can be compiled into a complete URI string
50
     * per the Uri object's string specification.
51
     *
52
     * If the query is empty, toUriString returns an empty string; if the query is not empty, toUriString returns the
53
     * query prefixed with a question mark.
54
     *
55
     * @see Uri::__toString
56
     *
57
     * @return string   A string representation of the query formatted for a complete URI string
58
     */
59
    public function toUriString()
60
    {
61
        $uri_query = $this->data;
62
63
        if (!empty($uri_query)) {
64
            $uri_query = "?" . $uri_query;
65
        }
66
67
        return $uri_query;
68
    }
69
70
    /**
71
     * Compiles a regexp pattern based on predefined patterns that define allowed characters for a query.
72
     */
73
    protected function compileValidPattern()
74
    {
75
        self::$valid_pattern = '/^([\/\?' .
76
            $this->unreserved_pattern .
77
            $this->sub_delims_pattern .
78
            $this->pchar_pattern .
79
            ']|' .                          //predefined patterns or percent-encoding
80
            $this->pct_encoded_pattern .
81
            ')*$/';
82
    }
83
84
    /**
85
     * Compiles an array of legal characters for a query based upon the RFC3986 specification:
86
     *
87
     * query = *( pchar / "/" / "?" )
88
     */
89
    protected function compileUnencodedCharacters()
90
    {
91
        $this->unencoded_characters = array_merge(
92
            $this->unencoded_characters,
93
            $this->unreserved_characters,
94
            $this->pchar_characters,
95
            $this->sub_delims_characters
96
        );
97
    }
98
}
99