2020-02-09 23:02:19 +01:00
|
|
|
<?php
|
|
|
|
|
2022-11-18 18:06:46 +01:00
|
|
|
namespace Core\API\Parameter;
|
2020-02-09 23:02:19 +01:00
|
|
|
|
2020-04-03 15:56:04 +02:00
|
|
|
use DateTime;
|
|
|
|
|
2020-02-09 23:02:19 +01:00
|
|
|
class Parameter {
|
2023-01-05 22:47:17 +01:00
|
|
|
const TYPE_INT = 0;
|
|
|
|
const TYPE_FLOAT = 1;
|
|
|
|
const TYPE_BOOLEAN = 2;
|
|
|
|
const TYPE_STRING = 3;
|
|
|
|
const TYPE_DATE = 4;
|
|
|
|
const TYPE_TIME = 5;
|
2020-02-09 23:02:19 +01:00
|
|
|
const TYPE_DATE_TIME = 6;
|
2023-01-05 22:47:17 +01:00
|
|
|
const TYPE_EMAIL = 7;
|
2020-02-09 23:02:19 +01:00
|
|
|
|
|
|
|
// only internal access
|
2023-01-05 22:47:17 +01:00
|
|
|
const TYPE_RAW = 8;
|
2020-06-19 16:37:44 +02:00
|
|
|
|
2023-01-05 22:47:17 +01:00
|
|
|
const TYPE_ARRAY = 9;
|
|
|
|
const TYPE_MIXED = 10;
|
2020-02-09 23:02:19 +01:00
|
|
|
|
2024-04-22 12:41:15 +02:00
|
|
|
const names = ['Integer', 'Float', 'Boolean', 'String', 'Date', 'Time', 'DateTime', 'E-Mail', 'Raw', 'Array', 'Mixed'];
|
2020-02-09 23:02:19 +01:00
|
|
|
|
2022-02-20 16:53:26 +01:00
|
|
|
const DATE_FORMAT = "Y-m-d";
|
|
|
|
const TIME_FORMAT = "H:i:s";
|
|
|
|
const DATE_TIME_FORMAT = self::DATE_FORMAT . " " . self::TIME_FORMAT;
|
|
|
|
|
|
|
|
private $defaultValue;
|
|
|
|
|
2020-04-03 15:56:04 +02:00
|
|
|
public string $name;
|
2020-02-09 23:02:19 +01:00
|
|
|
public $value;
|
2021-04-02 21:58:06 +02:00
|
|
|
public bool $optional;
|
2020-04-03 15:56:04 +02:00
|
|
|
public int $type;
|
|
|
|
public string $typeName;
|
2023-01-05 22:47:17 +01:00
|
|
|
public ?array $choices;
|
2020-02-09 23:02:19 +01:00
|
|
|
|
2023-01-05 22:47:17 +01:00
|
|
|
public function __construct(string $name, int $type, bool $optional = FALSE, $defaultValue = NULL, ?array $choices = NULL) {
|
2020-02-09 23:02:19 +01:00
|
|
|
$this->name = $name;
|
|
|
|
$this->optional = $optional;
|
2022-02-20 16:53:26 +01:00
|
|
|
$this->defaultValue = $defaultValue;
|
2020-02-09 23:02:19 +01:00
|
|
|
$this->value = $defaultValue;
|
|
|
|
$this->type = $type;
|
2023-01-05 22:47:17 +01:00
|
|
|
$this->choices = $choices;
|
2020-02-09 23:02:19 +01:00
|
|
|
$this->typeName = $this->getTypeName();
|
|
|
|
}
|
|
|
|
|
2024-04-22 12:41:15 +02:00
|
|
|
public function reset(): void {
|
2022-02-20 16:53:26 +01:00
|
|
|
$this->value = $this->defaultValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getSwaggerTypeName(): string {
|
2024-04-22 12:41:15 +02:00
|
|
|
$typeName = strtolower(Parameter::names[$this->type] ?? "invalid");
|
2022-02-20 16:53:26 +01:00
|
|
|
if ($typeName === "mixed" || $typeName === "raw") {
|
|
|
|
return "object";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!in_array($typeName, ["array", "boolean", "integer", "number", "object", "string"])) {
|
|
|
|
return "string";
|
|
|
|
}
|
|
|
|
|
|
|
|
return $typeName;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getSwaggerFormat(): ?string {
|
2023-01-05 22:47:17 +01:00
|
|
|
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,
|
|
|
|
};
|
2022-02-20 16:53:26 +01:00
|
|
|
}
|
|
|
|
|
2021-04-02 21:58:06 +02:00
|
|
|
public function getTypeName(): string {
|
2023-01-05 22:47:17 +01:00
|
|
|
$typeName = Parameter::names[$this->type] ?? "INVALID";
|
|
|
|
if ($this->choices) {
|
2023-01-11 15:28:47 +01:00
|
|
|
$typeName .= ", choices=" . json_encode($this->choices);
|
|
|
|
}
|
|
|
|
|
|
|
|
$format = $this->getSwaggerFormat();
|
|
|
|
if ($format && $this->type !== self::TYPE_EMAIL) {
|
|
|
|
$typeName .= ", format='$format'";
|
2023-01-05 22:47:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $typeName;
|
2020-02-09 23:02:19 +01:00
|
|
|
}
|
|
|
|
|
2021-04-02 21:58:06 +02:00
|
|
|
public function toString(): string {
|
2020-02-09 23:02:19 +01:00
|
|
|
$typeName = Parameter::names[$this->type];
|
|
|
|
|
|
|
|
$str = "$typeName $this->name";
|
|
|
|
$defaultValue = (is_null($this->value) ? 'NULL' : $this->value);
|
2023-01-05 22:47:17 +01:00
|
|
|
if ($this->optional) {
|
2020-02-09 23:02:19 +01:00
|
|
|
$str = "[$str = $defaultValue]";
|
|
|
|
}
|
|
|
|
|
|
|
|
return $str;
|
|
|
|
}
|
|
|
|
|
2023-01-20 12:16:18 +01:00
|
|
|
public static function parseType(mixed $value, bool $strict = false): int {
|
2023-01-05 22:47:17 +01:00
|
|
|
if (is_array($value))
|
2020-02-09 23:02:19 +01:00
|
|
|
return Parameter::TYPE_ARRAY;
|
2023-01-20 12:16:18 +01:00
|
|
|
else if (is_int($value) || (!$strict && is_numeric($value) && intval($value) == $value))
|
2020-02-09 23:02:19 +01:00
|
|
|
return Parameter::TYPE_INT;
|
2023-01-20 12:16:18 +01:00
|
|
|
else if (is_float($value) || (!$strict && is_numeric($value) && floatval($value) == $value))
|
2020-02-09 23:02:19 +01:00
|
|
|
return Parameter::TYPE_FLOAT;
|
2023-01-20 12:16:18 +01:00
|
|
|
else if (is_bool($value) || (!$strict && ($value == "true" || $value == "false")))
|
2020-02-09 23:02:19 +01:00
|
|
|
return Parameter::TYPE_BOOLEAN;
|
2023-01-05 22:47:17 +01:00
|
|
|
else if (is_a($value, 'DateTime'))
|
2020-02-09 23:02:19 +01:00
|
|
|
return Parameter::TYPE_DATE_TIME;
|
2023-01-05 22:47:17 +01:00
|
|
|
else if ($value !== null && ($d = DateTime::createFromFormat(self::DATE_FORMAT, $value)) && $d->format(self::DATE_FORMAT) === $value)
|
2020-02-09 23:02:19 +01:00
|
|
|
return Parameter::TYPE_DATE;
|
2023-01-05 22:47:17 +01:00
|
|
|
else if ($value !== null && ($d = DateTime::createFromFormat(self::TIME_FORMAT, $value)) && $d->format(self::TIME_FORMAT) === $value)
|
2020-02-09 23:02:19 +01:00
|
|
|
return Parameter::TYPE_TIME;
|
2023-01-05 22:47:17 +01:00
|
|
|
else if ($value !== null && ($d = DateTime::createFromFormat(self::DATE_TIME_FORMAT, $value)) && $d->format(self::DATE_TIME_FORMAT) === $value)
|
2020-02-09 23:02:19 +01:00
|
|
|
return Parameter::TYPE_DATE_TIME;
|
|
|
|
else if (filter_var($value, FILTER_VALIDATE_EMAIL))
|
|
|
|
return Parameter::TYPE_EMAIL;
|
|
|
|
else
|
|
|
|
return Parameter::TYPE_STRING;
|
|
|
|
}
|
|
|
|
|
2021-04-02 21:58:06 +02:00
|
|
|
public function parseParam($value): bool {
|
2023-01-05 22:47:17 +01:00
|
|
|
|
|
|
|
$valid = false;
|
|
|
|
switch ($this->type) {
|
2020-02-09 23:02:19 +01:00
|
|
|
case Parameter::TYPE_INT:
|
2023-01-05 22:47:17 +01:00
|
|
|
if (is_numeric($value) && intval($value) == $value) {
|
2020-02-09 23:02:19 +01:00
|
|
|
$this->value = intval($value);
|
2023-01-05 22:47:17 +01:00
|
|
|
$valid = true;
|
2020-02-09 23:02:19 +01:00
|
|
|
}
|
2023-01-05 22:47:17 +01:00
|
|
|
break;
|
2020-02-09 23:02:19 +01:00
|
|
|
|
|
|
|
case Parameter::TYPE_FLOAT:
|
2023-01-05 22:47:17 +01:00
|
|
|
if (is_numeric($value) && (floatval($value) == $value || intval($value) == $value)) {
|
2020-02-09 23:02:19 +01:00
|
|
|
$this->value = floatval($value);
|
2023-01-05 22:47:17 +01:00
|
|
|
$valid = true;
|
2020-02-09 23:02:19 +01:00
|
|
|
}
|
2023-01-05 22:47:17 +01:00
|
|
|
break;
|
2020-02-09 23:02:19 +01:00
|
|
|
|
|
|
|
case Parameter::TYPE_BOOLEAN:
|
2023-01-05 22:47:17 +01:00
|
|
|
if (strcasecmp($value, 'true') === 0) {
|
2020-02-09 23:02:19 +01:00
|
|
|
$this->value = true;
|
2023-01-05 22:47:17 +01:00
|
|
|
$valid = true;
|
|
|
|
} else if (strcasecmp($value, 'false') === 0) {
|
2020-02-09 23:02:19 +01:00
|
|
|
$this->value = false;
|
2023-01-05 22:47:17 +01:00
|
|
|
$valid = true;
|
|
|
|
} else if (is_bool($value)) {
|
2020-02-09 23:02:19 +01:00
|
|
|
$this->value = (bool)$value;
|
2023-01-05 22:47:17 +01:00
|
|
|
$valid = true;
|
2020-02-09 23:02:19 +01:00
|
|
|
}
|
2023-01-05 22:47:17 +01:00
|
|
|
break;
|
2020-02-09 23:02:19 +01:00
|
|
|
|
|
|
|
case Parameter::TYPE_TIME:
|
2023-01-05 22:47:17 +01:00
|
|
|
case Parameter::TYPE_DATE:
|
2020-02-09 23:02:19 +01:00
|
|
|
case Parameter::TYPE_DATE_TIME:
|
2023-01-05 22:47:17 +01:00
|
|
|
if ($value instanceof DateTime) {
|
2020-02-09 23:02:19 +01:00
|
|
|
$this->value = $value;
|
2023-01-05 22:47:17 +01:00
|
|
|
$valid = true;
|
2023-01-15 00:32:17 +01:00
|
|
|
} else if (is_int($value) || (is_string($value) && preg_match("/^\d+$/", $value))) {
|
|
|
|
$this->value = (new \DateTime())->setTimestamp(intval($value));
|
|
|
|
$valid = true;
|
2020-02-09 23:02:19 +01:00
|
|
|
} else {
|
2023-01-05 22:47:17 +01:00
|
|
|
$format = $this->getFormat();
|
|
|
|
$d = DateTime::createFromFormat($format, $value);
|
|
|
|
if ($d && $d->format($format) === $value) {
|
2020-02-09 23:02:19 +01:00
|
|
|
$this->value = $d;
|
2023-01-05 22:47:17 +01:00
|
|
|
$valid = true;
|
2020-02-09 23:02:19 +01:00
|
|
|
}
|
|
|
|
}
|
2023-01-05 22:47:17 +01:00
|
|
|
break;
|
2020-02-09 23:02:19 +01:00
|
|
|
|
|
|
|
case Parameter::TYPE_EMAIL:
|
|
|
|
if (filter_var($value, FILTER_VALIDATE_EMAIL)) {
|
|
|
|
$this->value = $value;
|
2023-01-05 22:47:17 +01:00
|
|
|
$valid = true;
|
2020-02-09 23:02:19 +01:00
|
|
|
}
|
2023-01-05 22:47:17 +01:00
|
|
|
break;
|
2020-02-09 23:02:19 +01:00
|
|
|
|
|
|
|
case Parameter::TYPE_ARRAY:
|
2023-01-05 22:47:17 +01:00
|
|
|
if (is_array($value)) {
|
2020-02-09 23:02:19 +01:00
|
|
|
$this->value = $value;
|
2023-01-05 22:47:17 +01:00
|
|
|
$valid = true;
|
2020-02-09 23:02:19 +01:00
|
|
|
}
|
2023-01-05 22:47:17 +01:00
|
|
|
break;
|
2020-02-09 23:02:19 +01:00
|
|
|
|
|
|
|
default:
|
|
|
|
$this->value = $value;
|
2023-01-05 22:47:17 +01:00
|
|
|
$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;
|
2020-02-09 23:02:19 +01:00
|
|
|
}
|
|
|
|
}
|
2020-04-03 15:56:04 +02:00
|
|
|
}
|