Skip to content

Commit fcec490

Browse files
committed
Initial Commit
0 parents  commit fcec490

9 files changed

+205
-0
lines changed

.editorconfig

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
end_of_line = lf
6+
insert_final_newline = true
7+
indent_style = space
8+
indent_size = 4
9+
trim_trailing_whitespace = true

.gitattributes

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
*.md diff=markdown
2+
*.php diff=php
3+
4+
/.editorconfig export-ignore
5+
/.gitignore export-ignore
6+
/.gitattributes export-ignore
7+
/phpunit.xml.dist export-ignore
8+
/tests export-ignore

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/vendor
2+
composer.lock
3+
.phpunit.result.cache
4+
.DS_Store

LICENSE.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) Johan Rosenson
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<p align="center">
2+
<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>
3+
<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>
4+
</p>
5+
6+
# Laravel Console Input Validation
7+
8+
A small trait to make it easier to validate the input to your Laravel commands.
9+
10+
# Installation
11+
12+
```bash
13+
composer require devlop/laravel-console-input-validation
14+
```
15+
16+
# Usage
17+
18+
```
19+
use Devlop\Laravel\Console\ValidateInput;
20+
use InvalidArgumentException;
21+
use Symfony\Component\Console\Input\InputInterface;
22+
use Webmozart\Assert\Assert; // not required, but used in the example below
23+
24+
class DemoCommand extends Command
25+
{
26+
use ValidateInput;
27+
28+
public function __construct()
29+
{
30+
parent::__construct();
31+
}
32+
33+
/**
34+
* Validate the console command input.
35+
*
36+
* @throws InvalidArgumentException
37+
*/
38+
protected function validate(InputInterface $input) : void
39+
{
40+
// Example using manual validation
41+
if (! is_numeric($input->getOption('limit'))) {
42+
throw new RuntimeException('--limit must be numeric');
43+
}
44+
45+
// Example using webmozarts/assert:
46+
Assert::numeric($input->getOption('limit')); // assert that the --limit option got a numeric value
47+
Assert::greaterThan($input->getOption('limit'), 0); // assert that the --limit option get a value greater than 0
48+
}
49+
50+
public function handle() : int
51+
{
52+
// ...
53+
}
54+
}
55+
```

composer.json

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"name": "devlop/laravel-console-input-validation",
3+
"description": "Trait to simplify the validation of console input in Laravel commands",
4+
"type": "library",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Johan Rosenson",
9+
"email": "[email protected]"
10+
}
11+
],
12+
"require": {
13+
"php": "^7.4|^8.0",
14+
"illuminate/console": "^8.0",
15+
"psr/log": "^1.0.1"
16+
},
17+
"require-dev": {
18+
"phpunit/phpunit": "^9.5",
19+
"symfony/var-dumper": "^5.2"
20+
},
21+
"autoload": {
22+
"psr-4": {
23+
"Devlop\\Laravel\\Console\\": "src/"
24+
}
25+
},
26+
"autoload-dev": {
27+
"psr-4": {
28+
"Devlop\\Laravel\\Console\\Tests\\": "tests/"
29+
}
30+
},
31+
"minimum-stability": "dev",
32+
"prefer-stable": true,
33+
"extra": {
34+
"branch-alias": {
35+
"dev-master": "1.x-dev"
36+
}
37+
}
38+
}

phpunit.xml.dist

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
4+
bootstrap="vendor/autoload.php"
5+
colors="true"
6+
verbose="true">
7+
<testsuites>
8+
<testsuite name="devlop/laravel-command-validation Test Suite">
9+
<directory suffix="Test.php">./tests</directory>
10+
</testsuite>
11+
</testsuites>
12+
<coverage processUncoveredFiles="true">
13+
<include>
14+
<directory suffix=".php">./src</directory>
15+
</include>
16+
</coverage>
17+
</phpunit>

src/ValidateInput.php

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Devlop\Laravel\Console;
6+
7+
use InvalidArgumentException;
8+
use Symfony\Component\Console\Input\InputInterface;
9+
use Symfony\Component\Console\Output\OutputInterface;
10+
11+
trait ValidateInput
12+
{
13+
/**
14+
* Validate the console command input.
15+
*
16+
* @throws InvalidArgumentException
17+
*/
18+
protected function validate(InputInterface $input) : void
19+
{
20+
//
21+
}
22+
23+
/**
24+
* Execute the console command.
25+
*/
26+
protected function execute(InputInterface $input, OutputInterface $output) : int
27+
{
28+
try {
29+
$this->validate($input);
30+
} catch (InvalidArgumentException $e) {
31+
$this->error($e->getMessage());
32+
33+
return 1;
34+
}
35+
36+
return parent::execute($input, $output);
37+
}
38+
}

tests/ValidateInputTest.php

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Devlop\Laravel\Console\Tests;
6+
7+
use PHPUnit\Framework\TestCase;
8+
9+
final class ValidateInputTest extends TestCase
10+
{
11+
public function test_validate_method_is_called() : void
12+
{
13+
$this->markTestIncomplete('This test has not been implemented yet.');
14+
}
15+
}

0 commit comments

Comments
 (0)