Giter VIP home page Giter VIP logo

command-validator's People

Contributors

cerbero90 avatar gregpeden avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

command-validator's Issues

Validation is performed on optional parameters in Laravel 5.3, fails if not provided

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.

[New Feature] Should add way to validate --option

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.

console.php / Artisan::command

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 ๐Ÿ‘

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.