1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* NOTICE OF LICENSE |
5
|
|
|
* |
6
|
|
|
* Part of the Rinvex Fort Package. |
7
|
|
|
* |
8
|
|
|
* This source file is subject to The MIT License (MIT) |
9
|
|
|
* that is bundled with this package in the LICENSE file. |
10
|
|
|
* |
11
|
|
|
* Package: Rinvex Fort Package |
12
|
|
|
* License: The MIT License (MIT) |
13
|
|
|
* Link: https://rinvex.com |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace Rinvex\Fort\Console\Commands; |
17
|
|
|
|
18
|
|
|
use Rinvex\Fort\Models\Ability; |
19
|
|
|
use Illuminate\Console\Command; |
20
|
|
|
use Illuminate\Support\Facades\Lang; |
21
|
|
|
use Illuminate\Contracts\Validation\Factory; |
22
|
|
|
|
23
|
|
|
class AbilityUpdateCommand extends Command |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* The name and signature of the console command. |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $signature = 'fort:ability:update |
31
|
|
|
{field? : The identifier of the ability (id, email, abilityname)} |
32
|
|
|
{--N|name= : The name of the ability} |
33
|
|
|
{--S|slug= : The slug of the ability} |
34
|
|
|
{--D|description= : The description of the ability}'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The console command description. |
38
|
|
|
* |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
protected $description = 'Update an existing ability.'; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Execute the console command. |
45
|
|
|
* |
46
|
|
|
* @return void |
47
|
|
|
*/ |
48
|
|
|
public function handle() |
49
|
|
|
{ |
50
|
|
|
$data = array_filter([ |
51
|
|
|
|
52
|
|
|
// Required ability attributes |
53
|
|
|
'name' => $this->option('name'), |
54
|
|
|
'slug' => $this->option('slug'), |
55
|
|
|
'description' => $this->option('description'), |
56
|
|
|
|
57
|
|
|
], [ |
58
|
|
|
$this, |
59
|
|
|
'filter', |
60
|
|
|
]); |
61
|
|
|
|
62
|
|
|
// Get required argument |
63
|
|
|
$field = $this->argument('field') ?: $this->ask(Lang::get('rinvex.fort::artisan.ability.invalid')); |
64
|
|
|
|
65
|
|
|
// Find single ability |
66
|
|
|
if (intval($field)) { |
67
|
|
|
$ability = Ability::find($field); |
68
|
|
|
} else { |
69
|
|
|
$ability = Ability::where(['slug' => $field])->first(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
if (! $ability) { |
73
|
|
|
return $this->error(Lang::get('rinvex.fort::artisan.ability.invalid', ['field' => $field])); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$rules = [ |
77
|
|
|
'name' => 'sometimes|required|max:255', |
78
|
|
|
'slug' => 'sometimes|required|max:255|unique:'.config('rinvex.fort.tables.abilities'), |
79
|
|
|
]; |
80
|
|
|
|
81
|
|
|
if (! empty($data)) { |
82
|
|
|
$validator = app(Factory::class)->make($data, $rules); |
83
|
|
|
|
84
|
|
|
if ($validator->fails()) { |
85
|
|
|
$this->error('Errors:'); |
86
|
|
|
|
87
|
|
|
foreach ($validator->errors()->getMessages() as $key => $messages) { |
88
|
|
|
$this->error('- '.$key.': '.$messages[0]); |
89
|
|
|
} |
90
|
|
|
} else { |
91
|
|
|
$ability->update($data); |
92
|
|
|
|
93
|
|
|
$this->info(Lang::get('rinvex.fort::artisan.ability.updated').' ['.Lang::get('rinvex.fort::artisan.ability.id').': '.$ability->id.', '.Lang::get('rinvex.fort::artisan.ability.name').': '.$ability->name.', '.Lang::get('rinvex.fort::artisan.ability.slug').': '.$ability->slug.']'); |
|
|
|
|
94
|
|
|
} |
95
|
|
|
} else { |
96
|
|
|
$this->info(Lang::get('rinvex.fort::artisan.ability.nothing')); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Filter null and empty values. |
102
|
|
|
* |
103
|
|
|
* @param $value |
104
|
|
|
* |
105
|
|
|
* @return bool |
106
|
|
|
*/ |
107
|
|
|
protected function filter($value) |
108
|
|
|
{ |
109
|
|
|
return $value !== null && $value !== ''; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.