Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F48999108
D4077.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
5 KB
Referenced Files
None
Subscribers
None
D4077.diff
View Options
diff --git a/omnitools/src/HTTP/QueryEncoding.php b/omnitools/src/HTTP/QueryEncoding.php
new file mode 100644
--- /dev/null
+++ b/omnitools/src/HTTP/QueryEncoding.php
@@ -0,0 +1,25 @@
+<?php
+
+namespace Keruald\OmniTools\HTTP;
+
+/**
+ * Represents the encoding strategy for URL queries.
+ */
+enum QueryEncoding: int {
+ /**
+ * Encode the query using RFC 3986, but keep / intact as a separators.
+ * As such, everything will be encoded excepted ~ - _ . / characters.
+ */
+ case RFC3986_SLASH_EXCEPTED = 1;
+
+ /**
+ * Encode the query using RFC 3986, including the /.
+ * As such, everything will be encoded excepted ~ - _ . characters.
+ */
+ case RFC3986_PURE = 2;
+
+ /**
+ * Consider the query already encoded.
+ */
+ case AS_IS = 3;
+}
diff --git a/omnitools/src/HTTP/URL.php b/omnitools/src/HTTP/URL.php
--- a/omnitools/src/HTTP/URL.php
+++ b/omnitools/src/HTTP/URL.php
@@ -10,41 +10,23 @@
*/
class URL {
- ///
- /// Constants
- ///
-
- /**
- * Encode the query using RFC 3986, but keep / intact as a separators.
- * As such, everything will be encoded excepted ~ - _ . / characters.
- */
- const ENCODE_RFC3986_SLASH_EXCEPTED = 1;
-
- /**
- * Encode the query using RFC 3986, including the /.
- * As such, everything will be encoded excepted ~ - _ . characters.
- */
- const ENCODE_RFC3986_PURE = 2;
-
- /**
- * Consider the query already encoded.
- */
- const ENCODE_AS_IS = 3;
-
///
/// Private members
///
private string $url;
- private int $queryEncoding;
+ /**
+ * The encoding strategy to use for query parameters.
+ */
+ protected QueryEncoding $queryEncoding = QueryEncoding::RFC3986_SLASH_EXCEPTED;
///
/// Constructors
///
public function __construct ($url,
- $queryEncoding = self::ENCODE_RFC3986_SLASH_EXCEPTED) {
+ $queryEncoding = QueryEncoding::RFC3986_SLASH_EXCEPTED) {
$this->url = $url;
$this->queryEncoding = $queryEncoding;
}
@@ -61,7 +43,7 @@
*/
public static function compose (string $protocol, string $domain,
string $query,
- $queryEncoding = self::ENCODE_RFC3986_SLASH_EXCEPTED
+ $queryEncoding = QueryEncoding::RFC3986_SLASH_EXCEPTED
) : self {
return (new URL("", $queryEncoding))
->update($protocol, $domain, $query);
@@ -128,7 +110,7 @@
}
public function setQuery ($query,
- $encodeMode = self::ENCODE_RFC3986_SLASH_EXCEPTED
+ $encodeMode = QueryEncoding::RFC3986_SLASH_EXCEPTED
) : self {
$this->queryEncoding = $encodeMode;
$this->update($this->getProtocol(), $this->getDomain(), $query);
@@ -137,7 +119,7 @@
}
private function isRootQuery($query) : bool {
- return $this->queryEncoding !== self::ENCODE_RFC3986_PURE
+ return $this->queryEncoding !== QueryEncoding::RFC3986_PURE
&& $query !== ""
&& $query[0] === '/';
}
@@ -173,22 +155,16 @@
* @see URL::beautifyQuery() for the inverse operation (decode)
*/
public function normalizeQuery (string $query) : string {
- switch ($this->queryEncoding) {
- case self::ENCODE_RFC3986_SLASH_EXCEPTED:
- return (new OmniString($query))
- ->explode("/")
- ->map("rawurlencode")
- ->implode("/")
- ->__toString();
-
- case self::ENCODE_AS_IS:
- return $query;
-
- case self::ENCODE_RFC3986_PURE:
- return rawurlencode($query);
- }
+ return match ($this->queryEncoding) {
+ QueryEncoding::AS_IS => $query,
+
+ QueryEncoding::RFC3986_PURE => rawurlencode($query),
- throw new \Exception('Unexpected encoding value');
+ QueryEncoding::RFC3986_SLASH_EXCEPTED => (string)(new OmniString($query))
+ ->explode("/")
+ ->map("rawurlencode")
+ ->implode("/"),
+ };
}
/**
@@ -200,22 +176,17 @@
* @see URL::normalizeQuery() for the inverse operation (encode)
*/
public function beautifyQuery (string $query) : string {
- switch ($this->queryEncoding) {
- case self::ENCODE_RFC3986_SLASH_EXCEPTED:
- return (new OmniString($query))
- ->explode("/")
- ->map("rawurldecode")
- ->implode("/")
- ->__toString();
-
- case self::ENCODE_AS_IS:
- return $query;
-
- case self::ENCODE_RFC3986_PURE:
- return rawurldecode($query);
- }
+ return match ($this->queryEncoding) {
+ QueryEncoding::AS_IS => $query,
+
+ QueryEncoding::RFC3986_PURE => rawurldecode($query),
- throw new \Exception('Unexpected encoding value');
+ QueryEncoding::RFC3986_SLASH_EXCEPTED => (new OmniString($query))
+ ->explode("/")
+ ->map("rawurldecode")
+ ->implode("/")
+ ->__toString(),
+ };
}
/**
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Thu, Sep 10, 21:10 (17 h, 37 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
4077219
Default Alt Text
D4077.diff (5 KB)
Attached To
Mode
D4077: Refactor URL encode/decode
Attached
Detach File
Event Timeline
Log In to Comment