code cleanup
This commit is contained in:
@@ -8,14 +8,22 @@ class ArrayType extends Parameter {
|
||||
public int $elementType;
|
||||
public int $canBeOne;
|
||||
|
||||
public function __construct($name, $elementType = Parameter::TYPE_MIXED, $canBeOne=false, $optional = FALSE, $defaultValue = NULL) {
|
||||
/**
|
||||
* ArrayType constructor.
|
||||
* @param string $name the name of the parameter
|
||||
* @param int $elementType element type inside the array, for example, allow only integer values (Parameter::TYPE_INT)
|
||||
* @param bool $canBeOne true, if a single element can be passed inside the request (e.g. array=1 instead of array[]=1). Will be automatically casted to an array
|
||||
* @param bool $optional true if the parameter is optional
|
||||
* @param array|null $defaultValue the default value to use, if the parameter is not given
|
||||
*/
|
||||
public function __construct(string $name, int $elementType = Parameter::TYPE_MIXED, bool $canBeOne = false, bool $optional = FALSE, ?array $defaultValue = NULL) {
|
||||
$this->elementType = $elementType;
|
||||
$this->elementParameter = new Parameter('', $elementType);
|
||||
$this->canBeOne = $canBeOne;
|
||||
parent::__construct($name, Parameter::TYPE_ARRAY, $optional, $defaultValue);
|
||||
}
|
||||
|
||||
public function parseParam($value) {
|
||||
public function parseParam($value): bool {
|
||||
if(!is_array($value)) {
|
||||
if (!$this->canBeOne) {
|
||||
return false;
|
||||
@@ -38,12 +46,12 @@ class ArrayType extends Parameter {
|
||||
return true;
|
||||
}
|
||||
|
||||
public function getTypeName() {
|
||||
public function getTypeName(): string {
|
||||
$elementType = $this->elementParameter->getTypeName();
|
||||
return parent::getTypeName() . "($elementType)";
|
||||
}
|
||||
|
||||
public function toString() {
|
||||
public function toString(): string {
|
||||
$typeName = $this->getTypeName();
|
||||
$str = "$typeName $this->name";
|
||||
$defaultValue = (is_null($this->value) ? 'NULL' : (is_array($this->value) ? '[' . implode(",", $this->value) . ']' : $this->value));
|
||||
|
||||
@@ -26,11 +26,11 @@ class Parameter {
|
||||
|
||||
public string $name;
|
||||
public $value;
|
||||
public $optional;
|
||||
public bool $optional;
|
||||
public int $type;
|
||||
public string $typeName;
|
||||
|
||||
public function __construct($name, $type, $optional = FALSE, $defaultValue = NULL) {
|
||||
public function __construct(string $name, int $type, bool $optional = FALSE, $defaultValue = NULL) {
|
||||
$this->name = $name;
|
||||
$this->optional = $optional;
|
||||
$this->value = $defaultValue;
|
||||
@@ -38,11 +38,11 @@ class Parameter {
|
||||
$this->typeName = $this->getTypeName();
|
||||
}
|
||||
|
||||
public function getTypeName() {
|
||||
public function getTypeName(): string {
|
||||
return ($this->type >= 0 && $this->type < count(Parameter::names)) ? Parameter::names[$this->type] : "INVALID";
|
||||
}
|
||||
|
||||
public function toString() {
|
||||
public function toString(): string {
|
||||
$typeName = Parameter::names[$this->type];
|
||||
|
||||
$str = "$typeName $this->name";
|
||||
@@ -54,7 +54,7 @@ class Parameter {
|
||||
return $str;
|
||||
}
|
||||
|
||||
public static function parseType($value) {
|
||||
public static function parseType($value): int {
|
||||
if(is_array($value))
|
||||
return Parameter::TYPE_ARRAY;
|
||||
else if(is_numeric($value) && intval($value) == $value)
|
||||
@@ -77,7 +77,7 @@ class Parameter {
|
||||
return Parameter::TYPE_STRING;
|
||||
}
|
||||
|
||||
public function parseParam($value) {
|
||||
public function parseParam($value): bool {
|
||||
switch($this->type) {
|
||||
case Parameter::TYPE_INT:
|
||||
if(is_numeric($value) && intval($value) == $value) {
|
||||
|
||||
@@ -5,12 +5,12 @@ namespace Api\Parameter;
|
||||
class StringType extends Parameter {
|
||||
|
||||
public int $maxLength;
|
||||
public function __construct($name, $maxLength = -1, $optional = FALSE, $defaultValue = NULL) {
|
||||
public function __construct(string $name, int $maxLength = -1, bool $optional = FALSE, ?string $defaultValue = NULL) {
|
||||
$this->maxLength = $maxLength;
|
||||
parent::__construct($name, Parameter::TYPE_STRING, $optional, $defaultValue);
|
||||
}
|
||||
|
||||
public function parseParam($value) {
|
||||
public function parseParam($value): bool {
|
||||
if(!is_string($value)) {
|
||||
return false;
|
||||
}
|
||||
@@ -23,12 +23,12 @@ class StringType extends Parameter {
|
||||
return true;
|
||||
}
|
||||
|
||||
public function getTypeName() {
|
||||
public function getTypeName(): string {
|
||||
$maxLength = ($this->maxLength > 0 ? "($this->maxLength)" : "");
|
||||
return parent::getTypeName() . $maxLength;
|
||||
}
|
||||
|
||||
public function toString() {
|
||||
public function toString(): string {
|
||||
$typeName = $this->getTypeName();
|
||||
$str = "$typeName $this->name";
|
||||
$defaultValue = (is_null($this->value) ? 'NULL' : $this->value);
|
||||
|
||||
Reference in New Issue
Block a user