1
0
Fork 0
mirror of synced 2024-06-01 18:39:57 +12:00

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:
kodumbeats 2021-09-15 10:28:39 -04:00 committed by GitHub
commit d305c7c442
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 73 additions and 38 deletions

View file

@ -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];
}

View file

@ -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;

View file

@ -44,6 +44,8 @@ class Attribute extends Model
;
}
public array $conditions = [];
/**
* Get Name
*

View file

@ -23,6 +23,10 @@ class AttributeBoolean extends Attribute
;
}
public array $conditions = [
'type' => self::TYPE_BOOLEAN
];
/**
* Get Name
*

View file

@ -31,6 +31,11 @@ class AttributeEmail extends Attribute
;
}
public array $conditions = [
'type' => self::TYPE_STRING,
'format' => \APP_DATABASE_ATTRIBUTE_EMAIL
];
/**
* Get Name
*

View file

@ -39,6 +39,10 @@ class AttributeFloat extends Attribute
;
}
public array $conditions = [
'type' => self::TYPE_FLOAT,
];
/**
* Get Name
*

View file

@ -31,6 +31,11 @@ class AttributeIP extends Attribute
;
}
public array $conditions = [
'type' => self::TYPE_STRING,
'format' => \APP_DATABASE_ATTRIBUTE_IP
];
/**
* Get Name
*

View file

@ -39,6 +39,10 @@ class AttributeInteger extends Attribute
;
}
public array $conditions = [
'type' => self::TYPE_INTEGER,
];
/**
* Get Name *
* @return string

View file

@ -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
])
;
}

View file

@ -29,6 +29,10 @@ class AttributeString extends Attribute
;
}
public array $conditions = [
'type' => self::TYPE_STRING,
];
/**
* Get Name
*

View file

@ -31,6 +31,11 @@ class AttributeURL extends Attribute
;
}
public array $conditions = [
'type' => self::TYPE_STRING,
'format' => \APP_DATABASE_ATTRIBUTE_URL
];
/**
* Get Name
*

View file

@ -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,