diff --git a/src/Phalcon/ApiException.php b/src/Phalcon/ApiException.php index 47e44fc..9b99216 100644 --- a/src/Phalcon/ApiException.php +++ b/src/Phalcon/ApiException.php @@ -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; } } diff --git a/src/Phalcon/RequestParams.php b/src/Phalcon/RequestParams.php new file mode 100644 index 0000000..a384dea --- /dev/null +++ b/src/Phalcon/RequestParams.php @@ -0,0 +1,112 @@ +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); + } +}