Files
unity/app/Http/Rules/Admin/ExistCheck.php
T

149 lines
3.8 KiB
PHP

<?php
namespace App\Http\Rules\Admin;
use App\Models\Country;
use App\Models\Role;
use App\Models\Permission;
use App\Models\Currency;
use App\Models\Language;
use App\Models\MediaPropertyType;
use App\Models\MimeType;
use App\Http\Helpers\Hasher;
use Illuminate\Contracts\Validation\Rule;
class ExistCheck implements Rule
{
/**
* Create a new rule instance.
*
* @return void
*/
private $type;
private $should_not;
public function __construct($type = '', $should_not = false)
{
$this->type = $type;
$this->should_not = $should_not;
}
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
$this->attribute = $attribute;
$value = is_array($value) ? $value : [$value];
if ($this->type == 'country') {
foreach ($value as $key => $row) {
if(!empty($row)) {
$id = Hasher::decode('countries', $row);
$data = Country::find($id);
if (!$data) {
return false;
}
}
}
}
if ($this->type == 'role') {
foreach ($value as $key => $row) {
if(!empty($row)) {
$id = Hasher::decode('roles', $row);
$data = Role::find($id);
if (!$data) {
return false;
}
}
}
}
if ($this->type == 'permission') {
foreach ($value as $key => $row) {
if(!empty($row)) {
$id = Hasher::decode('permissions', $row);
$data = Permission::find($id);
if (!$data) {
return false;
}
}
}
}
if ($this->type == 'currency') {
foreach ($value as $key => $row) {
if(!empty($row)) {
$id = Hasher::decode('currencies', $row);
$data = Currency::find($id);
if (!$data) {
return false;
}
}
}
}
if ($this->type == 'language') {
foreach ($value as $key => $row) {
if(!empty($row)) {
$id = Hasher::decode('languages', $row);
$data = Language::find($id);
if (!$data) {
return false;
}
}
}
}
if ($this->type == 'media_property_type') {
foreach ($value as $key => $row) {
if(!empty($row)) {
$id = Hasher::decode('media_property_types', $row);
$data = MediaPropertyType::find($id);
if (!$data) {
return false;
}
}
}
}
if ($this->type == 'mime_type') {
foreach ($value as $key => $row) {
if(!empty($row)) {
$id = Hasher::decode('mime_types', $row);
$data = MimeType::find($id);
if (!$data) {
return false;
}
}
}
}
return true;
}
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
if (!$this->should_not) {
return config('error_code.exist_error');
}
else {
return config('error_code.unique_error');
}
}
}