File: /mnt/data/ghayatcom/ghayatcom-api/app/Rules/CommaSeparatorCount.php
<?php
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
/**
* App\Rules
*
* @property string $name
*/
class CommaSeparatorCount implements Rule
{
/**
* Create a new rule instance.
*
* @return void
*/
public function __construct($name)
{
$this->name = $name;
}
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param string $value
* @return bool
*/
public function passes($attribute, $value)
{
if ((! empty($this->name)) || (! empty($value))) {
if (isset($this->name)) {
$nameExplode = explode(',', $this->name);
} else {
$nameExplode = [];
}
$nameCount = count(array_filter($nameExplode));
$valueCount = count(explode(',', $value));
$sum = $valueCount + $nameCount;
/** @var integer $sum */
return ($sum != 0) ? true : false;
} else {
return true;
}
}
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return 'The :attribute must be minimum 2.';
}
}