summaryrefslogtreecommitdiff
path: root/libs
diff options
context:
space:
mode:
authorFrédéric Guillot <fred@kanboard.net>2018-04-04 15:21:13 -0700
committerFrédéric Guillot <fred@kanboard.net>2018-04-04 15:21:13 -0700
commita4642d17e0e1ea018b128efdcc3db281461458b1 (patch)
tree00210c3d0abd0adea7f8817e6ba1d82c1ea4b50e /libs
parent62178b1f2b4ad6ed8eafbcd3be8ef2f46b041b82 (diff)
Move custom libs to the source tree
Diffstat (limited to 'libs')
-rw-r--r--libs/SimpleQueue/Adapter/AmqpQueueAdapter.php138
-rw-r--r--libs/SimpleQueue/Adapter/BeanstalkQueueAdapter.php120
-rw-r--r--libs/SimpleQueue/Exception/NotSupportedException.php14
-rw-r--r--libs/SimpleQueue/Job.php98
-rw-r--r--libs/SimpleQueue/Queue.php92
-rw-r--r--libs/SimpleQueue/QueueAdapterInterface.php58
-rw-r--r--libs/SimpleValidator/Validator.php44
-rw-r--r--libs/SimpleValidator/Validators/Alpha.php15
-rw-r--r--libs/SimpleValidator/Validators/AlphaNumeric.php15
-rw-r--r--libs/SimpleValidator/Validators/Base.php37
-rw-r--r--libs/SimpleValidator/Validators/Date.php45
-rw-r--r--libs/SimpleValidator/Validators/Email.php67
-rw-r--r--libs/SimpleValidator/Validators/Equals.php27
-rw-r--r--libs/SimpleValidator/Validators/Exists.php38
-rw-r--r--libs/SimpleValidator/Validators/GreaterThan.php23
-rw-r--r--libs/SimpleValidator/Validators/InArray.php23
-rw-r--r--libs/SimpleValidator/Validators/Integer.php25
-rw-r--r--libs/SimpleValidator/Validators/Ip.php15
-rw-r--r--libs/SimpleValidator/Validators/Length.php26
-rw-r--r--libs/SimpleValidator/Validators/MaxLength.php24
-rw-r--r--libs/SimpleValidator/Validators/MinLength.php24
-rw-r--r--libs/SimpleValidator/Validators/NotEmpty.php15
-rw-r--r--libs/SimpleValidator/Validators/NotEquals.php28
-rw-r--r--libs/SimpleValidator/Validators/NotInArray.php15
-rw-r--r--libs/SimpleValidator/Validators/Numeric.php15
-rw-r--r--libs/SimpleValidator/Validators/Range.php33
-rw-r--r--libs/SimpleValidator/Validators/Required.php11
-rw-r--r--libs/SimpleValidator/Validators/Unique.php48
28 files changed, 1133 insertions, 0 deletions
diff --git a/libs/SimpleQueue/Adapter/AmqpQueueAdapter.php b/libs/SimpleQueue/Adapter/AmqpQueueAdapter.php
new file mode 100644
index 00000000..379dd9b8
--- /dev/null
+++ b/libs/SimpleQueue/Adapter/AmqpQueueAdapter.php
@@ -0,0 +1,138 @@
+<?php
+
+namespace SimpleQueue\Adapter;
+
+use DateTime;
+use PhpAmqpLib\Channel\AMQPChannel;
+use PhpAmqpLib\Message\AMQPMessage;
+use PhpAmqpLib\Wire\AMQPTable;
+use SimpleQueue\Job;
+use SimpleQueue\QueueAdapterInterface;
+
+/**
+ * Class AmqpQueueAdapter
+ *
+ * @package SimpleQueue\Adapter
+ */
+class AmqpQueueAdapter implements QueueAdapterInterface
+{
+ /**
+ * @var AMQPChannel
+ */
+ protected $channel;
+
+ /**
+ * @var string
+ */
+ protected $exchange = '';
+
+ /**
+ * @var string
+ */
+ protected $queue = '';
+
+ /**
+ * AmqpQueueAdapter constructor.
+ *
+ * @param AMQPChannel $channel
+ * @param string $queue
+ * @param string $exchange
+ */
+ public function __construct(AMQPChannel $channel, $queue, $exchange)
+ {
+ $this->channel = $channel;
+ $this->exchange = $exchange;
+ $this->queue = $queue;
+ }
+
+ /**
+ * Send a job
+ *
+ * @access public
+ * @param Job $job
+ * @return $this
+ */
+ public function push(Job $job)
+ {
+ $message = new AMQPMessage($job->serialize(), array('content_type' => 'text/plain'));
+ $this->channel->basic_publish($message, $this->exchange);
+ return $this;
+ }
+
+ /**
+ * Schedule a job in the future
+ *
+ * @access public
+ * @param Job $job
+ * @param DateTime $dateTime
+ * @return $this
+ */
+ public function schedule(Job $job, DateTime $dateTime)
+ {
+ $now = new DateTime();
+ $when = clone($dateTime);
+ $delay = $when->getTimestamp() - $now->getTimestamp();
+
+ $message = new AMQPMessage($job->serialize(), array('delivery_mode' => 2));
+ $message->set('application_headers', new AMQPTable(array('x-delay' => $delay)));
+
+ $this->channel->basic_publish($message, $this->exchange);
+ return $this;
+ }
+
+ /**
+ * Wait and get job from a queue
+ *
+ * @access public
+ * @return Job|null
+ */
+ public function pull()
+ {
+ $message = null;
+
+ $this->channel->basic_consume($this->queue, 'test', false, false, false, false, function ($msg) use (&$message) {
+ $message = $msg;
+ $message->delivery_info['channel']->basic_cancel($message->delivery_info['consumer_tag']);
+ });
+
+ while (count($this->channel->callbacks)) {
+ $this->channel->wait();
+ }
+
+ if ($message === null) {
+ return null;
+ }
+
+ $job = new Job();
+ $job->setId($message->get('delivery_tag'));
+ $job->unserialize($message->getBody());
+
+ return $job;
+ }
+
+ /**
+ * Acknowledge a job
+ *
+ * @access public
+ * @param Job $job
+ * @return $this
+ */
+ public function completed(Job $job)
+ {
+ $this->channel->basic_ack($job->getId());
+ return $this;
+ }
+
+ /**
+ * Mark a job as failed
+ *
+ * @access public
+ * @param Job $job
+ * @return $this
+ */
+ public function failed(Job $job)
+ {
+ $this->channel->basic_nack($job->getId());
+ return $this;
+ }
+}
diff --git a/libs/SimpleQueue/Adapter/BeanstalkQueueAdapter.php b/libs/SimpleQueue/Adapter/BeanstalkQueueAdapter.php
new file mode 100644
index 00000000..407f60e2
--- /dev/null
+++ b/libs/SimpleQueue/Adapter/BeanstalkQueueAdapter.php
@@ -0,0 +1,120 @@
+<?php
+
+namespace SimpleQueue\Adapter;
+
+use DateTime;
+use Pheanstalk\Job as BeanstalkJob;
+use Pheanstalk\Pheanstalk;
+use Pheanstalk\PheanstalkInterface;
+use SimpleQueue\Job;
+use SimpleQueue\QueueAdapterInterface;
+
+/**
+ * Class BeanstalkQueueAdapter
+ *
+ * @package SimpleQueue\Adapter
+ */
+class BeanstalkQueueAdapter implements QueueAdapterInterface
+{
+ /**
+ * @var PheanstalkInterface
+ */
+ protected $beanstalk;
+
+ /**
+ * @var string
+ */
+ protected $queueName = '';
+
+ /**
+ * BeanstalkQueueAdapter constructor.
+ *
+ * @param PheanstalkInterface $beanstalk
+ * @param string $queueName
+ */
+ public function __construct(PheanstalkInterface $beanstalk, $queueName)
+ {
+ $this->beanstalk = $beanstalk;
+ $this->queueName = $queueName;
+ }
+
+ /**
+ * Send a job
+ *
+ * @access public
+ * @param Job $job
+ * @return $this
+ */
+ public function push(Job $job)
+ {
+ $this->beanstalk->putInTube($this->queueName, $job->serialize());
+ return $this;
+ }
+
+ /**
+ * Schedule a job in the future
+ *
+ * @access public
+ * @param Job $job
+ * @param DateTime $dateTime
+ * @return $this
+ */
+ public function schedule(Job $job, DateTime $dateTime)
+ {
+ $now = new DateTime();
+ $when = clone($dateTime);
+ $delay = $when->getTimestamp() - $now->getTimestamp();
+
+ $this->beanstalk->putInTube($this->queueName, $job->serialize(), Pheanstalk::DEFAULT_PRIORITY, $delay);
+ return $this;
+ }
+
+ /**
+ * Wait and get job from a queue
+ *
+ * @access public
+ * @return Job|null
+ */
+ public function pull()
+ {
+ $beanstalkJob = $this->beanstalk->reserveFromTube($this->queueName);
+
+ if ($beanstalkJob === false) {
+ return null;
+ }
+
+ $job = new Job();
+ $job->setId($beanstalkJob->getId());
+ $job->unserialize($beanstalkJob->getData());
+
+ return $job;
+ }
+
+ /**
+ * Acknowledge a job
+ *
+ * @access public
+ * @param Job $job
+ * @return $this
+ */
+ public function completed(Job $job)
+ {
+ $beanstalkJob = new BeanstalkJob($job->getId(), $job->serialize());
+ $this->beanstalk->delete($beanstalkJob);
+ return $this;
+ }
+
+ /**
+ * Mark a job as failed
+ *
+ * @access public
+ * @param Job $job
+ * @return $this
+ */
+ public function failed(Job $job)
+ {
+ $beanstalkJob = new BeanstalkJob($job->getId(), $job->serialize());
+ $this->beanstalk->bury($beanstalkJob);
+ return $this;
+ }
+}
diff --git a/libs/SimpleQueue/Exception/NotSupportedException.php b/libs/SimpleQueue/Exception/NotSupportedException.php
new file mode 100644
index 00000000..36106659
--- /dev/null
+++ b/libs/SimpleQueue/Exception/NotSupportedException.php
@@ -0,0 +1,14 @@
+<?php
+
+namespace SimpleQueue\Exception;
+
+use Exception;
+
+/**
+ * Class NotSupportedException
+ *
+ * @package SimpleQueue\Exception
+ */
+class NotSupportedException extends Exception
+{
+}
diff --git a/libs/SimpleQueue/Job.php b/libs/SimpleQueue/Job.php
new file mode 100644
index 00000000..799bbba8
--- /dev/null
+++ b/libs/SimpleQueue/Job.php
@@ -0,0 +1,98 @@
+<?php
+
+namespace SimpleQueue;
+
+/**
+ * Class Job
+ *
+ * @package SimpleQueue
+ */
+class Job
+{
+ protected $id;
+ protected $body;
+
+ /**
+ * Job constructor.
+ *
+ * @param null $body
+ * @param null $id
+ */
+ public function __construct($body = null, $id = null)
+ {
+ $this->body = $body;
+ $this->id = $id;
+ }
+
+ /**
+ * Unserialize a payload
+ *
+ * @param string $payload
+ * @return $this
+ */
+ public function unserialize($payload)
+ {
+ $this->body = json_decode($payload, true);
+ return $this;
+ }
+
+ /**
+ * Serialize the body
+ *
+ * @return string
+ */
+ public function serialize()
+ {
+ return json_encode($this->body);
+ }
+
+ /**
+ * Set body
+ *
+ * @param mixed $body
+ * @return Job
+ */
+ public function setBody($body)
+ {
+ $this->body = $body;
+ return $this;
+ }
+
+ /**
+ * Get body
+ *
+ * @return mixed
+ */
+ public function getBody()
+ {
+ return $this->body;
+ }
+
+ /**
+ * Set job ID
+ *
+ * @param mixed $jobId
+ * @return Job
+ */
+ public function setId($jobId)
+ {
+ $this->id = $jobId;
+ return $this;
+ }
+
+ /**
+ * Get job ID
+ * @return mixed
+ */
+ public function getId()
+ {
+ return $this->id;
+ }
+
+ /**
+ * Execute job
+ */
+ public function execute()
+ {
+ }
+}
diff --git a/libs/SimpleQueue/Queue.php b/libs/SimpleQueue/Queue.php
new file mode 100644
index 00000000..a88b55cb
--- /dev/null
+++ b/libs/SimpleQueue/Queue.php
@@ -0,0 +1,92 @@
+<?php
+
+namespace SimpleQueue;
+
+use DateTime;
+
+/**
+ * Class Queue
+ *
+ * @package SimpleQueue
+ */
+class Queue implements QueueAdapterInterface
+{
+ /**
+ * @var QueueAdapterInterface
+ */
+ protected $queueAdapter;
+
+ /**
+ * Queue constructor.
+ *
+ * @param QueueAdapterInterface $queueAdapter
+ */
+ public function __construct(QueueAdapterInterface $queueAdapter)
+ {
+ $this->queueAdapter = $queueAdapter;
+ }
+
+ /**
+ * Send a job
+ *
+ * @access public
+ * @param Job $job
+ * @return $this
+ */
+ public function push(Job $job)
+ {
+ $this->queueAdapter->push($job);
+ return $this;
+ }
+
+ /**
+ * Schedule a job in the future
+ *
+ * @access public
+ * @param Job $job
+ * @param DateTime $dateTime
+ * @return $this
+ */
+ public function schedule(Job $job, DateTime $dateTime)
+ {
+ $this->queueAdapter->schedule($job, $dateTime);
+ return $this;
+ }
+
+ /**
+ * Wait and get job from a queue
+ *
+ * @access public
+ * @return Job|null
+ */
+ public function pull()
+ {
+ return $this->queueAdapter->pull();
+ }
+
+ /**
+ * Acknowledge a job
+ *
+ * @access public
+ * @param Job $job
+ * @return $this
+ */
+ public function completed(Job $job)
+ {
+ $this->queueAdapter->completed($job);
+ return $this;
+ }
+
+ /**
+ * Mark a job as failed
+ *
+ * @access public
+ * @param Job $job
+ * @return $this
+ */
+ public function failed(Job $job)
+ {
+ $this->queueAdapter->failed($job);
+ return $this;
+ }
+}
diff --git a/libs/SimpleQueue/QueueAdapterInterface.php b/libs/SimpleQueue/QueueAdapterInterface.php
new file mode 100644
index 00000000..9bda3070
--- /dev/null
+++ b/libs/SimpleQueue/QueueAdapterInterface.php
@@ -0,0 +1,58 @@
+<?php
+
+namespace SimpleQueue;
+
+use DateTime;
+
+/**
+ * Interface AdapterInterface
+ *
+ * @package SimpleQueue\Adapter
+ */
+interface QueueAdapterInterface
+{
+ /**
+ * Send a job
+ *
+ * @access public
+ * @param Job $job
+ * @return $this
+ */
+ public function push(Job $job);
+
+ /**
+ * Schedule a job in the future
+ *
+ * @access public
+ * @param Job $job
+ * @param DateTime $dateTime
+ * @return $this
+ */
+ public function schedule(Job $job, DateTime $dateTime);
+
+ /**
+ * Wait and get job from a queue
+ *
+ * @access public
+ * @return Job|null
+ */
+ public function pull();
+
+ /**
+ * Acknowledge a job
+ *
+ * @access public
+ * @param Job $job
+ * @return $this
+ */
+ public function completed(Job $job);
+
+ /**
+ * Mark a job as failed
+ *
+ * @access public
+ * @param Job $job
+ * @return $this
+ */
+ public function failed(Job $job);
+}
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 @@
+<?php
+
+namespace SimpleValidator;
+
+class Validator
+{
+ private $data = array();
+ private $errors = array();
+ private $validators = array();
+
+ public function __construct(array $data, array $validators)
+ {
+ $this->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 @@
+<?php
+
+namespace SimpleValidator\Validators;
+
+class Alpha extends Base
+{
+ public function execute(array $data)
+ {
+ if ($this->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 @@
+<?php
+
+namespace SimpleValidator\Validators;
+
+class AlphaNumeric extends Base
+{
+ public function execute(array $data)
+ {
+ if ($this->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 @@
+<?php
+
+namespace SimpleValidator\Validators;
+
+abstract class Base
+{
+ protected $field = '';
+ protected $error_message = '';
+ protected $data = array();
+
+ abstract public function execute(array $data);
+
+ public function __construct($field, $error_message)
+ {
+ $this->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 @@
+<?php
+
+namespace SimpleValidator\Validators;
+
+use DateTime;
+
+class Date extends Base
+{
+ private $formats = array();
+
+ public function __construct($field, $error_message, array $formats)
+ {
+ parent::__construct($field, $error_message);
+ $this->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 @@
+<?php
+
+namespace SimpleValidator\Validators;
+
+class Email extends Base
+{
+ public function execute(array $data)
+ {
+ if ($this->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 @@
+<?php
+
+namespace SimpleValidator\Validators;
+
+class Equals extends Base
+{
+ private $field2;
+
+ public function __construct($field1, $field2, $error_message)
+ {
+ parent::__construct($field1, $error_message);
+ $this->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 @@
+<?php
+
+namespace SimpleValidator\Validators;
+
+use PDO;
+
+class Exists extends Base
+{
+ private $pdo;
+ private $key;
+ private $table;
+
+ public function __construct($field, $error_message, PDO $pdo, $table, $key = '')
+ {
+ parent::__construct($field, $error_message);
+
+ $this->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 @@
+<?php
+
+namespace SimpleValidator\Validators;
+
+class GreaterThan extends Base
+{
+ private $min;
+
+ public function __construct($field, $error_message, $min)
+ {
+ parent::__construct($field, $error_message);
+ $this->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 @@
+<?php
+
+namespace SimpleValidator\Validators;
+
+class InArray extends Base
+{
+ protected $array;
+
+ public function __construct($field, array $array, $error_message)
+ {
+ parent::__construct($field, $error_message);
+ $this->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 @@
+<?php
+
+namespace SimpleValidator\Validators;
+
+class Integer extends Base
+{
+ public function execute(array $data)
+ {
+ if ($this->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 @@
+<?php
+
+namespace SimpleValidator\Validators;
+
+class Ip extends Base
+{
+ public function execute(array $data)
+ {
+ if ($this->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 @@
+<?php
+
+namespace SimpleValidator\Validators;
+
+class Length extends Base
+{
+ private $min;
+ private $max;
+
+ public function __construct($field, $error_message, $min, $max)
+ {
+ parent::__construct($field, $error_message);
+ $this->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 @@
+<?php
+
+namespace SimpleValidator\Validators;
+
+class MaxLength extends Base
+{
+ private $max;
+
+ public function __construct($field, $error_message, $max)
+ {
+ parent::__construct($field, $error_message);
+ $this->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 @@
+<?php
+
+namespace SimpleValidator\Validators;
+
+class MinLength extends Base
+{
+ private $min;
+
+ public function __construct($field, $error_message, $min)
+ {
+ parent::__construct($field, $error_message);
+ $this->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 @@
+<?php
+
+namespace SimpleValidator\Validators;
+
+class NotEmpty extends Base
+{
+ public function execute(array $data)
+ {
+ if (array_key_exists($this->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 @@
+<?php
+
+namespace SimpleValidator\Validators;
+
+class NotEquals extends Base
+{
+ private $field2;
+
+ public function __construct($field1, $field2, $error_message)
+ {
+ parent::__construct($field1, $error_message);
+ $this->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 @@
+<?php
+
+namespace SimpleValidator\Validators;
+
+class NotInArray extends InArray
+{
+ 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/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 @@
+<?php
+
+namespace SimpleValidator\Validators;
+
+class Numeric extends Base
+{
+ public function execute(array $data)
+ {
+ if ($this->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 @@
+<?php
+
+namespace SimpleValidator\Validators;
+
+class Range extends Base
+{
+ private $min;
+ private $max;
+
+ public function __construct($field, $error_message, $min, $max)
+ {
+ parent::__construct($field, $error_message);
+
+ $this->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 @@
+<?php
+
+namespace SimpleValidator\Validators;
+
+class Required extends Base
+{
+ public function execute(array $data)
+ {
+ return $this->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 @@
+<?php
+
+namespace SimpleValidator\Validators;
+
+use PDO;
+
+class Unique extends Base
+{
+ private $pdo;
+ private $primary_key;
+ private $table;
+
+ public function __construct($field, $error_message, PDO $pdo, $table, $primary_key = 'id')
+ {
+ parent::__construct($field, $error_message);
+
+ $this->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;
+ }
+}