diff options
| author | polo-pc-greta <ordipolo@gmx.fr> | 2025-03-27 10:13:03 +0100 |
|---|---|---|
| committer | polo-pc-greta <ordipolo@gmx.fr> | 2025-03-27 10:13:03 +0100 |
| commit | df3612ed7e6691530503f79483d2fdbc032d01b8 (patch) | |
| tree | 56d1c68fdc8625f5dad1937a654299d45142c79a /src/controller/URL.php | |
| download | cms-df3612ed7e6691530503f79483d2fdbc032d01b8.tar.gz cms-df3612ed7e6691530503f79483d2fdbc032d01b8.tar.bz2 cms-df3612ed7e6691530503f79483d2fdbc032d01b8.zip | |
mise en ligne github
Diffstat (limited to 'src/controller/URL.php')
| -rw-r--r-- | src/controller/URL.php | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/src/controller/URL.php b/src/controller/URL.php new file mode 100644 index 0000000..956d85d --- /dev/null +++ b/src/controller/URL.php | |||
| @@ -0,0 +1,88 @@ | |||
| 1 | <?php | ||
| 2 | // src/controller/URL.php | ||
| 3 | |||
| 4 | declare(strict_types=1); | ||
| 5 | |||
| 6 | class URL implements Stringable | ||
| 7 | { | ||
| 8 | static private string $protocol = 'http://'; | ||
| 9 | static private string $host = 'localhost'; | ||
| 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 | ||
