Skip to content

Move item validation from Array to Collection #31

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 0 additions & 22 deletions src/Domain/Model/Type/ArrayDoc.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,4 @@
*/
class ArrayDoc extends CollectionDoc
{
/** @var TypeDoc|null */
private $itemValidation = null;

/**
* @param TypeDoc $itemValidation
*
* @return self
*/
public function setItemValidation(TypeDoc $itemValidation) : self
{
$this->itemValidation = $itemValidation;

return $this;
}

/**
* @return TypeDoc|null
*/
public function getItemValidation() : ?TypeDoc
{
return $this->itemValidation;
}
}
21 changes: 21 additions & 0 deletions src/Domain/Model/Type/CollectionDoc.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class CollectionDoc extends TypeDoc
private $allowExtraSibling = false;
/** @var bool */
private $allowMissingSibling = false;
/** @var TypeDoc|null */
private $itemValidation = null;

/**
* @param TypeDoc $doc
Expand Down Expand Up @@ -117,4 +119,23 @@ public function getSiblingList() : array
{
return $this->siblingList;
}
/**
* @param TypeDoc $itemValidation
*
* @return self
*/
public function setItemValidation(TypeDoc $itemValidation) : self
{
$this->itemValidation = $itemValidation;

return $this;
}

/**
* @return TypeDoc|null
*/
public function getItemValidation() : ?TypeDoc
{
return $this->itemValidation;
}
}
29 changes: 12 additions & 17 deletions src/Infra/Normalizer/TypeDocNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,8 @@ class TypeDocNormalizer
*/
public function normalize(TypeDoc $docObject) : array
{
$docArray = [];

$docArray = $this->appendIfNotNull($docArray, 'description', $docObject->getDescription());
$docArray = $this->appendIfNotNull($docArray, 'type', $this->normalizeSchemaType($docObject));

return $docArray
return $this->appendIfNotNull('description', $docObject->getDescription())
+ $this->appendIfNotNull('type', $this->normalizeSchemaType($docObject))
+ [
'nullable' => $docObject->isNullable(),
'required' => $docObject->isRequired(),
Expand Down Expand Up @@ -62,13 +58,13 @@ protected function appendMinMax(TypeDoc $docObject) : array
{
$docArray = [];
if ($docObject instanceof StringDoc) {
$docArray = $this->appendIfNotNull($docArray, 'minLength', $docObject->getMinLength());
$docArray = $this->appendIfNotNull($docArray, 'maxLength', $docObject->getMaxLength());
$docArray += $this->appendIfNotNull('minLength', $docObject->getMinLength());
$docArray += $this->appendIfNotNull('maxLength', $docObject->getMaxLength());
} elseif ($docObject instanceof CollectionDoc) {
$docArray = $this->appendIfNotNull($docArray, 'minItem', $docObject->getMinItem());
$docArray = $this->appendIfNotNull($docArray, 'maxItem', $docObject->getMaxItem());
$docArray += $this->appendIfNotNull('minItem', $docObject->getMinItem());
$docArray += $this->appendIfNotNull('maxItem', $docObject->getMaxItem());
} elseif ($docObject instanceof NumberDoc) {
return $this->appendNumberMinMax($docObject);
$docArray = $this->appendNumberMinMax($docObject);
}

return $docArray;
Expand Down Expand Up @@ -109,12 +105,11 @@ protected function appendCollectionDoc(TypeDoc $docObject) : array
*/
protected function appendMisc(TypeDoc $docObject) : array
{
$docArray = [];
$docArray = $this->appendIfNotNull($docArray, 'default', $docObject->getDefault());
$docArray = $this->appendIfNotNull($docArray, 'example', $docObject->getExample());
$docArray = $this->appendIfNotNull('default', $docObject->getDefault());
$docArray += $this->appendIfNotNull('example', $docObject->getExample());

if ($docObject instanceof StringDoc) {
$docArray = $this->appendIfNotNull($docArray, 'format', $docObject->getFormat());
$docArray += $this->appendIfNotNull('format', $docObject->getFormat());
} elseif ($docObject instanceof ArrayDoc && null !== $docObject->getItemValidation()) {
$docArray['item_validation'] = $this->normalize($docObject->getItemValidation());
}
Expand Down Expand Up @@ -188,13 +183,13 @@ private function appendNumberMinMax(NumberDoc $docObject) : array
}

/**
* @param array $docArray
* @param string $key
* @param mixed $value
* @param array $docArray
*
* @return array
*/
private function appendIfNotNull(array $docArray, string $key, $value) : array
private function appendIfNotNull(string $key, $value, array $docArray = []) : array
{
if (null !== $value) {
$docArray[$key] = $value;
Expand Down