SQL expression rewrite, Pagination, some frontend stuff
This commit is contained in:
@@ -5,22 +5,22 @@ namespace Core\API\Parameter;
|
||||
use DateTime;
|
||||
|
||||
class Parameter {
|
||||
const TYPE_INT = 0;
|
||||
const TYPE_FLOAT = 1;
|
||||
const TYPE_BOOLEAN = 2;
|
||||
const TYPE_STRING = 3;
|
||||
const TYPE_DATE = 4;
|
||||
const TYPE_TIME = 5;
|
||||
const TYPE_INT = 0;
|
||||
const TYPE_FLOAT = 1;
|
||||
const TYPE_BOOLEAN = 2;
|
||||
const TYPE_STRING = 3;
|
||||
const TYPE_DATE = 4;
|
||||
const TYPE_TIME = 5;
|
||||
const TYPE_DATE_TIME = 6;
|
||||
const TYPE_EMAIL = 7;
|
||||
const TYPE_EMAIL = 7;
|
||||
|
||||
// only internal access
|
||||
const TYPE_RAW = 8;
|
||||
const TYPE_RAW = 8;
|
||||
|
||||
// only json will work here I guess
|
||||
// nope. also name[]=value
|
||||
const TYPE_ARRAY = 9;
|
||||
const TYPE_MIXED = 10;
|
||||
const TYPE_ARRAY = 9;
|
||||
const TYPE_MIXED = 10;
|
||||
|
||||
const names = array('Integer', 'Float', 'Boolean', 'String', 'Date', 'Time', 'DateTime', 'E-Mail', 'Raw', 'Array', 'Mixed');
|
||||
|
||||
@@ -35,13 +35,15 @@ class Parameter {
|
||||
public bool $optional;
|
||||
public int $type;
|
||||
public string $typeName;
|
||||
public ?array $choices;
|
||||
|
||||
public function __construct(string $name, int $type, bool $optional = FALSE, $defaultValue = NULL) {
|
||||
public function __construct(string $name, int $type, bool $optional = FALSE, $defaultValue = NULL, ?array $choices = NULL) {
|
||||
$this->name = $name;
|
||||
$this->optional = $optional;
|
||||
$this->defaultValue = $defaultValue;
|
||||
$this->value = $defaultValue;
|
||||
$this->type = $type;
|
||||
$this->choices = $choices;
|
||||
$this->typeName = $this->getTypeName();
|
||||
}
|
||||
|
||||
@@ -63,22 +65,22 @@ class Parameter {
|
||||
}
|
||||
|
||||
public function getSwaggerFormat(): ?string {
|
||||
switch ($this->type) {
|
||||
case self::TYPE_DATE:
|
||||
return self::DATE_FORMAT;
|
||||
case self::TYPE_TIME:
|
||||
return self::TIME_FORMAT;
|
||||
case self::TYPE_DATE_TIME:
|
||||
return self::DATE_TIME_FORMAT;
|
||||
case self::TYPE_EMAIL:
|
||||
return "email";
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
return match ($this->type) {
|
||||
self::TYPE_DATE => self::DATE_FORMAT,
|
||||
self::TYPE_TIME => self::TIME_FORMAT,
|
||||
self::TYPE_DATE_TIME => self::DATE_TIME_FORMAT,
|
||||
self::TYPE_EMAIL => "email",
|
||||
default => null,
|
||||
};
|
||||
}
|
||||
|
||||
public function getTypeName(): string {
|
||||
return ($this->type >= 0 && $this->type < count(Parameter::names)) ? Parameter::names[$this->type] : "INVALID";
|
||||
$typeName = Parameter::names[$this->type] ?? "INVALID";
|
||||
if ($this->choices) {
|
||||
$typeName .= ", choices: " . json_encode($this->choices);
|
||||
}
|
||||
|
||||
return $typeName;
|
||||
}
|
||||
|
||||
public function toString(): string {
|
||||
@@ -86,7 +88,7 @@ class Parameter {
|
||||
|
||||
$str = "$typeName $this->name";
|
||||
$defaultValue = (is_null($this->value) ? 'NULL' : $this->value);
|
||||
if($this->optional) {
|
||||
if ($this->optional) {
|
||||
$str = "[$str = $defaultValue]";
|
||||
}
|
||||
|
||||
@@ -94,21 +96,21 @@ class Parameter {
|
||||
}
|
||||
|
||||
public static function parseType($value): int {
|
||||
if(is_array($value))
|
||||
if (is_array($value))
|
||||
return Parameter::TYPE_ARRAY;
|
||||
else if(is_numeric($value) && intval($value) == $value)
|
||||
else if (is_numeric($value) && intval($value) == $value)
|
||||
return Parameter::TYPE_INT;
|
||||
else if(is_float($value) || (is_numeric($value) && floatval($value) == $value))
|
||||
else if (is_float($value) || (is_numeric($value) && floatval($value) == $value))
|
||||
return Parameter::TYPE_FLOAT;
|
||||
else if(is_bool($value) || $value == "true" || $value == "false")
|
||||
else if (is_bool($value) || $value == "true" || $value == "false")
|
||||
return Parameter::TYPE_BOOLEAN;
|
||||
else if(is_a($value, 'DateTime'))
|
||||
else if (is_a($value, 'DateTime'))
|
||||
return Parameter::TYPE_DATE_TIME;
|
||||
else if($value !== null && ($d = DateTime::createFromFormat(self::DATE_FORMAT, $value)) && $d->format(self::DATE_FORMAT) === $value)
|
||||
else if ($value !== null && ($d = DateTime::createFromFormat(self::DATE_FORMAT, $value)) && $d->format(self::DATE_FORMAT) === $value)
|
||||
return Parameter::TYPE_DATE;
|
||||
else if($value !== null && ($d = DateTime::createFromFormat(self::TIME_FORMAT, $value)) && $d->format(self::TIME_FORMAT) === $value)
|
||||
else if ($value !== null && ($d = DateTime::createFromFormat(self::TIME_FORMAT, $value)) && $d->format(self::TIME_FORMAT) === $value)
|
||||
return Parameter::TYPE_TIME;
|
||||
else if($value !== null && ($d = DateTime::createFromFormat(self::DATE_TIME_FORMAT, $value)) && $d->format(self::DATE_TIME_FORMAT) === $value)
|
||||
else if ($value !== null && ($d = DateTime::createFromFormat(self::DATE_TIME_FORMAT, $value)) && $d->format(self::DATE_TIME_FORMAT) === $value)
|
||||
return Parameter::TYPE_DATE_TIME;
|
||||
else if (filter_var($value, FILTER_VALIDATE_EMAIL))
|
||||
return Parameter::TYPE_EMAIL;
|
||||
@@ -117,88 +119,90 @@ class Parameter {
|
||||
}
|
||||
|
||||
public function parseParam($value): bool {
|
||||
switch($this->type) {
|
||||
|
||||
$valid = false;
|
||||
switch ($this->type) {
|
||||
case Parameter::TYPE_INT:
|
||||
if(is_numeric($value) && intval($value) == $value) {
|
||||
if (is_numeric($value) && intval($value) == $value) {
|
||||
$this->value = intval($value);
|
||||
return true;
|
||||
$valid = true;
|
||||
}
|
||||
return false;
|
||||
break;
|
||||
|
||||
case Parameter::TYPE_FLOAT:
|
||||
if(is_numeric($value) && (floatval($value) == $value || intval($value) == $value)) {
|
||||
if (is_numeric($value) && (floatval($value) == $value || intval($value) == $value)) {
|
||||
$this->value = floatval($value);
|
||||
return true;
|
||||
$valid = true;
|
||||
}
|
||||
return false;
|
||||
break;
|
||||
|
||||
case Parameter::TYPE_BOOLEAN:
|
||||
if(strcasecmp($value, 'true') === 0)
|
||||
if (strcasecmp($value, 'true') === 0) {
|
||||
$this->value = true;
|
||||
else if(strcasecmp($value, 'false') === 0)
|
||||
$valid = true;
|
||||
} else if (strcasecmp($value, 'false') === 0) {
|
||||
$this->value = false;
|
||||
else if(is_bool($value))
|
||||
$valid = true;
|
||||
} else if (is_bool($value)) {
|
||||
$this->value = (bool)$value;
|
||||
else
|
||||
return false;
|
||||
return true;
|
||||
|
||||
case Parameter::TYPE_DATE:
|
||||
if(is_a($value, "DateTime")) {
|
||||
$this->value = $value;
|
||||
return true;
|
||||
$valid = true;
|
||||
}
|
||||
|
||||
$d = DateTime::createFromFormat(self::DATE_FORMAT, $value);
|
||||
if($d && $d->format(self::DATE_FORMAT) === $value) {
|
||||
$this->value = $d;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
break;
|
||||
|
||||
case Parameter::TYPE_TIME:
|
||||
if(is_a($value, "DateTime")) {
|
||||
$this->value = $value;
|
||||
return true;
|
||||
}
|
||||
|
||||
$d = DateTime::createFromFormat(self::TIME_FORMAT, $value);
|
||||
if($d && $d->format(self::TIME_FORMAT) === $value) {
|
||||
$this->value = $d;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
||||
case Parameter::TYPE_DATE:
|
||||
case Parameter::TYPE_DATE_TIME:
|
||||
if(is_a($value, 'DateTime')) {
|
||||
if ($value instanceof DateTime) {
|
||||
$this->value = $value;
|
||||
return true;
|
||||
$valid = true;
|
||||
} else {
|
||||
$d = DateTime::createFromFormat(self::DATE_TIME_FORMAT, $value);
|
||||
if($d && $d->format(self::DATE_TIME_FORMAT) === $value) {
|
||||
$format = $this->getFormat();
|
||||
$d = DateTime::createFromFormat($format, $value);
|
||||
if ($d && $d->format($format) === $value) {
|
||||
$this->value = $d;
|
||||
return true;
|
||||
$valid = true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
break;
|
||||
|
||||
case Parameter::TYPE_EMAIL:
|
||||
if (filter_var($value, FILTER_VALIDATE_EMAIL)) {
|
||||
$this->value = $value;
|
||||
return true;
|
||||
$valid = true;
|
||||
}
|
||||
return false;
|
||||
break;
|
||||
|
||||
case Parameter::TYPE_ARRAY:
|
||||
if(is_array($value)) {
|
||||
if (is_array($value)) {
|
||||
$this->value = $value;
|
||||
return true;
|
||||
$valid = true;
|
||||
}
|
||||
return false;
|
||||
break;
|
||||
|
||||
default:
|
||||
$this->value = $value;
|
||||
return true;
|
||||
$valid = true;
|
||||
break;
|
||||
}
|
||||
|
||||
if ($valid && $this->choices) {
|
||||
if (!in_array($this->value, $this->choices)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return $valid;
|
||||
}
|
||||
|
||||
private function getFormat(): ?string {
|
||||
if ($this->type === self::TYPE_TIME) {
|
||||
return self::TIME_FORMAT;
|
||||
} else if ($this->type === self::TYPE_DATE) {
|
||||
return self::DATE_FORMAT;
|
||||
} else if ($this->type === self::TYPE_DATE_TIME) {
|
||||
return self::DATE_TIME_FORMAT;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,9 +5,9 @@ namespace Core\API\Parameter;
|
||||
class StringType extends Parameter {
|
||||
|
||||
public int $maxLength;
|
||||
public function __construct(string $name, int $maxLength = -1, bool $optional = FALSE, ?string $defaultValue = NULL) {
|
||||
public function __construct(string $name, int $maxLength = -1, bool $optional = FALSE, ?string $defaultValue = NULL, ?array $choices = NULL) {
|
||||
$this->maxLength = $maxLength;
|
||||
parent::__construct($name, Parameter::TYPE_STRING, $optional, $defaultValue);
|
||||
parent::__construct($name, Parameter::TYPE_STRING, $optional, $defaultValue, $choices);
|
||||
}
|
||||
|
||||
public function parseParam($value): bool {
|
||||
|
||||
Reference in New Issue
Block a user