You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The `Pdp\Domain` object is an immutable value object representing a valid domain name. This object let's you access the domain properties.
65
+
66
+
~~~php
67
+
public function Domain::__toString(): string
68
+
public function Domain::getContent(): ?string
69
+
public function Domain::getLabel(int $key): ?string
70
+
public function Domain::keys(?string $label): int[]
71
+
~~~
72
+
73
+
*The getter methods return normalized and lowercased domain labels or `null` if no value was found for a particular domain part.*
74
+
75
+
The `Pdp\Domain` object also implements PHP's `Countable`, `IteratorAggregate` and `JsonSerializable` interfaces to ease retrieving the domain labels and properties.
76
+
77
+
Once you have a `Pdp\Domain` object you can also modify its content using the following methods:
78
+
79
+
~~~php
80
+
public function Domain::toAscii(): Domain
81
+
public function Domain::toUnicode(): Domain
82
+
public function Domain::withLabel(int $key, $label): Domain
83
+
public function Domain::withoutLabel(int $key, int ...$keys): Domain
84
+
~~~
85
+
86
+
~~~php
87
+
<?php
88
+
89
+
use Pdp\Domain;
90
+
91
+
$domain = new Domain('www.bébé.be');
92
+
$domain->getContent(); // 'www.bébé.be'
93
+
echo $domain->toAscii(); // 'www.xn--bb-bjab.be'
94
+
echo $domain->toUnicode(); // 'www.bébé.be'
95
+
$newDomain = $domain
96
+
->withLabel(-1, 'shop')
97
+
->withLabel(0, 'com')
98
+
->withoutLabel(1)
99
+
;
100
+
echo $domain; // 'www.bébé.be'
101
+
echo $newDomain // 'shop.com'
102
+
~~~
103
+
104
+
Because the `Pdp\Domain` object is immutable:
105
+
106
+
- If the method change any of the current object property, a new object is returned.
107
+
- If a modification is not possible a `Pdp\Exception` exception is thrown.
108
+
109
+
**WARNING: URI and URL accept registered name which encompass domain name. Therefore, some URI host are invalid domain name and will trigger an exception if you try to instantiate a `Pdp\Domain` with them.**
Using the above code you can parse any valid domain name.
138
+
Using the above code you can parse, validate and resolve a domain name and its public suffix status against a Public suffix list.
75
139
76
-
The returned `Pdp\Domain` object is an immutable value object representing a valid domain name. This object let's you access the domain properties.
140
+
The `Pdp\Domain` object can tell whether a public suffix can be attached to it using the `Pdp\Domain::isResolvable` method.
77
141
78
142
~~~php
79
-
public function Domain::__toString(): string
80
-
public function Domain::getContent(): ?string
81
-
public function Domain::getLabel(int $key): ?string
82
-
public function Domain::keys(?string $label): int[]
83
-
public function Domain::toAscii(): self
84
-
public function Domain::toUnicode(): self
85
-
public function Domain::isResolvable(): bool;
143
+
<?php
144
+
145
+
use Pdp\Domain;
146
+
147
+
$domain = new Domain('www.ExAmple.com');
148
+
$domain->isResolvable(); //returns true;
149
+
150
+
$altDomain = new Domain('localhost');
151
+
$altDomain->isResolvable(); //returns false;
86
152
~~~
87
153
88
-
as well as its public suffix informations attached to it by the `Pdp\Rules::resolve` method.
154
+
Furthermore, the `Pdp\Domain` object let's you access and modify its related public suffix properties using the following methods:
89
155
90
156
~~~php
91
157
public function Domain::getPublicSuffix(): ?string
@@ -94,29 +160,20 @@ public function Domain::getSubDomain(); ?string
94
160
public function Domain::isKnown(): bool;
95
161
public function Domain::isICANN(): bool;
96
162
public function Domain::isPrivate(): bool;
97
-
~~~
98
-
99
-
*The getter methods return normalized and lowercased domain labels or `null` if no value was found for a particular domain part.*
100
-
101
-
The `Pdp\Domain` object also implements PHP's `Countable`, `IteratorAggregate` and `JsonSerializable` interfaces to ease retrieving the domain labels and properties.
102
-
103
-
Once you have a `Pdp\Domain` object you can also modify its content using the following methods:
104
-
105
-
~~~php
106
-
public function Domain::withLabel(int $key, $label): self
107
-
public function Domain::withoutLabel(int $key, int ...$keys): self
108
163
public function Domain::resolve($publicSuffix): self
109
164
public function Domain::withPublicSuffix($publicSuffix): self
110
165
public function Domain::withSubDomain($subDomain): self
111
166
~~~
112
167
113
-
Because the `Pdp\Domain` object is immutable:
114
-
115
-
- If the method change any of the current object property, a new object is returned.
116
-
- If a modification is not possible a `Pdp\Exception` exception is thrown.
168
+
Here's a more complex example:
117
169
118
170
~~~php
119
171
$domain = $rules->resolve('www.bbc.co.uk');
172
+
$domain->getContent(); //returns 'www.bbc.co.uk';
173
+
$domain->getPublicSuffix(); //returns 'co.uk';
174
+
$domain->isKnown(); //return true;
175
+
$domain->isICANN(); //return true;
176
+
120
177
$newDomain = $domain
121
178
->withPublicSuffix('com')
122
179
->withSubDomain('shop')
@@ -149,7 +206,7 @@ The `Pdp\PublicSuffix` is an immutable value object which exposes the same metho
149
206
- all the modifying methods, **`toAscii` and `toUnicode` excluded**.
0 commit comments