<?php
 namespace Moto\Validator\Db; use Moto; use Zend\Validator; class RecordExists extends Validator\Db\RecordExists { protected $_context = array(); protected $_where = null; public function __construct($options = null) { $this->setOptions(array( 'allow_zero' => false, 'allow_null' => false, )); parent::__construct($options); if ($options instanceof Traversable) { $options = ArrayUtils::iteratorToArray($options); } if (is_array($options)) { if (!empty($options['where'])) { $this->_where = $options['where']; } } if (null === $this->adapter) { $this->adapter = Moto\Config::get('databaseAdapter'); } } public function getSelect() { $select = parent::getSelect(); if (!empty($this->_where)) { foreach ($this->_where as $key => $value) { if (!array_key_exists($key, $this->_context)) { $select->where->equalTo($key, $value); continue; } switch ($value) { case '@equalTo': $select->where->equalTo($key, $this->_context[$key]); break; case '@notEqualTo': $select->where->notEqualTo($key, $this->_context[$key]); break; default: $select->where->equalTo($key, $value); break; } } $this->_where = null; $this->select = $select; } return $select; } public function isValid($value, $context = array()) { $this->_context = $context; if ($value === '0' || $value === 0) { $allowZero = $this->getOption('allow_zero'); if (is_callable($allowZero)) { $allowZero = $allowZero($value, $context); } if ($allowZero) { return true; } } if ($value === null) { $allowNull = $this->getOption('allow_null'); if (is_callable($allowNull)) { $allowNull = $allowNull($value, $context); } if ($allowNull) { return true; } } return parent::isValid($value, $context); } } 