From a4642d17e0e1ea018b128efdcc3db281461458b1 Mon Sep 17 00:00:00 2001 From: Frédéric Guillot Date: Wed, 4 Apr 2018 15:21:13 -0700 Subject: Move custom libs to the source tree --- libs/SimpleValidator/Validator.php | 44 ++++++++++++++++ libs/SimpleValidator/Validators/Alpha.php | 15 ++++++ libs/SimpleValidator/Validators/AlphaNumeric.php | 15 ++++++ libs/SimpleValidator/Validators/Base.php | 37 +++++++++++++ libs/SimpleValidator/Validators/Date.php | 45 ++++++++++++++++ libs/SimpleValidator/Validators/Email.php | 67 ++++++++++++++++++++++++ libs/SimpleValidator/Validators/Equals.php | 27 ++++++++++ libs/SimpleValidator/Validators/Exists.php | 38 ++++++++++++++ libs/SimpleValidator/Validators/GreaterThan.php | 23 ++++++++ libs/SimpleValidator/Validators/InArray.php | 23 ++++++++ libs/SimpleValidator/Validators/Integer.php | 25 +++++++++ libs/SimpleValidator/Validators/Ip.php | 15 ++++++ libs/SimpleValidator/Validators/Length.php | 26 +++++++++ libs/SimpleValidator/Validators/MaxLength.php | 24 +++++++++ libs/SimpleValidator/Validators/MinLength.php | 24 +++++++++ libs/SimpleValidator/Validators/NotEmpty.php | 15 ++++++ libs/SimpleValidator/Validators/NotEquals.php | 28 ++++++++++ libs/SimpleValidator/Validators/NotInArray.php | 15 ++++++ libs/SimpleValidator/Validators/Numeric.php | 15 ++++++ libs/SimpleValidator/Validators/Range.php | 33 ++++++++++++ libs/SimpleValidator/Validators/Required.php | 11 ++++ libs/SimpleValidator/Validators/Unique.php | 48 +++++++++++++++++ 22 files changed, 613 insertions(+) create mode 100644 libs/SimpleValidator/Validator.php create mode 100644 libs/SimpleValidator/Validators/Alpha.php create mode 100644 libs/SimpleValidator/Validators/AlphaNumeric.php create mode 100644 libs/SimpleValidator/Validators/Base.php create mode 100644 libs/SimpleValidator/Validators/Date.php create mode 100644 libs/SimpleValidator/Validators/Email.php create mode 100644 libs/SimpleValidator/Validators/Equals.php create mode 100644 libs/SimpleValidator/Validators/Exists.php create mode 100644 libs/SimpleValidator/Validators/GreaterThan.php create mode 100644 libs/SimpleValidator/Validators/InArray.php create mode 100644 libs/SimpleValidator/Validators/Integer.php create mode 100644 libs/SimpleValidator/Validators/Ip.php create mode 100644 libs/SimpleValidator/Validators/Length.php create mode 100644 libs/SimpleValidator/Validators/MaxLength.php create mode 100644 libs/SimpleValidator/Validators/MinLength.php create mode 100644 libs/SimpleValidator/Validators/NotEmpty.php create mode 100644 libs/SimpleValidator/Validators/NotEquals.php create mode 100644 libs/SimpleValidator/Validators/NotInArray.php create mode 100644 libs/SimpleValidator/Validators/Numeric.php create mode 100644 libs/SimpleValidator/Validators/Range.php create mode 100644 libs/SimpleValidator/Validators/Required.php create mode 100644 libs/SimpleValidator/Validators/Unique.php (limited to 'libs/SimpleValidator') diff --git a/libs/SimpleValidator/Validator.php b/libs/SimpleValidator/Validator.php new file mode 100644 index 00000000..30015dc6 --- /dev/null +++ b/libs/SimpleValidator/Validator.php @@ -0,0 +1,44 @@ +data = $data; + $this->validators = $validators; + } + + public function execute() + { + $result = true; + + foreach ($this->validators as $validator) { + if (! $validator->execute($this->data)) { + $this->addError($validator->getField(), $validator->getErrorMessage()); + $result = false; + } + } + + return $result; + } + + public function addError($field, $message) + { + if (! isset($this->errors[$field])) { + $this->errors[$field] = array(); + } + + $this->errors[$field][] = $message; + } + + public function getErrors() + { + return $this->errors; + } +} diff --git a/libs/SimpleValidator/Validators/Alpha.php b/libs/SimpleValidator/Validators/Alpha.php new file mode 100644 index 00000000..c29ba481 --- /dev/null +++ b/libs/SimpleValidator/Validators/Alpha.php @@ -0,0 +1,15 @@ +isFieldNotEmpty($data)) { + return ctype_alpha($data[$this->field]); + } + + return true; + } +} diff --git a/libs/SimpleValidator/Validators/AlphaNumeric.php b/libs/SimpleValidator/Validators/AlphaNumeric.php new file mode 100644 index 00000000..8d5000b4 --- /dev/null +++ b/libs/SimpleValidator/Validators/AlphaNumeric.php @@ -0,0 +1,15 @@ +isFieldNotEmpty($data)) { + return ctype_alnum($data[$this->field]); + } + + return true; + } +} diff --git a/libs/SimpleValidator/Validators/Base.php b/libs/SimpleValidator/Validators/Base.php new file mode 100644 index 00000000..8157ae50 --- /dev/null +++ b/libs/SimpleValidator/Validators/Base.php @@ -0,0 +1,37 @@ +field = $field; + $this->error_message = $error_message; + } + + public function getErrorMessage() + { + return $this->error_message; + } + + public function getField() + { + if (is_array($this->field)) { + return $this->field[0]; + } + + return $this->field; + } + + public function isFieldNotEmpty(array $data) + { + return isset($data[$this->field]) && $data[$this->field] !== ''; + } +} diff --git a/libs/SimpleValidator/Validators/Date.php b/libs/SimpleValidator/Validators/Date.php new file mode 100644 index 00000000..4ec4b7fd --- /dev/null +++ b/libs/SimpleValidator/Validators/Date.php @@ -0,0 +1,45 @@ +formats = $formats; + } + + public function execute(array $data) + { + if ($this->isFieldNotEmpty($data)) { + foreach ($this->formats as $format) { + if ($this->isValidDate($data[$this->field], $format)) { + return true; + } + } + + return false; + } + + return true; + } + + public function isValidDate($value, $format) + { + $date = DateTime::createFromFormat($format, $value); + + if ($date !== false) { + $errors = DateTime::getLastErrors(); + if ($errors['error_count'] === 0 && $errors['warning_count'] === 0) { + return $date->getTimestamp() > 0; + } + } + + return false; + } +} diff --git a/libs/SimpleValidator/Validators/Email.php b/libs/SimpleValidator/Validators/Email.php new file mode 100644 index 00000000..f3977042 --- /dev/null +++ b/libs/SimpleValidator/Validators/Email.php @@ -0,0 +1,67 @@ +isFieldNotEmpty($data)) { + + // I use the same validation method as Firefox + // http://hg.mozilla.org/mozilla-central/file/cf5da681d577/content/html/content/src/nsHTMLInputElement.cpp#l3967 + + $value = $data[$this->field]; + $length = strlen($value); + + // If the email address begins with a '@' or ends with a '.', + // we know it's invalid. + if ($value[0] === '@' || $value[$length - 1] === '.') { + + return false; + } + + // Check the username + for ($i = 0; $i < $length && $value[$i] !== '@'; ++$i) { + + $c = $value[$i]; + + if (! (ctype_alnum($c) || $c === '.' || $c === '!' || $c === '#' || $c === '$' || + $c === '%' || $c === '&' || $c === '\'' || $c === '*' || $c === '+' || + $c === '-' || $c === '/' || $c === '=' || $c === '?' || $c === '^' || + $c === '_' || $c === '`' || $c === '{' || $c === '|' || $c === '}' || + $c === '~')) { + + return false; + } + } + + // There is no domain name (or it's one-character long), + // that's not a valid email address. + if (++$i >= $length) return false; + if (($i + 1) === $length) return false; + + // The domain name can't begin with a dot. + if ($value[$i] === '.') return false; + + // Parsing the domain name. + for (; $i < $length; ++$i) { + + $c = $value[$i]; + + if ($c === '.') { + + // A dot can't follow a dot. + if ($value[$i - 1] === '.') return false; + } + elseif (! (ctype_alnum($c) || $c === '-')) { + + // The domain characters have to be in this list to be valid. + return false; + } + } + } + + return true; + } +} diff --git a/libs/SimpleValidator/Validators/Equals.php b/libs/SimpleValidator/Validators/Equals.php new file mode 100644 index 00000000..6b69dd80 --- /dev/null +++ b/libs/SimpleValidator/Validators/Equals.php @@ -0,0 +1,27 @@ +field2 = $field2; + } + + public function execute(array $data) + { + if ($this->isFieldNotEmpty($data)) { + if (! isset($data[$this->field2])) { + return false; + } + + return $data[$this->field] === $data[$this->field2]; + } + + return true; + } +} diff --git a/libs/SimpleValidator/Validators/Exists.php b/libs/SimpleValidator/Validators/Exists.php new file mode 100644 index 00000000..1998e673 --- /dev/null +++ b/libs/SimpleValidator/Validators/Exists.php @@ -0,0 +1,38 @@ +pdo = $pdo; + $this->table = $table; + $this->key = $key; + } + + + public function execute(array $data) + { + if (! $this->isFieldNotEmpty($data)) { + return true; + } + + if ($this->key === '') { + $this->key = $this->field; + } + + $rq = $this->pdo->prepare('SELECT 1 FROM '.$this->table.' WHERE '.$this->key.'=?'); + $rq->execute(array($data[$this->field])); + + return $rq->fetchColumn() == 1; + } +} diff --git a/libs/SimpleValidator/Validators/GreaterThan.php b/libs/SimpleValidator/Validators/GreaterThan.php new file mode 100644 index 00000000..6e560319 --- /dev/null +++ b/libs/SimpleValidator/Validators/GreaterThan.php @@ -0,0 +1,23 @@ +min = $min; + } + + public function execute(array $data) + { + if ($this->isFieldNotEmpty($data)) { + return $data[$this->field] > $this->min; + } + + return true; + } +} diff --git a/libs/SimpleValidator/Validators/InArray.php b/libs/SimpleValidator/Validators/InArray.php new file mode 100644 index 00000000..f2f8c134 --- /dev/null +++ b/libs/SimpleValidator/Validators/InArray.php @@ -0,0 +1,23 @@ +array = $array; + } + + public function execute(array $data) + { + if ($this->isFieldNotEmpty($data)) { + return in_array($data[$this->field], $this->array); + } + + return true; + } +} diff --git a/libs/SimpleValidator/Validators/Integer.php b/libs/SimpleValidator/Validators/Integer.php new file mode 100644 index 00000000..5afdc1e0 --- /dev/null +++ b/libs/SimpleValidator/Validators/Integer.php @@ -0,0 +1,25 @@ +isFieldNotEmpty($data)) { + if (is_string($data[$this->field])) { + + if ($data[$this->field][0] === '-') { + return ctype_digit(substr($data[$this->field], 1)); + } + + return ctype_digit($data[$this->field]); + } + else { + return is_int($data[$this->field]); + } + } + + return true; + } +} diff --git a/libs/SimpleValidator/Validators/Ip.php b/libs/SimpleValidator/Validators/Ip.php new file mode 100644 index 00000000..754f4f3e --- /dev/null +++ b/libs/SimpleValidator/Validators/Ip.php @@ -0,0 +1,15 @@ +isFieldNotEmpty($data)) { + return filter_var($data[$this->field], FILTER_VALIDATE_IP) !== false; + } + + return true; + } +} diff --git a/libs/SimpleValidator/Validators/Length.php b/libs/SimpleValidator/Validators/Length.php new file mode 100644 index 00000000..7ef241c4 --- /dev/null +++ b/libs/SimpleValidator/Validators/Length.php @@ -0,0 +1,26 @@ +min = $min; + $this->max = $max; + } + + public function execute(array $data) + { + if ($this->isFieldNotEmpty($data)) { + $length = mb_strlen($data[$this->field], 'UTF-8'); + return $length >= $this->min && $length <= $this->max; + } + + return true; + } +} diff --git a/libs/SimpleValidator/Validators/MaxLength.php b/libs/SimpleValidator/Validators/MaxLength.php new file mode 100644 index 00000000..6c4e7771 --- /dev/null +++ b/libs/SimpleValidator/Validators/MaxLength.php @@ -0,0 +1,24 @@ +max = $max; + } + + public function execute(array $data) + { + if ($this->isFieldNotEmpty($data)) { + $length = mb_strlen($data[$this->field], 'UTF-8'); + return $length <= $this->max; + } + + return true; + } +} diff --git a/libs/SimpleValidator/Validators/MinLength.php b/libs/SimpleValidator/Validators/MinLength.php new file mode 100644 index 00000000..0ac4217a --- /dev/null +++ b/libs/SimpleValidator/Validators/MinLength.php @@ -0,0 +1,24 @@ +min = $min; + } + + public function execute(array $data) + { + if ($this->isFieldNotEmpty($data)) { + $length = mb_strlen($data[$this->field], 'UTF-8'); + return $length >= $this->min; + } + + return true; + } +} diff --git a/libs/SimpleValidator/Validators/NotEmpty.php b/libs/SimpleValidator/Validators/NotEmpty.php new file mode 100644 index 00000000..bbb14b5b --- /dev/null +++ b/libs/SimpleValidator/Validators/NotEmpty.php @@ -0,0 +1,15 @@ +field, $data)) { + return $data[$this->field] !== null && $data[$this->field] !== ''; + } + + return true; + } +} diff --git a/libs/SimpleValidator/Validators/NotEquals.php b/libs/SimpleValidator/Validators/NotEquals.php new file mode 100644 index 00000000..d1d949ea --- /dev/null +++ b/libs/SimpleValidator/Validators/NotEquals.php @@ -0,0 +1,28 @@ +field2 = $field2; + } + + public function execute(array $data) + { + if ($this->isFieldNotEmpty($data)) { + + if (! isset($data[$this->field2])) { + return true; + } + + return $data[$this->field] !== $data[$this->field2]; + } + + return true; + } +} diff --git a/libs/SimpleValidator/Validators/NotInArray.php b/libs/SimpleValidator/Validators/NotInArray.php new file mode 100644 index 00000000..98974c9c --- /dev/null +++ b/libs/SimpleValidator/Validators/NotInArray.php @@ -0,0 +1,15 @@ +isFieldNotEmpty($data)) { + return ! in_array($data[$this->field], $this->array); + } + + return true; + } +} diff --git a/libs/SimpleValidator/Validators/Numeric.php b/libs/SimpleValidator/Validators/Numeric.php new file mode 100644 index 00000000..31226866 --- /dev/null +++ b/libs/SimpleValidator/Validators/Numeric.php @@ -0,0 +1,15 @@ +isFieldNotEmpty($data)) { + return is_numeric($data[$this->field]); + } + + return true; + } +} diff --git a/libs/SimpleValidator/Validators/Range.php b/libs/SimpleValidator/Validators/Range.php new file mode 100644 index 00000000..065b2b9d --- /dev/null +++ b/libs/SimpleValidator/Validators/Range.php @@ -0,0 +1,33 @@ +min = $min; + $this->max = $max; + } + + public function execute(array $data) + { + if ($this->isFieldNotEmpty($data)) { + + if (! is_numeric($data[$this->field])) { + return false; + } + + if ($data[$this->field] < $this->min || $data[$this->field] > $this->max) { + return false; + } + } + + return true; + } +} diff --git a/libs/SimpleValidator/Validators/Required.php b/libs/SimpleValidator/Validators/Required.php new file mode 100644 index 00000000..f5e65616 --- /dev/null +++ b/libs/SimpleValidator/Validators/Required.php @@ -0,0 +1,11 @@ +isFieldNotEmpty($data); + } +} diff --git a/libs/SimpleValidator/Validators/Unique.php b/libs/SimpleValidator/Validators/Unique.php new file mode 100644 index 00000000..00caeb54 --- /dev/null +++ b/libs/SimpleValidator/Validators/Unique.php @@ -0,0 +1,48 @@ +pdo = $pdo; + $this->primary_key = $primary_key; + $this->table = $table; + } + + public function execute(array $data) + { + if ($this->isFieldNotEmpty($data)) { + if (! isset($data[$this->primary_key])) { + $rq = $this->pdo->prepare('SELECT 1 FROM '.$this->table.' WHERE '.$this->field.'=?'); + $rq->execute(array($data[$this->field])); + } + else { + + $rq = $this->pdo->prepare( + 'SELECT 1 FROM '.$this->table.' + WHERE '.$this->field.'=? AND '.$this->primary_key.' != ?' + ); + + $rq->execute(array($data[$this->field], $data[$this->primary_key])); + } + + $result = $rq->fetchColumn(); + + if ($result == 1) { // Postgresql returns an integer but other database returns a string '1' + return false; + } + } + + return true; + } +} -- cgit v1.2.3