-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit fcec490
Showing
9 changed files
with
205 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/vendor | ||
composer.lock | ||
.phpunit.result.cache | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
// ... | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.'); | ||
} | ||
} |