Skip to content

Commit

Permalink
cleaner validation output
Browse files Browse the repository at this point in the history
  • Loading branch information
dcarbone committed Dec 22, 2019
1 parent 3799961 commit e559686
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 17 deletions.
49 changes: 32 additions & 17 deletions template/validation/methods.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
/** @var \DCarbone\PHPFHIR\Definition\Type $type */

$typeNameConst = $type->getTypeNameConst(true);
$typeKind = $type->getKind();

// TODO: this is a quick and lazy initial implementation. Should improve this later...

Expand Down Expand Up @@ -51,29 +52,43 @@ public function _getValidationErrors()
<?php foreach ($type->getProperties()->getDirectSortedIterator() as $property) :
$propertyType = $property->getValueFHIRType();
if (null === $propertyType) :
continue;
endif;
if ($property->isCollection()) :
echo require_with(
if ($property->isCollection()) :
echo require_with(
PHPFHIR_TEMPLATE_VALIDATION_DIR . '/methods/collection_typed.php',
[
'property' => $property
'property' => $property
]
);
else :
echo require_with(
PHPFHIR_TEMPLATE_VALIDATION_DIR . '/methods/primitive.php',
[
'property' => $property
]
);
);
endif;
else :
echo require_with(
PHPFHIR_TEMPLATE_VALIDATION_DIR . '/methods/typed.php',
[
'property' => $property
]
);
endif; ?>
<?php endforeach;

if ($property->isCollection()) :
echo require_with(
PHPFHIR_TEMPLATE_VALIDATION_DIR . '/methods/collection_typed.php',
[
'property' => $property
]
);
else :
echo require_with(
PHPFHIR_TEMPLATE_VALIDATION_DIR . '/methods/typed.php',
[
'property' => $property
]
);
endif;
endif;
endforeach;
if (null !== $type->getParentType()) :
$ptype = $type;
while (null !== $ptype) :
foreach($ptype->getProperties()->getSortedIterator() as $property) : ?>
foreach($ptype->getProperties()->getDirectSortedIterator() as $property) : ?>
if (isset($validationRules[self::<?php echo $property->getFieldConstantName(); ?>])) {
$v = $this-><?php echo $property->getGetterName(); ?>();
foreach($validationRules[self::<?php echo $property->getFieldConstantName(); ?>] as $rule => $constraint) {
Expand All @@ -86,7 +101,7 @@ public function _getValidationErrors()
}
}
}
<?php endforeach;
<?php endforeach;
$ptype = $ptype->getParentType();
endwhile;
endif; ?>
Expand Down
35 changes: 35 additions & 0 deletions template/validation/methods/collection_primitive.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/*
* Copyright 2018-2019 Daniel Carbone ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/** @var \DCarbone\PHPFHIR\Definition\Property $property */

ob_start(); ?>
if (isset($validationRules[self::FIELD_VALUE]) && [] !== ($vs = $this-><?php echo $property->getGetterName(); ?>())) {
foreach($vs as $i => $v) {
$err = $this->_performValidation(<?php echo $property->getMemberOf()->getTypeNameConst(true); ?>, self::<?php echo $property->getFieldConstantName(); ?>, $rule, $constraint, $v);
if (null !== $err) {
$key = sprintf('%s.%d', self::<?php echo $property->getFieldConstantName(); ?>, $i);
if (!isset($errs[$key])) {
$errs[$key] = [];
}
$errs[$key][$rule] = $err;
}
}
}
<?php
return ob_get_clean();
34 changes: 34 additions & 0 deletions template/validation/methods/primitive.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/*
* Copyright 2018-2019 Daniel Carbone ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/** @var \DCarbone\PHPFHIR\Definition\Property $property */

ob_start(); ?>
if (isset($validationRules[self::FIELD_VALUE]) && null !== ($v = $this->getValue())) {
foreach($validationRules[self::FIELD_VALUE] as $rule => $constraint) {
$err = $this->_performValidation(<?php echo $property->getMemberOf()->getTypeNameConst(true); ?>, self::<?php echo $property->getFieldConstantName(); ?>, $rule, $constraint, $v);
if (null !== $err) {
if (!isset($errs[self::FIELD_VALUE])) {
$errs[self::FIELD_VALUE] = [];
}
$errs[self::FIELD_VALUE][$rule] = $err;
}
}
}
<?php
return ob_get_clean();

0 comments on commit e559686

Please sign in to comment.