Add CityValidationByName.
continuous-integration/drone/push Build is passing

This commit is contained in:
2023-08-18 13:20:09 +03:00
parent f437d777a5
commit 1a7c585aa7
6 changed files with 96 additions and 59 deletions
+9 -17
View File
@@ -6,9 +6,10 @@ use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use App\Models\Validation\ValidationByNameInterface;
use App\Models\Company;
class City extends Model
class City extends Model implements ValidationByNameInterface
{
use HasFactory;
@@ -58,24 +59,15 @@ class City extends Model
$this->companies()->detach($company_ids);
}
public static function validateWithName(string $name)
public static function isExistByName(string $name): bool
{
$name = $name ?? '';
$count = City::where('name', $name)->count();
if($name == '')
return [
'error' => 'The city name is empty, please, write the name!!!'
];
return $count != 0;
}
$city = City::where('name', $name)->first();
if(!$city)
return [
'error' => 'A city with the name does not exist!!!'
];
return [
'ok' => 'A city with the name is valid.'
];
public static function isNameValid(string $name): bool
{
return $name != '';
}
}
@@ -0,0 +1,18 @@
<?php
namespace App\Models\Validation;
use App\Models\Validation\ModelValidationByName;
use App\Models\Validation\Messages\CityMessagesFactory;
class CityValidationByName extends ModelValidationByName
{
public function __construct(string $name)
{
parent::__construct(
$name,
'App\Models\City',
(new CityMessagesFactory())->create(),
);
}
}
@@ -0,0 +1,20 @@
<?php
namespace App\Models\Validation\Messages;
use App\Models\Validation\Messages\BaseMessages;
class CityMessagesFactory
{
protected array $messages = [
'found' => 'A city with the name is valid.',
'not_found' => 'A city with the name does not exist!!!',
'invalid_name' => 'The city name is empty, please, write the name!!!',
];
public function create()
{
return new BaseMessages($this->messages);
}
}