Skip to content
This repository has been archived by the owner on Mar 28, 2023. It is now read-only.

Commit

Permalink
Refactored Validation class, moved some functionality to traits.
Browse files Browse the repository at this point in the history
  • Loading branch information
grz-gajda committed Nov 30, 2015
1 parent 3962bac commit 3efda9b
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 26 deletions.
21 changes: 19 additions & 2 deletions src/Api/Filters/FilterCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,30 @@ trait FilterCheck
protected function isEnabledFilter($field, array $filters, $object = null)
{
if (array_key_exists($field, $filters) === false) {
return ($object === null) ? false : $object;
return $this->returnObjectOrBool($object);
}

if ($filters[$field] === false) {
return ($object === null) ? false : $object;
return $this->returnObjectOrBool($object);
}

return true;
}

/**
* Check if object is null, if yes: return bool, if not:
* return object.
*
* @param mixed $object Instance of called class.
*
* @return bool|null
*/
protected function returnObjectOrBool($object)
{
if ($object === null) {
return false;
}

return $object;
}
}
113 changes: 113 additions & 0 deletions src/Api/Filters/FilterValidation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php

namespace ComicVine\Api\Filters;

/**
* Trait FilterCheck
*
* @package grzgajda/comicvine-api
* @author Grzegorz Gajda <[email protected]>
*/
trait FilterValidation
{
/**
* Check if input is integer and stay
* in range between $min and $max.
*
* @param mixed $input Input field
* @param int $min Min range of input
* @param int|string $max Max range of input
*
* @return bool
*/
public function isIntAndBetween($input, $min, $max = "")
{
if (is_int($input) === false) {
return false;
}

if ($input < $min) {
return false;
}

if ($input > $max && is_int($max) === true) {
return false;
}

return true;
}

/**
* Check if key or value is not of any described types.
*
* @param string $key Value of key
* @param string|array $keyParams Not-Types of key
* @param string $value Value of value
* @param string|array $valueParams Not-Types of value
*
* @return bool
*/
public function isKeyAndValueAre($key, $keyParams, $value, $valueParams)
{
if ($this->isParamOfType($key, $keyParams) === false) {
return false;
}

if ($this->isParamOfType($value, $valueParams) === false) {
return false;
}

return true;
}

/**
* Check if values is any of these types.
*
* @param string $param Value
* @param string|array $types Type or array of types
*
* @return bool
*/
public function isParamOfType($param, $types)
{
if (is_array($types) === true) {
return $this->isParamOfTypeMultiple($param, $types);
}

return $this->isParamOfTypeSingle($param, $types);
}

/**
* Check if value is any of these types.
*
* @param string $param Value to check
* @param array $types List of types
*
* @return bool
*/
public function isParamOfTypeMultiple($param, $types)
{
array_walk($types, function(&$type) use ($param) {
$type = $this->isParamOfTypeSingle($param, $type);
});

if (in_array(true, $types) === true) {
return true;
}

return false;
}

/**
* Check if value is not that type.
*
* @param string $param Value
* @param string $type Type
*
* @return bool
*/
public function isParamOfTypeSingle($param, $type)
{
return call_user_func("is_$type", $param);
}
}
27 changes: 6 additions & 21 deletions src/Api/Validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace ComicVine\Api;

use ComicVine\Api\Filters\FilterCheck;
use ComicVine\Api\Filters\FilterValidation;

/**
* Check validation of inputs for ControllerQuery.
Expand All @@ -15,6 +16,7 @@
class Validation
{
use FilterCheck;
use FilterValidation;

/**
* Mock for enabled filters.
Expand Down Expand Up @@ -75,10 +77,7 @@ protected function validFieldList($input)
}

foreach ($input as $key => $value) {
if (is_int($key) === false) {
return false;
}
if (is_string($value) === false) {
if ($this->isKeyAndValueAre($key, 'int', $value, 'string') === false) {
return false;
}
}
Expand All @@ -95,11 +94,7 @@ protected function validFieldList($input)
*/
protected function validLimit($input)
{
if (is_int($input) === false) {
return false;
}

if ($input < 0 || $input > 100) {
if ($this->isIntAndBetween($input, 0, 100) === false) {
return false;
}

Expand All @@ -115,11 +110,7 @@ protected function validLimit($input)
*/
protected function validOffset($input)
{
if (is_int($input) === false) {
return false;
}

if ($input < 0) {
if ($this->isIntAndBetween($input, 0) === false) {
return false;
}

Expand All @@ -140,13 +131,7 @@ protected function validFilter($input)
}

foreach ($input as $key => $value) {
if (is_string($key) === false) {
return false;
}
if (is_array($value) === true) {
return false;
}
if (is_object($value) === true) {
if ($this->isKeyAndValueAre($key, 'string', $value, ['string', 'int', 'float']) === false) {
return false;
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/ComicVine.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ class ComicVine
{

/**
* RegisterKey object.
* Api Key
*
* @var RegisterKey
* @var string
*/
public static $key;

/**
* Connection object.
*
* @var CURLConnection
* @var \ComicVine\Api\Connection\Connection;
*/
public static $conn;

Expand Down

0 comments on commit 3efda9b

Please sign in to comment.