diff --git a/src/Common/Mapper/ObjectMapper.php b/src/Common/Mapper/ObjectMapper.php index 19a112f..645a66e 100644 --- a/src/Common/Mapper/ObjectMapper.php +++ b/src/Common/Mapper/ObjectMapper.php @@ -9,7 +9,7 @@ namespace Common\Mapper; use Common\Models\ModelClass; use Common\Models\ModelPropertyType; -use Common\Validator\ObjectValidator; +use Common\Util\Validation; class ObjectMapper implements IModelMapper { @@ -104,13 +104,13 @@ public function unmap($model) { foreach($modelClass->getProperties() as $property) { $propertyKey = $property->getName(); $propertyValue = $property->getPropertyValue(); - if(ObjectValidator::isValueEmpty($propertyValue)) { + if(Validation::isEmpty($propertyValue)) { continue; } $unmappedObject->$propertyKey = $this->unmapValueByType($property->getType(), $propertyValue); } - if(!ObjectValidator::isValueEmpty($modelClass->getRootName())) { + if(!Validation::isEmpty($modelClass->getRootName())) { $unmappedObject = $this->addRootElement($unmappedObject, $modelClass->getRootName()); } @@ -200,10 +200,10 @@ protected function findSourceValueByName(string $name, $source, $defaultValue) { */ protected static function hasRoot($sourceObject, string $rootName) { $hasRoot = false; - if(!ObjectValidator::isValueEmpty($rootName) && isset($sourceObject->$rootName)) { + if(!Validation::isEmpty($rootName) && isset($sourceObject->$rootName)) { $hasRoot = true; } - if(!ObjectValidator::isValueEmpty($rootName) && !isset($sourceObject->$rootName)) { + if(!Validation::isEmpty($rootName) && !isset($sourceObject->$rootName)) { throw new ObjectMapperException('The source object has no ' . $rootName . ' root defined.'); } diff --git a/src/Common/Models/ModelClass.php b/src/Common/Models/ModelClass.php index 60ff4c3..59ec961 100644 --- a/src/Common/Models/ModelClass.php +++ b/src/Common/Models/ModelClass.php @@ -7,7 +7,7 @@ */ namespace Common\Models; -use Common\Validator\ObjectValidator; +use Common\Util\Validation; class ModelClass { @@ -47,7 +47,7 @@ public function __construct($customObject) { $this->namespace = $reflectionClass->getNamespaceName(); $this->rootName = ''; - if($this->docBlock->annotationExists('root') && !ObjectValidator::isValueEmpty($this->docBlock->getAnnotation('root'))) { + if($this->docBlock->annotationExists('root') && !Validation::isEmpty($this->docBlock->getAnnotation('root'))) { $this->rootName = $this->docBlock->getFirstAnnotation('root'); } diff --git a/src/Common/Models/ModelProperty.php b/src/Common/Models/ModelProperty.php index 6b8f70b..fdd06d6 100644 --- a/src/Common/Models/ModelProperty.php +++ b/src/Common/Models/ModelProperty.php @@ -7,7 +7,7 @@ */ namespace Common\Models; -use Common\Validator\ObjectValidator; +use Common\Util\Validation; class ModelProperty { @@ -108,7 +108,7 @@ public function getPropertyValue() { */ public function getName() { $name = $this->propertyName; - if(!ObjectValidator::isValueEmpty($this->annotatedName)) { + if(!Validation::isEmpty($this->annotatedName)) { $name = $this->annotatedName; } diff --git a/src/Common/Models/ModelPropertyType.php b/src/Common/Models/ModelPropertyType.php index c1b563e..f701729 100644 --- a/src/Common/Models/ModelPropertyType.php +++ b/src/Common/Models/ModelPropertyType.php @@ -9,6 +9,8 @@ namespace Common\Models; +use Common\Util\Validation; + class ModelPropertyType { /** @@ -42,7 +44,7 @@ public function __construct(string $propertyType, string $annotatedType, string $this->namespace = $namespace; $this->actualType = $this->annotatedType; - if(self::isCustomType($this->annotatedType)) { + if(Validation::isCustomType($this->annotatedType)) { $this->isModel = true; $this->actualType = 'object'; } @@ -51,24 +53,6 @@ public function __construct(string $propertyType, string $annotatedType, string } } - /** - * @param string $type - * @return bool - */ - public static function isCustomType(string $type) { - $result = true; - $simpleTypes = ['boolean', 'integer', 'double', 'string', 'array', 'object', - 'boolean[]', 'integer[]', 'double[]', 'string[]', '[]', 'object[]']; - foreach($simpleTypes as $simpleType) { - if($type == $simpleType) { - $result = false; - break; - } - } - - return $result; - } - /** * @return string * @throws \Exception diff --git a/src/Common/Traits/MappableTrait.php b/src/Common/Traits/MappableTrait.php index c71ed13..ab5746e 100644 --- a/src/Common/Traits/MappableTrait.php +++ b/src/Common/Traits/MappableTrait.php @@ -9,7 +9,7 @@ namespace Common\Traits; use Common\Mapper\ObjectMapperException; use Common\Mapper\ObjectMapper; -use Common\Validator\ObjectValidator; +use Common\Util\Validation; trait MappableTrait { @@ -41,7 +41,7 @@ public function mapFromJson(string $data) { * @throws ObjectMapperException */ public function mapFromObject($object) { - if(ObjectValidator::isValueEmpty((array) $object)) { + if(Validation::isEmpty((array) $object)) { throw new \InvalidArgumentException('Invalid json string supplied.'); } $mapper = new ObjectMapper(); diff --git a/src/Common/Util/Validation.php b/src/Common/Util/Validation.php new file mode 100644 index 0000000..1173c7f --- /dev/null +++ b/src/Common/Util/Validation.php @@ -0,0 +1,49 @@ +getPropertyValue(); - if(!self::isValueEmpty($propertyValue)) { + if(!Validation::isEmpty($propertyValue)) { if(is_array($propertyValue)) { foreach ($propertyValue as $value) { $validator = new ObjectValidator(); @@ -100,7 +101,7 @@ protected function validatePropertyType(ModelProperty $property, string $require */ protected function validateRequiredProperty(ModelProperty $property, string $requiredType) { $expectedRequired = $property->isRequired(); - $actualRequired = !self::isValueEmpty($property->getPropertyValue()); + $actualRequired = !Validation::isEmpty($property->getPropertyValue()); foreach($property->getRequiredTypes() as $expectedRequiredType) { if(($expectedRequiredType == '' || $requiredType == '') || $expectedRequiredType == $requiredType) { @@ -131,17 +132,4 @@ protected function assertRequiredProperty(bool $expected, bool $actual, ModelPro throw new ObjectValidatorException('Required property ' . $propertyData->getClassName() . '::' . $propertyData->getPropertyName() . ' not set.'); } } - - /** - * @param mixed $value - * @return bool - */ - public static function isValueEmpty($value) { - $isEmpty = false; - if($value === array() || is_null($value) || $value === '') { - $isEmpty= true; - } - - return $isEmpty; - } } \ No newline at end of file