Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 24 additions & 20 deletions Validator/Datatype.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ public function __construct($params, $input){

}

/*
* This will not work as intended if a custom datatype calls `parent::validadte()` due to the assumptions of the
* `checkLength()` method
*/
public function validate(){

if(isset($this->params[self::MIN_LENGTH]) && !$this->checkLength($this->params[self::MIN_LENGTH]))
Expand Down Expand Up @@ -61,31 +65,31 @@ public function sanitize(){
protected function checkLength($limit, $comparator = self::LENGTH_GT, $datatype = null){
// establish a comparator
if ($datatype == null) {
if ($this->params['type'] === self::TYPE_ARRAY) { $datatype = self::COMPARATOR_ARRAY; }
elseif ($this->params['type'] === self::TYPE_INT) { $datatype = self::COMPARATOR_NUMERIC; }
elseif ($this->params['type'] === self::TYPE_STRING) { $datatype = self::COMPARATOR_STRING; }
}

if (isset($this->params['type'])) {
if ($this->params['type'] === self::TYPE_ARRAY) { $datatype = self::COMPARATOR_ARRAY; }
elseif ($this->params['type'] === self::TYPE_INT) { $datatype = self::COMPARATOR_NUMERIC; }
elseif ($this->params['type'] === self::TYPE_STRING) { $datatype = self::COMPARATOR_STRING; }
} else {
// try to guess if not specified
if (is_array( $this->input)) { $datatype = self::COMPARATOR_ARRAY; }
elseif (is_numeric($this->input)) { $datatype = self::COMPARATOR_NUMERIC; }
elseif (is_string( $this->input)) { $datatype = self::COMPARATOR_STRING; }
}
}
// based on the comparator deduce the correct count to compare
// how to pass a callable comparator when most custom datatypes simply call `parent::validate()`? No datatype will
// be set in that situation, thus the comparisons below will not produce the intended outcome.
$count = null;
if($datatype === self::COMPARATOR_ARRAY)
$count = count($this->input);
else if($datatype === self::COMPARATOR_NUMERIC)
$count = $this->input;
else if($datatype === self::COMPARATOR_STRING)
$count = strlen($this->input);
else
$count = $this->input;
if ($datatype === self::COMPARATOR_ARRAY) { $count = count($this->input); }
elseif ($datatype === self::COMPARATOR_NUMERIC) { $count = $this->input; }
elseif ($datatype === self::COMPARATOR_STRING) { $count = strlen($this->input); }
else { $count = $this->input; }

//if the comparator is callable (is a function), return the result of that instead, otherwise do the standard op

if(is_callable($datatype))
return $datatype($comparator, $this->input, $limit);
else if($comparator == self::LENGTH_GT)
return $count >= $limit;
else
return $count <= $limit;

if (is_callable($datatype)) { return $datatype($comparator, $this->input, $limit); }
elseif ($comparator == self::LENGTH_GT) { return $count >= $limit; }
else { return $count <= $limit; }
}

protected function checkMembership($container){
Expand Down