<?php
namespace Moto\Application\MediaLibrary\Validator; use Zend\Db\Sql\Select; use Zend\Validator\Db\NoRecordExists; class UniqueTreeItem extends NoRecordExists { const ERROR_NOT_STRING = 'notString'; protected $_parentId = null; protected $_context = null; function __construct($options = null) { $this->messageTemplates = array_merge($this->messageTemplates, array( self::ERROR_NOT_STRING => 'Value is not string' )); parent::__construct($options); if (array_key_exists('parent_id', $options)) { $this->setParentId($options['parent_id']); } } function setParentId($parentId) { $this->_parentId = $parentId; return $this; } public function isValid($value, array $context = array()) { $this->_context = $context; if (!is_string($value)) { $this->error(self::ERROR_NOT_STRING); return false; } if (empty($value)) { return true; } if (is_null($this->_parentId) && array_key_exists('parent_id', $this->_context)) { $this->setParentId(array_key_exists('parent_id', $this->_context) ? $this->_context['parent_id'] : 0); } return parent::isValid($value); } public function getSelect() { $select = parent::getSelect(); $select->where->equalTo('parent_id', $this->_parentId); return $select; } }