Created
June 27, 2018 06:08
-
-
Save virdignus/42a45f2c896655bfb72d418ec78a6788 to your computer and use it in GitHub Desktop.
checking array of inputs for equal to some value
This file contains hidden or 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
| <?php | |
| namespace App\Rules; | |
| use App\Services\Helpers\Helper; | |
| use Illuminate\Contracts\Validation\Rule; | |
| class EqualTo implements Rule | |
| { | |
| /** | |
| * Create a new rule instance. | |
| * | |
| * @return void | |
| */ | |
| private $equal; | |
| private $key; | |
| public function __construct($equal, $key) | |
| { | |
| $this->equal = $equal; | |
| $this->key = $key; | |
| } | |
| /** | |
| * Determine if the validation rule passes. | |
| * | |
| * @param string $attribute | |
| * @param mixed $value | |
| * @return bool | |
| */ | |
| public function passes($attribute, $value) | |
| { | |
| if (is_array($value)) { | |
| $filtered_array = array_filter($value); | |
| $percents = array_map(function ($array) { | |
| if (isset($array[$this->key])) { | |
| return Helper::commaToDot($array[$this->key]); | |
| } | |
| return 0; | |
| }, $filtered_array); | |
| return array_sum($percents) == $this->equal; | |
| } | |
| } | |
| /** | |
| * Get the validation error message. | |
| * | |
| * @return string | |
| */ | |
| public function message() | |
| { | |
| return trans('validation.percent_error'); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment