Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
johanrosenson committed Mar 1, 2021
0 parents commit fcec490
Show file tree
Hide file tree
Showing 9 changed files with 205 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 4
trim_trailing_whitespace = true
8 changes: 8 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
*.md diff=markdown
*.php diff=php

/.editorconfig export-ignore
/.gitignore export-ignore
/.gitattributes export-ignore
/phpunit.xml.dist export-ignore
/tests export-ignore
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/vendor
composer.lock
.phpunit.result.cache
.DS_Store
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) Johan Rosenson

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
55 changes: 55 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<p align="center">
<a href="https://packagist.org/packages/devlop/laravel-console-input-validation"><img src="https://img.shields.io/packagist/v/devlop/laravel-console-input-validation" alt="Latest Stable Version"></a>
<a href="https://github.com/devlop-ab/laravel-console-input-validation/blob/master/LICENSE.md"><img src="https://img.shields.io/packagist/l/devlop/laravel-console-input-validation" alt="License"></a>
</p>

# Laravel Console Input Validation

A small trait to make it easier to validate the input to your Laravel commands.

# Installation

```bash
composer require devlop/laravel-console-input-validation
```

# Usage

```
use Devlop\Laravel\Console\ValidateInput;
use InvalidArgumentException;
use Symfony\Component\Console\Input\InputInterface;
use Webmozart\Assert\Assert; // not required, but used in the example below
class DemoCommand extends Command
{
use ValidateInput;
public function __construct()
{
parent::__construct();
}
/**
* Validate the console command input.
*
* @throws InvalidArgumentException
*/
protected function validate(InputInterface $input) : void
{
// Example using manual validation
if (! is_numeric($input->getOption('limit'))) {
throw new RuntimeException('--limit must be numeric');
}
// Example using webmozarts/assert:
Assert::numeric($input->getOption('limit')); // assert that the --limit option got a numeric value
Assert::greaterThan($input->getOption('limit'), 0); // assert that the --limit option get a value greater than 0
}
public function handle() : int
{
// ...
}
}
```
38 changes: 38 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "devlop/laravel-console-input-validation",
"description": "Trait to simplify the validation of console input in Laravel commands",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Johan Rosenson",
"email": "johan@devlop.se"
}
],
"require": {
"php": "^7.4|^8.0",
"illuminate/console": "^8.0",
"psr/log": "^1.0.1"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"symfony/var-dumper": "^5.2"
},
"autoload": {
"psr-4": {
"Devlop\\Laravel\\Console\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Devlop\\Laravel\\Console\\Tests\\": "tests/"
}
},
"minimum-stability": "dev",
"prefer-stable": true,
"extra": {
"branch-alias": {
"dev-master": "1.x-dev"
}
}
}
17 changes: 17 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
verbose="true">
<testsuites>
<testsuite name="devlop/laravel-command-validation Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./src</directory>
</include>
</coverage>
</phpunit>
38 changes: 38 additions & 0 deletions src/ValidateInput.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Devlop\Laravel\Console;

use InvalidArgumentException;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

trait ValidateInput
{
/**
* Validate the console command input.
*
* @throws InvalidArgumentException
*/
protected function validate(InputInterface $input) : void
{
//
}

/**
* Execute the console command.
*/
protected function execute(InputInterface $input, OutputInterface $output) : int
{
try {
$this->validate($input);
} catch (InvalidArgumentException $e) {
$this->error($e->getMessage());

return 1;
}

return parent::execute($input, $output);
}
}
15 changes: 15 additions & 0 deletions tests/ValidateInputTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Devlop\Laravel\Console\Tests;

use PHPUnit\Framework\TestCase;

final class ValidateInputTest extends TestCase
{
public function test_validate_method_is_called() : void
{
$this->markTestIncomplete('This test has not been implemented yet.');
}
}

0 comments on commit fcec490

Please sign in to comment.