<?php
namespace Moto\Application\Content\Validator; use Moto; use Illuminate; class UniqueTaxonomy extends Moto\Validator\AbstractValidator { const ERROR_NOT_UNIQUE = 'notUnique'; const ERROR_INVALID_OPTIONS = 'invalidOptions'; protected $messageTemplates = array( self::ERROR_NOT_UNIQUE => 'Value is not unique', self::ERROR_INVALID_OPTIONS => 'Validator options is invalid', ); public function isValid($value, array $context = array()) { $model = $this->getOption('model'); if (!($model instanceof Moto\Database\ContentTaxonomy)) { $this->error(static::ERROR_INVALID_OPTIONS); return false; } $field = $this->getOption('field'); if (!is_string($field) || empty($field)) { $this->error(static::ERROR_INVALID_OPTIONS); return false; } $query = $model->newQuery(); $query->where($field, '=', $value); if (Moto\Util::arrayHas($context, 'parent_id')) { $query->where('parent_id', '=', (int) $context['parent_id']); } if (empty($model->type)) { $type = trim(Moto\Util::getValue($context, 'type')); if (!empty($type)) { $query->where('type', '=', $context['type']); } } $id = (int) Moto\Util::getValue($context, 'id'); if ($id > 0) { $query->where('id', '!=', $id); } $record = $query->first(); if ($record) { $this->error(static::ERROR_NOT_UNIQUE); return false; } return true; } } 