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

Commit

Permalink
Refactored Validation and FilterValidation.
Browse files Browse the repository at this point in the history
  • Loading branch information
grz-gajda committed Nov 30, 2015
1 parent 3efda9b commit d6329df
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 90 deletions.
8 changes: 4 additions & 4 deletions src/Api/Controllers/ControllerRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,10 @@ public function getSeriesList()
*
* @todo Waiting for implementation
*/
public function getSearch()
/*public function getSearch()
{
// @todo Waiting for implementation
}
}*/

/**
* Get all story arcs from ComicVine.
Expand Down Expand Up @@ -233,10 +233,10 @@ public function getTypes()
*
* @todo Waiting for implementation
*/
public function getVideos()
/*public function getVideos()
{
// @todo Waiting for implementation
}
}*/

/**
* Get all video types from ComicVine.
Expand Down
25 changes: 1 addition & 24 deletions src/Api/Controllers/ControllerRequestAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,6 @@ abstract class ControllerRequestAbstract
'filter' => false,
];

/**
* Part of URL.
*
* @var string
*/
protected $url = "";

/**
* Set parameters which can be added to URL.
*
Expand Down Expand Up @@ -71,23 +64,7 @@ protected function setFilters($limit = false, $offset = false, $sort = false, $f
*/
protected function setUrl($url)
{
if (empty($url) === true) {
throw new EmptyControllerRequestUrl("URL parameter for request cannot be empty.");
}

$this->url = $url;

return $this->getController();
}

/**
* Return new controller for making query.
*
* @return \ComicVine\Api\Controllers\ControllerQuery
*/
protected function getController()
{
return new ControllerQuery($this->enabledFilters, $this->url);
return new ControllerQuery($this->enabledFilters, $url);
}

}
3 changes: 0 additions & 3 deletions src/Api/Filters/FilterHandlers.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,12 @@ trait FilterHandlers
protected function flushDisabledFilters(array $filters, array $enabled)
{
array_walk($filters, function (&$param, $field) use ($enabled) {
// 'api_key' field
if ($this->isApiKey($field) === true) {
return $param;
}
// 'field_list'
if ($this->isFieldList($field) === true) {
return $param = $this->validFieldList($field, $param);
}
// 'sort', 'filter', 'limit', 'offset'
if ($this->isFilter($field, $enabled) === true) {
return $param = $this->validFilter($param);
}
Expand Down
51 changes: 19 additions & 32 deletions src/Api/Filters/FilterValidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,9 @@ public function isIntAndBetween($input, $min, $max = "")
return false;
}

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

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

Expand All @@ -41,42 +39,33 @@ public function isIntAndBetween($input, $min, $max = "")
* 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 $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 (is_array($valueParams) === true) {
if ($this->isParamOfTypeMultiple($value, $valueParams) === false
|| $this->isParamOfTypeSingle($key, $keyParams) === false
) {
return false;
}
}

if ($this->isParamOfType($value, $valueParams) === false) {
return false;
if (is_array($valueParams) === false) {
if ($this->isParamOfTypeSingle($key, $keyParams) === false
|| $this->isParamOfTypeSingle($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.
*
Expand All @@ -87,15 +76,13 @@ public function isParamOfType($param, $types)
*/
public function isParamOfTypeMultiple($param, $types)
{
array_walk($types, function(&$type) use ($param) {
array_walk($types, function (&$type) use ($param) {
$type = $this->isParamOfTypeSingle($param, $type);
});

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

return false;
return in_array(true, $types);
}

/**
Expand Down
5 changes: 2 additions & 3 deletions src/Api/RegisterKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ class RegisterKey
public function __construct($key)
{
$this->key = $key;

return $this->checkLength();
}

/**
Expand All @@ -45,7 +43,8 @@ public function __construct($key)
*/
public function getKey()
{
return $this->key;
return $this->checkLength()
->key;
}

/**
Expand Down
35 changes: 11 additions & 24 deletions src/Api/Validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ public function validation($type = "", $input)
case 'field_list':
return $this->validFieldList($input);
case 'limit':
return $this->validLimit($input);
return $this->validNumber('limit', $input, 0, 100);
case 'offset':
return $this->validOffset($input);
return $this->validNumber('offset', $input, 0);
case 'filter':
return $this->validFilter($input);
case 'sort':
Expand Down Expand Up @@ -86,35 +86,22 @@ protected function validFieldList($input)
}

/**
* Validation for LIMIT parameter.
* Check if offset or limit is valid.
*
* @param string|array $input
*
* @return bool
*/
protected function validLimit($input)
{
if ($this->isIntAndBetween($input, 0, 100) === false) {
return false;
}

return $this->isEnabledFilter('limit', $this->enabledFilters);
}

/**
* Validation for OFFSET parameter.
* @param string $type Type of valid (offset or limit)
* @param string $input Value
* @param integer $min Min range what value can be
* @param string|integer $max Max range what value can be
*
* @param string|array $input
*
* @return bool
* @return $this|bool
*/
protected function validOffset($input)
protected function validNumber($type, $input, $min, $max = "")
{
if ($this->isIntAndBetween($input, 0) === false) {
if ($this->isIntAndBetween($input, $min, $max) === false) {
return false;
}

return $this->isEnabledFilter('offset', $this->enabledFilters);
return $this->isEnabledFilter($type, $this->enabledFilters);
}

/**
Expand Down

0 comments on commit d6329df

Please sign in to comment.