MailtoImmutable::__construct()   A
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 14
nc 5
nop 1
dl 0
loc 22
rs 9.2222
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Keppler\Url\Scheme\Schemes\Mailto;
5
6
use Keppler\Url\Interfaces\Immutable\ImmutableSchemeInterface;
7
use Keppler\Url\Scheme\Schemes\AbstractImmutable;
8
use Keppler\Url\Scheme\Schemes\Mailto\Bags\MailtoImmutablePath;
9
use Keppler\Url\Scheme\Schemes\Mailto\Bags\MailtoImmutableQuery;
10
use Keppler\Url\Traits\Accessor;
11
12
/**
13
 * Note that the following class makes no assumption regarding url encoding
14
 * the mailto url is taken AS IS and will not be decoded or encoded
15
 * Url encoded strings WILL result in errors
16
 *
17
 * Default and accepted possible components of a mailto
18
 *
19
 *   mailtoURI    = "mailto:" [ to ] [ hfields ]
20
 *   to           = addr-spec *("," addr-spec )
21
 *   hfields      = "?" hfield *( "&" hfield )
22
 *   hfield       = hfname "=" hfvalue
23
 *   hfname       = *qchar
24
 *   hfvalue      = *qchar
25
 *   addr-spec    = local-part "@" domain
26
 *   local-part   = dot-atom-text / quoted-string
27
 *   domain       = dot-atom-text / "[" *dtext-no-obs "]"
28
 *   dtext-no-obs = %d33-90 / ; Printable US-ASCII
29
 *   %d94-126  ; characters not including
30
 *   ; "[", "]", or "\"
31
 *   qchar        = unreserved / pct-encoded / some-delims
32
 *   some-delims  = "!" / "$" / "'" / "(" / ")" / "*"
33
 *   / "+" / "," / ";" / ":" / "@"
34
 *
35
 * @example mailto:[email protected][email protected],[email protected]&[email protected],[email protected]&[email protected],[email protected]&subject=hello&body=welcome
36
 *
37
 * @see https://tools.ietf.org/html/rfc6068
38
 *
39
 * Class MailtoImmutable
40
 * @package Keppler\Url\Scheme\Schemes\MailtoImmutable
41
 */
42
class MailtoImmutable extends AbstractImmutable implements ImmutableSchemeInterface
43
{
44
    use Accessor;
45
46
    /**
47
     * The default scheme for this class
48
     *
49
     * @var string
50
     */
51
    const SCHEME = 'mailto';
52
53
    /**
54
     * @var MailtoImmutableQuery
55
     */
56
    private $queryBag;
57
58
    /**
59
     * @var MailtoImmutablePath
60
     */
61
    private $pathBag;
62
63
    /**
64
     * @var string
65
     */
66
    private $raw = '';
67
68
    /**
69
     * MailtoImmutable constructor.
70
     * @param $url
71
     */
72
    public function __construct(string $url)
73
    {
74
        $this->raw = $url;
75
76
        $parsedUrl = parse_url($url);
77
78
        if (MailtoImmutable::SCHEME !== $parsedUrl['scheme']) {
79
            throw new \InvalidArgumentException(sprintf('Invalid scheme provided for %s, expected "%s" got "%s"',
80
                MailtoImmutable::class, MailtoImmutable::SCHEME,
81
                $parsedUrl['scheme']));
82
        }
83
84
        if (isset($parsedUrl['query']) && !empty($parsedUrl['query'])) {
85
            $this->queryBag = new MailtoImmutableQuery($parsedUrl['query']);
86
        } else {
87
            $this->queryBag = new MailtoImmutableQuery();
88
        }
89
90
        if (isset($parsedUrl['path']) && !empty($parsedUrl['path'])) {
91
            $this->pathBag = new MailtoImmutablePath($parsedUrl['path']);
92
        } else {
93
            $this->pathBag = new MailtoImmutablePath();
94
        }
95
    }
96
97
/////////////////////////
98
/// GETTER FUNCTIONS  ///
99
////////////////////////
100
101
    /**
102
     * @return MailtoImmutablePath
103
     */
104
    public function getPathBag(): MailtoImmutablePath
105
    {
106
        return $this->pathBag;
107
    }
108
109
    /**
110
     * @return MailtoImmutableQuery
111
     */
112
    public function getQueryBag(): MailtoImmutableQuery
113
    {
114
        return $this->queryBag;
115
    }
116
117
/////////////////////////////////
118
/// INTERFACE IMPLEMENTATION  ///
119
/////////////////////////////////
120
121
    /**
122
     * @inheritdoc
123
     */
124
    public function all(): array
125
    {
126
        return [
127
            'scheme' => self::SCHEME,
128
            'path' => $this->pathBag->all(),
129
            'query' => $this->queryBag->all(),
130
        ];
131
    }
132
133
    /**
134
     * @inheritdoc
135
     */
136
    public function raw(): string
137
    {
138
        return $this->raw;
139
    }
140
141
    /**
142
     * @return string
143
     */
144
    public function getScheme(): string
145
    {
146
        return self::SCHEME;
147
    }
148
}
149