Skip to content
This repository was archived by the owner on Apr 13, 2022. It is now read-only.

Commit 593f5d5

Browse files
Initial commit
0 parents  commit 593f5d5

13 files changed

+483
-0
lines changed

.gitattributes

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
* text=auto
2+
3+
/tests export-ignore
4+
/.gitattributes export-ignore
5+
/.gitignore export-ignore
6+
/.travis.yml export-ignore
7+
/phpunit.xml.dist export-ignore
8+
/CONTRIBUTING.md export-ignore
9+
/README.md export-ignore

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
composer.lock
2+
phpunit.xml
3+
vendor

.travis.yml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
language: php
2+
3+
php:
4+
- 5.5.9
5+
- 5.5
6+
- 5.6
7+
- hhvm
8+
9+
sudo: false
10+
11+
install:
12+
- travis_retry composer install --no-interaction --prefer-source
13+
14+
script:
15+
- bash -c 'if [ "$TRAVIS_PHP_VERSION" == "hhvm" ]; then vendor/bin/phpunit; fi;'
16+
- bash -c 'if [ "$TRAVIS_PHP_VERSION" != "hhvm" ]; then vendor/bin/phpunit --coverage-clover build/logs/clover.xml; fi;'
17+
18+
after_script:
19+
- bash -c 'if [ "$TRAVIS_PHP_VERSION" != "hhvm" ]; then wget https://scrutinizer-ci.com/ocular.phar; fi;'
20+
- bash -c 'if [ "$TRAVIS_PHP_VERSION" != "hhvm" ]; then php ocular.phar code-coverage:upload --format=php-clover build/logs/clover.xml; fi;'

