Skip to content

Instantly share code, notes, and snippets.

@asifjaveduk
Last active December 11, 2015 17:29
Show Gist options
  • Save asifjaveduk/4635137 to your computer and use it in GitHub Desktop.
Save asifjaveduk/4635137 to your computer and use it in GitHub Desktop.
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Validator
*/
namespace Zend\Validator\Db;
use Traversable;
use Zend\Db\Adapter\Adapter as DbAdapter;
use Zend\Db\Adapter\Driver\DriverInterface as DbDriverInterface;
use Zend\Db\Sql\Select as DbSelect;
use Zend\Db\Sql\TableIdentifier;
use Zend\Stdlib\ArrayUtils;
use Zend\Validator\AbstractValidator;
use Zend\Validator\Exception;
/**
* Class for Database record validation
*
* @category Zend
* @package Zend_Validate
*/
abstract class CustomAbstractDb extends AbstractValidator
{
/**
* Error constants
*/
const ERROR_NO_RECORD_FOUND = 'noRecordFound';
const ERROR_RECORD_FOUND = 'recordFound';
/**
* @var array Message templates
*/
protected $messageTemplates = array(
self::ERROR_NO_RECORD_FOUND => "No record matching the input was found",
self::ERROR_RECORD_FOUND => "A record matching the input was found",
);
/**
* Select object to use. can be set, or will be auto-generated
*
* @var DbSelect
*/
protected $select;
/**
* @var string
*/
protected $schema = null;
/**
* @var string
*/
protected $table = '';
/**
* @var string
*/
protected $field = '';
/**
* @var mixed
*/
protected $exclude = null;
/**
* @var mixed
*/
protected $include = null;
/**
* Database adapter to use. If null isValid() will throw an exception
*
* @var \Zend\Db\Adapter\Adapter
*/
protected $adapter = null;
/**
* Provides basic configuration for use with Zend\Validator\Db Validators
* Setting $exclude allows a single record to be excluded from matching.
* Exclude can either be a String containing a where clause, or an array with `field` and `value` keys
* to define the where clause added to the sql.
* A database adapter may optionally be supplied to avoid using the registered default adapter.
*
* The following option keys are supported:
* 'table' => The database table to validate against
* 'schema' => The schema keys
* 'field' => The field to check for a match
* 'exclude' => An optional where clause or field/value pair to exclude from the query
* 'adapter' => An optional database adapter to use
*
* @param array|Traversable|DbSelect $options Options to use for this validator
* @throws \Zend\Validator\Exception\InvalidArgumentException
*/
public function __construct($options = null)
{
parent::__construct();
if ($options instanceof DbSelect) {
$this->setSelect($options);
return;
}
if ($options instanceof Traversable) {
$options = ArrayUtils::iteratorToArray($options);
} elseif (func_num_args() > 1) {
$options = func_get_args();
$firstArgument = array_shift($options);
if (is_array($firstArgument)) {
$temp = ArrayUtils::iteratorToArray($firstArgument);
} else {
$temp['table'] = $firstArgument;
}
$temp['field'] = array_shift($options);
if (!empty($options)) {
$temp['exclude'] = array_shift($options);
}
if (!empty($options)) {
$temp['include'] = array_shift($options);
}
if (!empty($options)) {
$temp['adapter'] = array_shift($options);
}
$options = $temp;
}
if (!array_key_exists('table', $options) && !array_key_exists('schema', $options)) {
throw new Exception\InvalidArgumentException('Table or Schema option missing!');
}
if (!array_key_exists('field', $options)) {
throw new Exception\InvalidArgumentException('Field option missing!');
}
if (array_key_exists('adapter', $options)) {
$this->setAdapter($options['adapter']);
}
if (array_key_exists('exclude', $options)) {
$this->setExclude($options['exclude']);
}
if (array_key_exists('include', $options)) {
$this->setInclude($options['include']);
}
$this->setField($options['field']);
if (array_key_exists('table', $options)) {
$this->setTable($options['table']);
}
if (array_key_exists('schema', $options)) {
$this->setSchema($options['schema']);
}
}
/**
* Returns the set adapter
*
* @throws \Zend\Validator\Exception\RuntimeException When no database adapter is defined
* @return DbAdapter
*/
public function getAdapter()
{
return $this->adapter;
}
/**
* Sets a new database adapter
*
* @param DbAdapter $adapter
* @return self Provides a fluent interface
*/
public function setAdapter(DbAdapter $adapter)
{
$this->adapter = $adapter;
return $this;
}
/**
* Returns the set exclude clause
*
* @return string|array
*/
public function getExclude()
{
return $this->exclude;
}
/**
* Returns the set include clause
*
* @return string|array
*/
public function getInclude()
{
return $this->include;
}
/**
* Sets a new exclude clause
*
* @param string|array $exclude
* @return self Provides a fluent interface
*/
public function setExclude($exclude)
{
$this->exclude = $exclude;
return $this;
}
/**
* Sets a new include clause
*
* @param string|array $include
* @return self Provides a fluent interface
*/
public function setInclude($include)
{
$this->include = $include;
return $this;
}
/**
* Returns the set field
*
* @return string|array
*/
public function getField()
{
return $this->field;
}
/**
* Sets a new field
*
* @param string $field
* @return AbstractDb
*/
public function setField($field)
{
$this->field = (string)$field;
return $this;
}
/**
* Returns the set table
*
* @return string
*/
public function getTable()
{
return $this->table;
}
/**
* Sets a new table
*
* @param string $table
* @return self Provides a fluent interface
*/
public function setTable($table)
{
$this->table = (string)$table;
return $this;
}
/**
* Returns the set schema
*
* @return string
*/
public function getSchema()
{
return $this->schema;
}
/**
* Sets a new schema
*
* @param string $schema
* @return self Provides a fluent interface
*/
public function setSchema($schema)
{
$this->schema = $schema;
return $this;
}
/**
* Sets the select object to be used by the validator
*
* @param DbSelect $select
* @return self Provides a fluent interface
*/
public function setSelect(DbSelect $select)
{
$this->select = $select;
return $this;
}
/**
* Gets the select object to be used by the validator.
* If no select object was supplied to the constructor,
* then it will auto-generate one from the given table,
* schema, field, and adapter options.
*
* @return DbSelect The Select object which will be used
*/
public function getSelect()
{
if ($this->select instanceof DbSelect) {
return $this->select;
}
$adapter = $this->getAdapter();
$driver = $adapter->getDriver();
$platform = $adapter->getPlatform();
/*
* Build select object
*/
$select = new DbSelect();
$tableIdentifier = new TableIdentifier($this->table, $this->schema);
$select->from($tableIdentifier)->columns(
array($this->field)
);
// Support both named and positional parameters
if (DbDriverInterface::PARAMETERIZATION_NAMED == $driver->getPrepareType()) {
$select->where(
$platform->quoteIdentifier($this->field, true) . ' = :value'
);
} else {
$select->where(
$platform->quoteIdentifier($this->field, true) . ' = ?'
);
}
if ($this->exclude !== null) {
if (is_array($this->exclude)) {
$select->where->notEqualTo(
$this->exclude['field'],
$this->exclude['value']
);
} else {
$select->where($this->exclude);
}
}
if ($this->include !== null) {
if (is_array($this->include)) {
$select->where->equalTo(
$this->include['field'],
$this->include['value']
);
} else {
$select->where($this->include);
}
}
$this->select = $select;
return $this->select;
}
/**
* Run query and returns matches, or null if no matches are found.
*
* @param string $value
* @return array when matches are found.
*/
protected function query($value)
{
$adapter = $this->getAdapter();
$statement = $adapter->createStatement();
$this->getSelect()->prepareStatement($adapter, $statement);
return $statement->execute(array('value' => $value))->current();
}
}
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Validator
*/
namespace Zend\Validator\Db;
use Zend\Validator\Exception;
/**
* Confirms a record does not exist in a table.
*
* @category Zend
* @package Zend_Validate
*/
class CustomNoRecordExists extends CustomAbstractDb
{
public function isValid($value)
{
/*
* Check for an adapter being defined. If not, throw an exception.
*/
if (null === $this->adapter) {
throw new Exception\RuntimeException('No database adapter present');
}
$valid = true;
$this->setValue($value);
$result = $this->query($value);
if ($result) {
$valid = false;
$this->error(self::ERROR_RECORD_FOUND);
}
return $valid;
}
}
<?php
//Check no other users have the username
$user_id = $user->getId();
$validator = new Zend\Validator\Db\CustomNoRecordExists(
array(
'table' => 'users',
'field' => 'username',
'include' => array(
'field' => 'id',
'value' => $user_id
)
)
);
if ($validator->isValid($username)) {
// username appears to be valid
} else {
// username is invalid; print the reason
$messages = $validator->getMessages();
foreach ($messages as $message) {
echo "$message\n";
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment