Skip to content

Commit

Permalink
[feat] Added get function and some info
Browse files Browse the repository at this point in the history
  • Loading branch information
MrAnyx committed Oct 16, 2020
1 parent 17132d6 commit ebc1242
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 12 deletions.
17 changes: 17 additions & 0 deletions src/Exception/ParserException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace TimePHP\UrlParser\Exception;

class ParserException extends \Exception {

public function __construct($message = null, $code = 6000) {
if (!$message) {
throw new $this('Unknown ' . get_class($this));
}
parent::__construct($message, $code);
}

public function __toString(): string {
return __CLASS__ . "[{$this->code}] : {$this->message} at line {$this->line}";
}
}
78 changes: 66 additions & 12 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

namespace TimePHP\UrlParser;

class Parser {

use TimePHP\UrlParser\Exception\ParserException;

class Parser
{


/**
* Url pass un parameter
Expand All @@ -26,8 +29,9 @@ class Parser {
*/
private $domain;

public function __construct(){
$this->url = $_SERVER["PATH_INFO"];
public function __construct()
{
$this->url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";;
$this->method = $_SERVER["REQUEST_METHOD"];
$this->domain = $_SERVER["SERVER_NAME"];
}
Expand All @@ -37,26 +41,76 @@ public function __construct(){
*
* @return string
*/
public function getUrl(): string {
public function getUrl(): string
{
return $this->url;
}


/**
* Return the current request method (GET, POST, HEAD, PUT)
*
* @return string
*/
public function getMethod(): string {
public function getMethod(): string
{
return $this->method;
}

public function getDomain(): string {
/**
* Return the domain
*
* @return string
*/
public function getDomain(): string
{
return $this->domain;
}

/**
* Undocumented function
*
* @return mixed|null
*/
public function getInfo(string $index = null)
{
$info = parse_url($this->getUrl());
if ($index === null) {
return $info;
} else {
if (array_key_exists($index, $info)) {
return $info[$index];
} else {
throw new ParserException("Can not find the index $index", 6001);
}
}
}




}
/**
* Return a value from $_GET for a specific index
*
* @param string $index
* @return mixed|null
*/
public function get(string $index = null)
{
if ($index === null) {
$list = [];
foreach($_GET as $key => $value){
if(filter_var($value, FILTER_VALIDATE_INT)) $list[$key] = (int)$value;
elseif(filter_var($value, FILTER_VALIDATE_FLOAT)) $list[$key] = (float)$value;
elseif(filter_var($value, FILTER_VALIDATE_BOOLEAN)) $list[$key] = (bool)$value;
else $list[$key] = $value;
}
return $list;
} else {
if(array_key_exists($index, $_GET)) {
if(filter_var($_GET[$index], FILTER_VALIDATE_INT)) return (int)$_GET[$index];
elseif(filter_var($_GET[$index], FILTER_VALIDATE_FLOAT)) return (float)$_GET[$index];
elseif(filter_var($_GET[$index], FILTER_VALIDATE_BOOLEAN)) return (bool)$_GET[$index];
else return $_GET[$index];
} else {
throw new ParserException("Can not find the index $index", 6001);
}
}
}
}

0 comments on commit ebc1242

Please sign in to comment.