Skip to content

Commit

Permalink
Extracted a validation utility class
Browse files Browse the repository at this point in the history
  • Loading branch information
milos-pejanovic-devtech committed Jul 26, 2016
1 parent c5e2c33 commit 5e936c7
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 45 deletions.
10 changes: 5 additions & 5 deletions src/Common/Mapper/ObjectMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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());
}

Expand Down Expand Up @@ -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.');
}

Expand Down
4 changes: 2 additions & 2 deletions src/Common/Models/ModelClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

namespace Common\Models;
use Common\Validator\ObjectValidator;
use Common\Util\Validation;

class ModelClass {

Expand Down Expand Up @@ -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');
}

Expand Down
4 changes: 2 additions & 2 deletions src/Common/Models/ModelProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

namespace Common\Models;
use Common\Validator\ObjectValidator;
use Common\Util\Validation;

class ModelProperty {

Expand Down Expand Up @@ -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;
}

Expand Down
22 changes: 3 additions & 19 deletions src/Common/Models/ModelPropertyType.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
namespace Common\Models;


use Common\Util\Validation;

class ModelPropertyType {

/**
Expand Down Expand Up @@ -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';
}
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/Common/Traits/MappableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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();
Expand Down
49 changes: 49 additions & 0 deletions src/Common/Util/Validation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/**
* Created by PhpStorm.
* User: milos.pejanovic
* Date: 7/26/2016
* Time: 9:23 AM
*/
namespace Common\Util;

class Validation {

/**
* Checks for null, '', and empty arrays
* Casts objects to arrays before checking
* @param mixed $value
* @return bool
*/
public static function isEmpty($value) {
$isEmpty = false;
if(is_object($value)) {
$value = (array) $value;
}
if($value === array() || is_null($value) || $value === '') {
$isEmpty= true;
}

return $isEmpty;
}

/**
* Checks if a type is of a custom object or simple
* @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;
}
}
18 changes: 3 additions & 15 deletions src/Common/Validator/ObjectValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace Common\Validator;
use Common\Models\ModelClass;
use Common\Models\ModelProperty;
use Common\Util\Validation;

class ObjectValidator {

Expand Down Expand Up @@ -62,7 +63,7 @@ protected function validateProperty(ModelProperty $property, string $requiredTyp
*/
protected function validateCustomTypeValue(ModelProperty $property, string $requiredType) {
$propertyValue = $property->getPropertyValue();
if(!self::isValueEmpty($propertyValue)) {
if(!Validation::isEmpty($propertyValue)) {
if(is_array($propertyValue)) {
foreach ($propertyValue as $value) {
$validator = new ObjectValidator();
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
}

0 comments on commit 5e936c7

Please sign in to comment.