Integer + Regextypes, unit tests

This commit is contained in:
2024-04-22 12:41:15 +02:00
parent fcccf18644
commit a80b34e78f
7 changed files with 163 additions and 22 deletions

View File

@@ -0,0 +1,57 @@
<?php
namespace Core\API\Parameter;
class IntegerType extends Parameter {
public int $minValue;
public int $maxValue;
public function __construct(string $name, int $minValue = PHP_INT_MIN, int $maxValue = PHP_INT_MAX,
bool $optional = FALSE, ?int $defaultValue = NULL, ?array $choices = NULL) {
$this->minValue = $minValue;
$this->maxValue = $maxValue;
parent::__construct($name, Parameter::TYPE_INT, $optional, $defaultValue, $choices);
}
public function parseParam($value): bool {
if (!parent::parseParam($value)) {
return false;
}
$this->value = $value;
if ($this->value < $this->minValue || $this->value > $this->maxValue) {
return false;
}
return true;
}
public function getTypeName(): string {
$typeName = parent::getTypeName();
$hasMin = $this->minValue > PHP_INT_MIN;
$hasMax = $this->maxValue < PHP_INT_MAX;
if ($hasMin || $hasMax) {
if ($hasMin && $hasMax) {
$typeName .= " ($this->minValue - $this->maxValue)";
} else if ($hasMin) {
$typeName .= " (> $this->minValue)";
} else if ($hasMax) {
$typeName .= " (< $this->maxValue)";
}
}
return $typeName;
}
public function toString(): string {
$typeName = $this->getTypeName();
$str = "$typeName $this->name";
$defaultValue = (is_null($this->value) ? 'NULL' : $this->value);
if ($this->optional) {
$str = "[$str = $defaultValue]";
}
return $str;
}
}

View File

@@ -17,12 +17,10 @@ class Parameter {
// only internal access
const TYPE_RAW = 8;
// only json will work here I guess
// nope. also name[]=value
const TYPE_ARRAY = 9;
const TYPE_MIXED = 10;
const names = array('Integer', 'Float', 'Boolean', 'String', 'Date', 'Time', 'DateTime', 'E-Mail', 'Raw', 'Array', 'Mixed');
const names = ['Integer', 'Float', 'Boolean', 'String', 'Date', 'Time', 'DateTime', 'E-Mail', 'Raw', 'Array', 'Mixed'];
const DATE_FORMAT = "Y-m-d";
const TIME_FORMAT = "H:i:s";
@@ -47,12 +45,12 @@ class Parameter {
$this->typeName = $this->getTypeName();
}
public function reset() {
public function reset(): void {
$this->value = $this->defaultValue;
}
public function getSwaggerTypeName(): string {
$typeName = strtolower(($this->type >= 0 && $this->type < count(Parameter::names)) ? Parameter::names[$this->type] : "invalid");
$typeName = strtolower(Parameter::names[$this->type] ?? "invalid");
if ($typeName === "mixed" || $typeName === "raw") {
return "object";
}

View File

@@ -0,0 +1,36 @@
<?php
namespace Core\API\Parameter;
class RegexType extends StringType {
public string $pattern;
public function __construct(string $name, string $pattern, bool $optional = FALSE,
?string $defaultValue = NULL) {
$this->pattern = $pattern;
if (!startsWith($this->pattern, "/") || !endsWith($this->pattern, "/")) {
$this->pattern = "/" . $this->pattern . "/";
}
parent::__construct($name, -1, $optional, $defaultValue);
}
public function parseParam($value): bool {
if (!parent::parseParam($value)) {
return false;
}
$matches = [];
if (!preg_match($this->pattern, $this->value, $matches)) {
return false;
}
return strlen($matches[0]) === strlen($this->value);
}
public function getTypeName(): string {
return parent::getTypeName() . " ($this->pattern)";
}
}

View File

@@ -7,7 +7,8 @@ class StringType extends Parameter {
const UNLIMITED = -1;
public int $maxLength;
public function __construct(string $name, int $maxLength = self::UNLIMITED, bool $optional = FALSE, ?string $defaultValue = NULL, ?array $choices = NULL) {
public function __construct(string $name, int $maxLength = self::UNLIMITED, bool $optional = FALSE,
?string $defaultValue = NULL, ?array $choices = NULL) {
$this->maxLength = $maxLength;
parent::__construct($name, Parameter::TYPE_STRING, $optional, $defaultValue, $choices);
}