summaryrefslogtreecommitdiff
path: root/src/URL.php
diff options
context:
space:
mode:
authorpolo <ordipolo@gmx.fr>2025-08-03 04:06:53 +0200
committerpolo <ordipolo@gmx.fr>2025-08-03 04:06:53 +0200
commit90673ef19133e037cf401773f4262ba3d7d050bf (patch)
tree0b1a23427399a521747fab7a91a22fb2af4c5a19 /src/URL.php
parent547d7feed68e89957f062b8ed9b988f28c5830ce (diff)
downloadcms-90673ef19133e037cf401773f4262ba3d7d050bf.zip
réorganisation 4: déplacement de fichiers, plus que des contrôleurs dans /src/controller
Diffstat (limited to 'src/URL.php')
-rw-r--r--src/URL.php88
1 files changed, 88 insertions, 0 deletions
diff --git a/src/URL.php b/src/URL.php
new file mode 100644
index 0000000..689332f
--- /dev/null
+++ b/src/URL.php
@@ -0,0 +1,88 @@
1<?php
2// src/URL.php
3
4declare(strict_types=1);
5
6class URL implements Stringable
7{
8 static private string $protocol = 'http://';
9 static private string $host = '';
10 static private string $port;
11 static private string $path = '/index.php';
12 private array $params;
13 private string $anchor = '';
14
15 // setters statiques
16 static public function setProtocol(string $protocol = 'http'): void
17 {
18 self::$protocol = $protocol === 'https' ? 'https://' : 'http://';
19 }
20 static public function setPort(int|string $port = 80): void
21 {
22 if((int)$port === 443){
23 self::$protocol = 'https://';
24 self::$port = '';
25 }
26 elseif((int)$port === 80){
27 self::$protocol = 'http://';
28 self::$port = '';
29 }
30 else{
31 self::$port = ':' . (string)$port;
32 }
33 }
34 static public function setHost(string $host): void
35 {
36 self::$host = $host;
37 }
38 static public function setPath(string $path): void
39 {
40 self::$path = '/' . ltrim($path, '/');
41 }
42
43 public function __construct(array $gets = [], string $anchor = ''){
44 $this->params = $gets;
45 if($anchor != ''){
46 $this->setAnchor($anchor);
47 }
48 }
49
50 //setters normaux
51 public function addParams(array $gets): void
52 {
53 // array_merge est préféré à l'opérateur d'union +, si une clé existe déjà la valeur est écrasée
54 $this->params = array_merge($this->params, $gets);
55 }
56 public function setAnchor(string $anchor = ''): void
57 {
58 if($anchor != ''){
59 $this->anchor = '#' . ltrim($anchor, '#');
60 }
61 else{
62 $this->anchor = '';
63 }
64 }
65
66 private function makeParams(): string
67 {
68 $output = '';
69 $first = true;
70
71 foreach($this->params as $key => $value) {
72 if($first){
73 $output .= '?';
74 $first = false;
75 }
76 else{
77 $output .= '&';
78 }
79 $output .= $key . '=' . $value;
80 }
81 return $output;
82 }
83
84 public function __toString(): string
85 {
86 return self::$protocol . self::$host . self::$port . self::$path . $this->makeParams() . $this->anchor;
87 }
88} \ No newline at end of file