-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} | ||
} | ||
} |