CONTRIBUTING.md

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
CONTRIBUTING
2+
============
3+
4+
5+
Contributions are welcome, and are accepted via pull requests. Please review these guidelines before submitting any pull requests.
6+
7+
8+
## Guidelines
9+
10+
* Please follow the [PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md) and [PHP-FIG Naming Conventions](https://github.com/php-fig/fig-standards/blob/master/bylaws/002-psr-naming-conventions.md).
11+
* Ensure that the current tests pass, and if you've added something new, add the tests where relevant.
12+
* Remember that we follow [SemVer](http://semver.org). If you are changing the behaviour, or the public api, you may need to update the docs.
13+
* Send a coherent commit history, making sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash](http://git-scm.com/book/en/Git-Tools-Rewriting-History) them before submitting.
14+
* You may also need to [rebase](http://git-scm.com/book/en/Git-Branching-Rebasing) to avoid merge conflicts.
15+
16+
17+
## Running Tests
18+
19+
You will need an install of [Composer](https://getcomposer.org) before continuing.
20+
21+
First, install the dependencies:
22+
23+
```bash
24+
$ composer install
25+
```
26+
27+
Then run phpunit:
28+
29+
```bash
30+
$ vendor/bin/phpunit
31+
```
32+
33+
If the test suite passes on your local machine you should be good to go.
34+
35+
When you make a pull request, the tests will automatically be run again by [Travis CI](https://travis-ci.org/) on multiple php versions and hhvm.

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2015 Alt Three LTD <[email protected]>
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
13+
all 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
21+
THE SOFTWARE.

README.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Alt Three Validator
2+
3+
A validation wrapper for Laravel 5.
4+
5+
6+
## Installation
7+
8+
[PHP](https://php.net) 5.5+ or [HHVM](http://hhvm.com) 3.6+, and [Composer](https://getcomposer.org) are required.
9+
10+
To get the latest version of Alt Three Validator, simply add the following line to the require block of your `composer.json` file:
11+
12+
```
13+
"alt-three/validator": "~1.0"
14+
```
15+
16+
You'll then need to run `composer install` or `composer update` to download it and have the autoloader updated.
17+
18+
19+
## License
20+
21+
Alt Three Validator is licensed under [The MIT License (MIT)](LICENSE).

composer.json

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{
2+
"name": "alt-three/validator",
3+
"description": "A Validation Wrapper For Laravel 5",
4+
"keywords": ["validator", "logging", "Validator", "Alt Three"],
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "James Brooks",
9+
"email": "[email protected]"
10+
},
11+
{
12+
"name": "Graham Campbell",
13+
"email": "[email protected]"
14+
},
15+
{
16+
"name": "Joseph Cohen",
17+
"email": "[email protected]"
18+
}
19+
],
20+
"require": {
21+
"php": ">=5.5.9",
22+
"illuminate/contracts": "5.1.*",
23+
"illuminate/support": "5.1.*",
24+
"psr/log": "~1.0"
25+
},
26+
"require-dev": {
27+
"phpunit/phpunit": "^4.7.6"
28+
},
29+
"autoload": {
30+
"psr-4": {
31+
"AltThree\\Validator\\": "src/"
32+
}
33+
},
34+
"autoload-dev": {
35+
"psr-4": {
36+
"AltThree\\Tests\\Validator\\": "tests/"
37+
}
38+
},
39+
"config": {
40+
"preferred-install": "dist"
41+
},
42+
"extra": {
43+
"branch-alias": {
44+
"dev-master": "1.0-dev"
45+
}
46+
},
47+
"minimum-stability": "dev",
48+
"prefer-stable": true
49+
}

phpunit.xml.dist

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
beStrictAboutTestsThatDoNotTestAnything="true"
5+
beStrictAboutOutputDuringTests="true"
6+
bootstrap="vendor/autoload.php"
7+
colors="true"
8+
convertErrorsToExceptions="true"
9+
convertNoticesToExceptions="true"
10+
convertWarningsToExceptions="true"
11+
processIsolation="false"
12+
stopOnError="false"
13+
stopOnFailure="false"
14+
verbose="true"
15+
>
16+
<testsuites>
17+
<testsuite name="Alt Three Validator Test Suite">
18+
<directory suffix="Test.php">./tests</directory>
19+
</testsuite>
20+
</testsuites>
21+
<filter>
22+
<whitelist processUncoveredFilesFromWhitelist="true">
23+
<directory suffix=".php">./src</directory>
24+
</whitelist>
25+
</filter>
26+
</phpunit>

src/ValidatingMiddleware.php

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Alt Three Validator.
5+
*
6+
* (c) Alt Three LTD <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace AltThree\Validator;
13+
14+
use Closure;
15+
use Illuminate\Contracts\Validation\Factory;
16+
use ReflectionClass;
17+
use ReflectionProperty;
18+
19+
/**
20+
* This is the command validating middleware class.
21+
*
22+
* @author Graham Campbell <[email protected]>
23+
*/
24+
class ValidatingMiddleware
25+
{
26+
/**
27+
* The validation factory instance.
28+
*
29+
* @var \Illuminate\Contracts\Validation\Factory
30+
*/
31+
protected $factory;
32+
33+
/**
34+
* Create a new run analysis job handler instance.
35+
*
36+
* @param \Illuminate\Contracts\Validation\Factory $factory
37+
*
38+
* @return void
39+
*/
40+
public function __construct(Factory $factory)
41+
{
42+
$this->factory = $factory;
43+
}
44+
45+
/**
46+
* Validate the command before execution.
47+
*
48+
* @param object $command
49+
* @param \Closure $next
50+
*
51+
* @throws \Watson\Validating\ValidationException
52+
*
53+
* @return void
54+
*/
55+
public function handle($command, Closure $next)
56+
{
57+
if (property_exists($command, 'rules') && is_array($command->rules)) {
58+
$this->validate($command);
59+
}
60+
61+
return $next($command);
62+
}
63+
64+
/**
65+
* Validate the command.
66+
*
67+
* @param object $command
68+
*
69+
* @throws \Watson\Validating\ValidationException
70+
*
71+
* @return void
72+
*/
73+
protected function validate($command)
74+
{
75+
$messages = property_exists($command, 'validationMessages') ? $command->validationMessages : [];
76+
77+
$validator = $this->factory->make($this->getData($command), $command->rules, $messages);
78+
79+
if ($validator->fails()) {
80+
throw new ValidationException($validator->getMessageBag());
81+
}
82+
}
83+
84+
/**
85+
* Get the data to be validated.
86+
*
87+
* @param object $command
88+
*
89+
* @return array
90+
*/
91+
protected function getData($command)
92+
{
93+
$data = [];
94+
95+
foreach ((new ReflectionClass($command))->getProperties(ReflectionProperty::IS_PUBLIC) as $property) {
96+
$name = $property->getName();
97+
$value = $property->getValue($command);
98+
99+
if (in_array($name, ['rules', 'validationMessages'], true) || is_object($value)) {
100+
continue;
101+
}
102+
103+
$data[$name] = $value;
104+
}
105+
106+
return $data;
107+
}
108+
}

src/ValidatingObserver.php

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Alt Three Validator.
5+
*
6+
* (c) Alt Three LTD <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace AltThree\Validator;
13+
14+
use Illuminate\Database\Eloquent\Model;
15+
16+
/**
17+
* This is the model validating observer class.
18+
*
19+
* @author Graham Campbell <[email protected]>
20+
*/
21+
class ValidatingObserver
22+
{
23+
/**
24+
* Validate the model on saving.
25+
*
26+
* @param \Illuminate\Database\Eloquent\Model $model
27+
*
28+
* @throws \AltThree\Validator\ValidationException
29+
*
30+
* @return void
31+
*/
32+
public function saving(Model $model)
33+
{
34+
$this->performValidation($model);
35+
}
36+
37+
/**
38+
* Validate the model on restoring.
39+
*
40+
* @param \Illuminate\Database\Eloquent\Model $model
41+
*
42+
* @throws \AltThree\Validator\ValidationException
43+
*
44+
* @return void
45+
*/
46+
public function restoring(Model $model)
47+
{
48+
$this->performValidation($model);
49+
}
50+
51+
/**
52+
* Validate the given model.
53+
*
54+
* @param \Illuminate\Database\Eloquent\Model $model
55+
*
56+
* @throws \AltThree\Validator\ValidationException
57+
*
58+
* @return void
59+
*/
60+
protected function validate(Model $model)
61+
{
62+
if (!$model->isValid()) {
63+
throw new ValidationException($model->getMessageBag());
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)