Merge pull request #1624 from TorstenDittmann/feat-response-models-new-attributes-torsten
feat(feat-response-models-new-attributes): introduce response type array
This commit is contained in:
commit
d305c7c442
|
@ -368,7 +368,24 @@ class Response extends SwooleResponse
|
|||
|
||||
foreach ($data[$key] as &$item) {
|
||||
if ($item instanceof Document) {
|
||||
$ruleType = (!\is_null($rule['getNestedType'])) ? $rule['getNestedType']($item) : $rule['type'];
|
||||
if (\is_array($rule['type'])) {
|
||||
foreach ($rule['type'] as $type) {
|
||||
$condition = false;
|
||||
foreach ($this->getModel($type)->conditions as $attribute => $val) {
|
||||
$condition = $item->getAttribute($attribute) === $val;
|
||||
if(!$condition) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ($condition) {
|
||||
$ruleType = $type;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$ruleType = $rule['type'];
|
||||
}
|
||||
|
||||
if (!array_key_exists($ruleType, $this->models)) {
|
||||
throw new Exception('Missing model for rule: '. $ruleType);
|
||||
}
|
||||
|
@ -377,7 +394,7 @@ class Response extends SwooleResponse
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
$output[$key] = $data[$key];
|
||||
}
|
||||
|
||||
|
|
|
@ -69,13 +69,11 @@ abstract class Model
|
|||
/**
|
||||
* Add a New Rule
|
||||
* If rule is an array of documents with varying models
|
||||
* Pass callable $getNestedType that accepts Document and returns the nested response type
|
||||
*
|
||||
* @param string $key
|
||||
* @param array $options
|
||||
* @param callable $getNestedType function(Document $value): string
|
||||
*/
|
||||
protected function addRule(string $key, array $options, callable $getNestedType = null): self
|
||||
protected function addRule(string $key, array $options): self
|
||||
{
|
||||
$this->rules[$key] = array_merge([
|
||||
'require' => true,
|
||||
|
@ -83,8 +81,7 @@ abstract class Model
|
|||
'description' => '',
|
||||
'default' => null,
|
||||
'example' => '',
|
||||
'array' => false,
|
||||
'getNestedType' => $getNestedType
|
||||
'array' => false
|
||||
], $options);
|
||||
|
||||
return $this;
|
||||
|
|
|
@ -44,6 +44,8 @@ class Attribute extends Model
|
|||
;
|
||||
}
|
||||
|
||||
public array $conditions = [];
|
||||
|
||||
/**
|
||||
* Get Name
|
||||
*
|
||||
|
|
|
@ -23,6 +23,10 @@ class AttributeBoolean extends Attribute
|
|||
;
|
||||
}
|
||||
|
||||
public array $conditions = [
|
||||
'type' => self::TYPE_BOOLEAN
|
||||
];
|
||||
|
||||
/**
|
||||
* Get Name
|
||||
*
|
||||
|
|
|
@ -31,6 +31,11 @@ class AttributeEmail extends Attribute
|
|||
;
|
||||
}
|
||||
|
||||
public array $conditions = [
|
||||
'type' => self::TYPE_STRING,
|
||||
'format' => \APP_DATABASE_ATTRIBUTE_EMAIL
|
||||
];
|
||||
|
||||
/**
|
||||
* Get Name
|
||||
*
|
||||
|
|
|
@ -39,6 +39,10 @@ class AttributeFloat extends Attribute
|
|||
;
|
||||
}
|
||||
|
||||
public array $conditions = [
|
||||
'type' => self::TYPE_FLOAT,
|
||||
];
|
||||
|
||||
/**
|
||||
* Get Name
|
||||
*
|
||||
|
|
|
@ -31,6 +31,11 @@ class AttributeIP extends Attribute
|
|||
;
|
||||
}
|
||||
|
||||
public array $conditions = [
|
||||
'type' => self::TYPE_STRING,
|
||||
'format' => \APP_DATABASE_ATTRIBUTE_IP
|
||||
];
|
||||
|
||||
/**
|
||||
* Get Name
|
||||
*
|
||||
|
|
|
@ -39,6 +39,10 @@ class AttributeInteger extends Attribute
|
|||
;
|
||||
}
|
||||
|
||||
public array $conditions = [
|
||||
'type' => self::TYPE_INTEGER,
|
||||
];
|
||||
|
||||
/**
|
||||
* Get Name *
|
||||
* @return string
|
||||
|
|
|
@ -18,24 +18,18 @@ class AttributeList extends Model
|
|||
'example' => 5,
|
||||
])
|
||||
->addRule('attributes', [
|
||||
'type' => Response::MODEL_ATTRIBUTE,
|
||||
'type' => [
|
||||
Response::MODEL_ATTRIBUTE_BOOLEAN,
|
||||
Response::MODEL_ATTRIBUTE_INTEGER,
|
||||
Response::MODEL_ATTRIBUTE_FLOAT,
|
||||
Response::MODEL_ATTRIBUTE_EMAIL,
|
||||
Response::MODEL_ATTRIBUTE_URL,
|
||||
Response::MODEL_ATTRIBUTE_IP,
|
||||
Response::MODEL_ATTRIBUTE_STRING // needs to be last, since its condition would dominate any other string attribute
|
||||
],
|
||||
'description' => 'List of attributes.',
|
||||
'default' => [],
|
||||
'array' => true,
|
||||
'getNestedType' => function(Document $attribute) {
|
||||
return match($attribute->getAttribute('type')) {
|
||||
self::TYPE_BOOLEAN => Response::MODEL_ATTRIBUTE_BOOLEAN,
|
||||
self::TYPE_INTEGER => Response::MODEL_ATTRIBUTE_INTEGER,
|
||||
self::TYPE_FLOAT => Response::MODEL_ATTRIBUTE_FLOAT,
|
||||
self::TYPE_STRING => match($attribute->getAttribute('format')) {
|
||||
APP_DATABASE_ATTRIBUTE_EMAIL => Response::MODEL_ATTRIBUTE_EMAIL,
|
||||
APP_DATABASE_ATTRIBUTE_IP => Response::MODEL_ATTRIBUTE_IP,
|
||||
APP_DATABASE_ATTRIBUTE_URL => Response::MODEL_ATTRIBUTE_URL,
|
||||
default => Response::MODEL_ATTRIBUTE_STRING,
|
||||
},
|
||||
default => Response::MODEL_ATTRIBUTE,
|
||||
};
|
||||
},
|
||||
'array' => true
|
||||
])
|
||||
;
|
||||
}
|
||||
|
|
|
@ -29,6 +29,10 @@ class AttributeString extends Attribute
|
|||
;
|
||||
}
|
||||
|
||||
public array $conditions = [
|
||||
'type' => self::TYPE_STRING,
|
||||
];
|
||||
|
||||
/**
|
||||
* Get Name
|
||||
*
|
||||
|
|
|
@ -31,6 +31,11 @@ class AttributeURL extends Attribute
|
|||
;
|
||||
}
|
||||
|
||||
public array $conditions = [
|
||||
'type' => self::TYPE_STRING,
|
||||
'format' => \APP_DATABASE_ATTRIBUTE_URL
|
||||
];
|
||||
|
||||
/**
|
||||
* Get Name
|
||||
*
|
||||
|
|
|
@ -45,25 +45,19 @@ class Collection extends Model
|
|||
'example' => 'document',
|
||||
])
|
||||
->addRule('attributes', [
|
||||
'type' => Response::MODEL_ATTRIBUTE,
|
||||
'type' => [
|
||||
Response::MODEL_ATTRIBUTE_BOOLEAN,
|
||||
Response::MODEL_ATTRIBUTE_INTEGER,
|
||||
Response::MODEL_ATTRIBUTE_FLOAT,
|
||||
Response::MODEL_ATTRIBUTE_EMAIL,
|
||||
Response::MODEL_ATTRIBUTE_URL,
|
||||
Response::MODEL_ATTRIBUTE_IP,
|
||||
Response::MODEL_ATTRIBUTE_STRING, // needs to be last, since its condition would dominate any other string attribute
|
||||
],
|
||||
'description' => 'Collection attributes.',
|
||||
'default' => [],
|
||||
'example' => new stdClass,
|
||||
'array' => true,
|
||||
'getNestedType' => function(Document $attribute) {
|
||||
return match($attribute->getAttribute('type')) {
|
||||
self::TYPE_BOOLEAN => Response::MODEL_ATTRIBUTE_BOOLEAN,
|
||||
self::TYPE_INTEGER => Response::MODEL_ATTRIBUTE_INTEGER,
|
||||
self::TYPE_FLOAT => Response::MODEL_ATTRIBUTE_FLOAT,
|
||||
self::TYPE_STRING => match($attribute->getAttribute('format')) {
|
||||
APP_DATABASE_ATTRIBUTE_EMAIL => Response::MODEL_ATTRIBUTE_EMAIL,
|
||||
APP_DATABASE_ATTRIBUTE_IP => Response::MODEL_ATTRIBUTE_IP,
|
||||
APP_DATABASE_ATTRIBUTE_URL => Response::MODEL_ATTRIBUTE_URL,
|
||||
default => Response::MODEL_ATTRIBUTE_STRING,
|
||||
},
|
||||
default => Response::MODEL_ATTRIBUTE,
|
||||
};
|
||||
},
|
||||
])
|
||||
->addRule('indexes', [
|
||||
'type' => Response::MODEL_INDEX,
|
||||
|
|
Loading…
Reference in a new issue