cerbero90 / command-validator Goto Github PK
View Code? Open in Web Editor NEWValidate Laravel console commands input.
License: MIT License
Validate Laravel console commands input.
License: MIT License
Per the subject, optional parameters have validation performed on them. I'm not sure if they are being passed '' (empty string) or NULL or whatever, but validation which is checking for format conformity is run and then fails.
I first observed this after an update to Laravel 5.3. I will investigate a bit, and maybe provide a PR if I figure it out.
Reference code being executed in Laravel 5.3 (yes it's a bit sloppy, was written as prototype with cleanup forthcoming, sorry!)
<?php
namespace App\Console\Commands;
use App\Models\Toggl\TogglTimeEntry;
use App\Models\Toggl\TogglUser;
use App\Models\User\User;
use Carbon\Carbon;
use Cerbero\CommandValidator\ValidatesInput;
use Illuminate\Console\Command;
class TogglPullTimeEntries extends Command
{
use ValidatesInput;
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'toggl:pulltimeentries
{users? : (optional) Comma-separated list of user IDs to be synced, default is all Toggl users.},
{--S|since= : Start date for range to be synced, default is the most recent sync date in existing records for each user.},
{--U|until= : End date for range to be synced, default is today.},
{--P|page= : Toggl API data page number to start retrieval. If not defined, all pages within the scope will be retrieved starting with page 1.},
{--Q|queued : Perform action asynchronously with use of Laravel\'s Jobs feature.},
{--N|noNextPage : Do not pull the next page in the data set.}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Pull activity entries from Toggl to local via API.';
protected function rules()
{
return [
//users regex expression is for comma-separated list of numbers
'users' => 'regex:/^\s*\d+(\s*,+\s*\d+)*\s*$/',
'since' => 'date_format:Y-m-d',
'until' => 'date_format:Y-m-d',
'page' => 'integer',
'queued' => 'boolean',
'noNextPage' => 'boolean'
];
}
protected function messages()
{
return [
'users.regex' => '\'Users\' argument must be an integer or comma-separated list of integers representing Users with related Toggl account.'
];
}
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
if (!\Config::get('toggl.enabled')) {
$this->error('Toggl API function is disabled');
return;
}
$users = $this->argument('users');
$query = User::query()->whereHas('toggl', function($q) {
$q->where('membership_status', TogglUser::WORKSPACE_STATUS_MEMBER);
});
if($users !== null) {
$query = $query->whereIn('id', explode(',', str_replace(' ', '', $users)));
}
$usersList = $query->get();
$params = [];
if($usersList->count() > 0) {
if($this->option('queued')) {
foreach ($usersList as $user) {
/** @var User $user */
$user->getTogglTimeEntriesJob(Carbon::parse($this->option('since')), Carbon::parse($this->option('until')), $this->option('page'), !is_null($this->option('noNextPage')));
}
$this->line('Toggl time entries retrieval queued for ' . $usersList->count() . ' users.');
} else {
if ($this->option('since') !== null)
$params['since'] = Carbon::parse($this->option('since'))->toDateString();
if ($this->option('until') !== null)
$params['until'] = Carbon::parse($this->option('until'))->toDateString();
if ($this->option('page') !== null)
$params['page'] = $this->option('page');
$doNextPage = !is_null($this->option('noNextPage'));
$this->line('Beginning Toggl API time entries fetch for ' . $usersList->count() . ' users...');
$progressBar = $this->output->createProgressBar(count($usersList));
foreach ($usersList as $user) {
/** @var User $user */
TogglTimeEntry::fetch($user, $params, $doNextPage);
$progressBar->advance();
}
$progressBar->finish();
$this->line(PHP_EOL . '...Toggl time entries sync complete!');
}
} else {
$this->error('No Toggl API users in set.');
}
}
}
Console output on running "php artisan toggl:pulltimeentries":
'Users' argument must be an integer or comma-separated list of integers representing Users with related Toggl account.
The since does not match the format Y-m-d.
The until does not match the format Y-m-d.
The page must be an integer.
Usually, I use --option instead of argument in case like.
php artisan command --user=1 --date=1 --partner=1
so any option can be optional and that value will be default.
If I use argument. php artisan command 1
I don't know which argument is it.
Just bumped into this - just what I need!
Any chance this can be made to work inside an Artisan::command
closure? If there is already a way, what is it? Would also be great if we could validate ask()
input - not sure how that could be done, if at all possible...
Thanks ๐
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.