Skip to content

Commit

Permalink
3
Browse files Browse the repository at this point in the history
  • Loading branch information
Code8525 committed Jul 23, 2019
1 parent c9cf90f commit 23453b9
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/Phalcon/ApiException.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,36 @@

namespace Phalcon;

use Throwable;

/**
* Class ApiException
* @package Phalcon
*/
class ApiException
class ApiException extends Exception
{

public function __construct()
private $error;

/**
* ApiException constructor.
* @param string $error
* @param string $message
* @param int $code
* @param Throwable $previous
*/
public function __construct(string $error, string $message, int $code = 0, Throwable $previous = null)
{
$this->error = $error;
parent::__construct($message, $code, $previous);
}

/**
* @return string
*/
public function getError(): string
{
return $this->error;
}

}
112 changes: 112 additions & 0 deletions src/Phalcon/RequestParams.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php
declare(strict_types=1);

namespace Phalcon;

/**
* Class Request
*/
class RequestParams
{
static private $json_data = [];
static private $filter = null;

/**
* @return array
*/
static public function getJson(): array
{
if (count(self::$json_data) === 0)
self::$json_data = json_decode(file_get_contents("php://input"), true) ?? [];
return self::$json_data;
}

/**
* @return \Phalcon\Filter
*/
static public function getFilter()
{
if (self::$filter === null)
self::$filter = new \Phalcon\Filter();
return self::$filter;
}
//

/**
* @param string $name
* @return string
*/
static public function getAlphaNum(string $name): string
{
$val = self::get($name);
return (string)(self::getFilter()->sanitize($val, 'alphanum'));
}

/**
* 'Y' => array('year', '\d{4}'),
* 'y' => array('year', '\d{2}'),
* 'm' => array('month', '\d{2}'),
* 'n' => array('month', '\d{1,2}'),
* 'M' => array('month', '[A-Z][a-z]{3}'),
* 'F' => array('month', '[A-Z][a-z]{2,8}'),
* 'd' => array('day', '\d{2}'),
* 'j' => array('day', '\d{1,2}'),
* 'D' => array('day', '[A-Z][a-z]{2}'),
* 'l' => array('day', '[A-Z][a-z]{6,9}'),
* 'u' => array('hour', '\d{1,6}'),
* 'h' => array('hour', '\d{2}'),
* 'H' => array('hour', '\d{2}'),
* 'g' => array('hour', '\d{1,2}'),
* 'G' => array('hour', '\d{1,2}'),
* 'i' => array('minute', '\d{2}'),
* 's' => array('second', '\d{2}')
*
* d.m.Y H:i:s
*
* @param string $name
* @param string $format
* @return \DateTime|null
*/
static public function getDate(string $name, string $format = 'd.m.Y'): ?\DateTime
{
$date = \DateTime::createFromFormat($format, self::get($name));
if ($date === false)
return null;
return $date;
}

/**
* @param string $name
* @return mixed|null
*/
static public function get(string $name)
{
$val = null;
if (isset($_REQUEST[$name]))
$val = $_REQUEST[$name];
else if (isset(self::getJson()[$name]))
$val = self::getJson()[$name];
if ($val === null)
return $val;
return $val;
}

/**
* @param string $name
* @return int|null
*/
static public function getInit(string $name): ?int
{
$val = self::get($name);
return (int)(self::getFilter()->sanitize($val, 'int'));
}

/**
* @param string $name
* @return int
*/
static public function getInitNoNull(string $name): int
{
return (int)self::getInit($name);
}
}

0 comments on commit 23453b9

Please sign in to comment.