Skip to content

Commit

Permalink
new MinMaxValidator
Browse files Browse the repository at this point in the history
  • Loading branch information
nateiler committed Apr 25, 2018
1 parent fa14182 commit c78b270
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
Changelog
=========

## Unreleased

### Added
- MinMaxValidator which extends Craft's MinMaxValidator except it also works with query value attributes

## 1.0.0 - 2018-04-24
- Production release

Expand Down
88 changes: 88 additions & 0 deletions src/validators/MinMaxValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

/**
* @copyright Copyright (c) Flipbox Digital Limited
* @license https://github.com/flipboxfactory/craft-ember/blob/master/LICENSE
* @link https://github.com/flipboxfactory/craft-ember
*/

namespace flipbox\domains\validators;

use craft\validators\ArrayValidator;
use yii\db\QueryInterface;

/**
* @author Flipbox Factory <hello@flipboxfactory.com>
* @since 1.0.0
*/
class MinMaxValidator extends ArrayValidator
{
/**
* @inheritdoc
*/
public function validateAttribute($model, $attribute)
{
$value = $model->$attribute;

if($value instanceof QueryInterface) {
return $this->validateQueryAttribute($model, $attribute, $value);
}

return parent::validateAttribute($model, $attribute);
}

/**
* @inheritdoc
*/
protected function validateValue($value)
{
if($value instanceof QueryInterface) {
return $this->validateQueryValue($value);
}

return parent::validateValue($value);
}

/**
* @param QueryInterface $query
* @return array|null the error message and the parameters to be inserted into the error message.
* Null should be returned if the data is valid.
*/
protected function validateQueryValue(QueryInterface $query)
{
/** @var QueryInterface $value */
$count = $query->count();

if ($this->min !== null && $count < $this->min) {
return [$this->tooFew, ['min' => $this->min]];
}
if ($this->max !== null && $count > $this->max) {
return [$this->tooMany, ['max' => $this->max]];
}
if ($this->count !== null && $count !== $this->count) {
return [$this->notEqual, ['count' => $this->count]];
}

return null;
}

/**
* @param $model
* @param $attribute
* @param QueryInterface $query
*/
protected function validateQueryAttribute($model, $attribute, QueryInterface $query)
{
$count = $query->count();

if ($this->min !== null && $count < $this->min) {
$this->addError($model, $attribute, $this->tooFew, ['min' => $this->min]);
}
if ($this->max !== null && $count > $this->max) {
$this->addError($model, $attribute, $this->tooMany, ['max' => $this->max]);
}
if ($this->count !== null && $count !== $this->count) {
$this->addError($model, $attribute, $this->notEqual, ['count' => $this->count]);
}
}
}

0 comments on commit c78b270

Please sign in to comment.