123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- <?php
- use Symfony\Component\HttpFoundation\Request;
- class Checks
- {
- public $filters = "filters";
- /**
- * Throws exception if not integer.
- * @param Request|array $all Contains array with query.
- * @param string $property Contains the name of property
- * @param boolean $required Check if property exists in request
- * @param mixed $default Default value.
- * @return string Return string or if the property not exists.
- * @throws Error Throws errors.
- */
- function string($all, $property, $required = true, $default = null)
- {
- if ($all != null) {
- if ($all instanceof Request) {
- $all = $all->query->all();
- }
- $key_exists = array_key_exists($property, $all);
- if (!$key_exists && $default != null) {
- $all[$property] = $default;
- $key_exists = true;
- }
- if (!$key_exists) {
- if ($required) {
- throw (new Errors())->errorRequired($property, "string");
- } else {
- // value not exists
- return null;
- }
- } else if (is_string($all[$property])) {
- return $all[$property];
- } else {
- throw (new Errors())->errorString($property);
- }
- } else {
- return null;
- }
- }
- /**
- * Throws exception if not integer.
- * @param Request|array $all Contains array with query.
- * @param string $property Contains the name of property
- * @param boolean $required Check if property exists in request
- * @param mixed $default Default value.
- * @return Integer Return integer or if the property not exists.
- * @throws Error Throws errors.
- */
- function integer($all, $property, $required = true, $default = null)
- {
- if ($all != null) {
- if ($all instanceof Request) {
- $all = $all->query->all();
- }
- $key_exists = array_key_exists($property, $all);
- if (!$key_exists && $default != null) {
- $all[$property] = $default;
- $key_exists = true;
- }
- if (!$key_exists) {
- if ($required) {
- throw (new Errors())->errorRequired($property, "integer");
- } else {
- // value not exists
- return null;
- }
- } else if (is_numeric($all[$property])) {
- return $all[$property];
- } else {
- throw (new Errors())->errorInteger($property);
- }
- } else {
- return null;
- }
- }
- /**
- * Throws exception if not integer.
- * @param string $property Contains the name of property
- * @param string $value Value for array key
- * @return string Return strings with format 'filters[$property]=$value or null.
- * @throws Error Throws errors.
- */
- public function filters($property, $value)
- {
- if ($value != null) {
- $value = "filters[$property]=$value";
- }
- return $value;
- }
- /**
- * Throws exception if not boolean.
- * @param Request|array $all Contains array with query.
- * @param string $property Contains the name of property
- * @param boolean $required Check if property exists in request
- * @param mixed $default Default value.
- * @return integer Return integer 0, 1 or null.
- * @throws Error Throws errors.
- */
- function boolean($all, $property, $required = true, $default = null)
- {
- if ($all != null) {
- if ($all instanceof Request) {
- $all = $all->query->all();
- }
- $key_exists = array_key_exists($property, $all);
- if (!$key_exists && $default != null) {
- $all[$property] = $default;
- $key_exists = true;
- }
- if (!$key_exists) {
- if ($required) {
- throw (new Errors())->errorRequired($property, "boolean");
- } else {
- // value not exists
- return null;
- }
- } else if (is_bool($all[$property]) || in_array(strtolower($all[$property]),['true','false','0','1'])) {
- $bool = filter_var( $all[$property], FILTER_VALIDATE_BOOLEAN );
- return (int)$bool;
- } else {
- throw (new Errors())->errorBoolean($property);
- }
- } else {
- return null;
- }
- }
- }
|