-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] Initial release #1
base: master
Are you sure you want to change the base?
Changes from 5 commits
e81602f
05beae0
a240cfc
9905a2f
d508fa0
9d28bf2
281e250
e68afab
16877ba
84d435e
8eb3dd5
66fec1b
4d4b9df
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
vendor/ | ||
composer.lock | ||
composer.phar |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
language: php | ||
|
||
php: | ||
- 5.5 | ||
- 5.6 | ||
- hhvm | ||
- hhvm-nightly | ||
|
||
before_script: | ||
- composer self-update | ||
- composer update --prefer-source --dev | ||
|
||
script: | ||
- ./vendor/bin/phpunit --coverage-clover ./build/logs/clover.xml --group=Coverage | ||
- ./vendor/bin/phpcs --standard=PSR2 ./src/ ./tests | ||
|
||
after_script: | ||
- php vendor/bin/coveralls -v | ||
|
||
notifications: | ||
irc: "irc.freenode.org#zftalk.modules" | ||
email: false |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,12 @@ | ||
input-filter | ||
============ | ||
# Input Filter | ||
|
||
ZF3 prototype for Input filter component | ||
This component is a prototype for ZF3 Input Filter. Its main advantages over original implementation are: | ||
|
||
* Much more efficient (benchmarks showed a x10 performance and a lot less consumed memory). | ||
* Completely stateless | ||
* Uses a concept of "InputFilterResult" | ||
* Renames to make it easier to understand (previous "InputFilter" has been renamed to "InputCollection") | ||
* Behaviour is now much more predictable | ||
* ... and a lot of things! | ||
|
||
## How to use it? |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
{ | ||
"name": "zfr/input-filter", | ||
"description": "Zend Framework 3 prototype for a better input filter component", | ||
"type": "library", | ||
"license": "MIT", | ||
"keywords": [ | ||
"zf3", | ||
"zend framework", | ||
"input filter" | ||
], | ||
"homepage": "https://github.com/zf-fr/input-filter", | ||
"authors": [ | ||
{ | ||
"name": "Michaël Gallego", | ||
"email": "[email protected]", | ||
"homepage": "http://www.michaelgallego.fr/" | ||
} | ||
], | ||
"require": { | ||
"php": ">=5.5", | ||
"zendframework/zend-servicemanager": "~2.2", | ||
"zendframework/zend-validator": "~2.2", | ||
"zendframework/zend-filter": "~2.2", | ||
"zendframework/zend-stdlib": "~2.2" | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "~4.1", | ||
"squizlabs/php_codesniffer": "1.4.*", | ||
"satooshi/php-coveralls": "~0.6", | ||
"athletic/athletic": "0.1.7" | ||
}, | ||
"autoload": { | ||
"psr-0": { | ||
"InputFilter\\": "src/" | ||
} | ||
}, | ||
"autoload-dev": { | ||
"psr-0": { | ||
"InputFilterBenchmark\\": "benchmarks/" | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?xml version="1.0"?> | ||
<phpunit | ||
bootstrap="./tests/Bootstrap.php" | ||
colors="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
verbose="true" | ||
stopOnFailure="false" | ||
processIsolation="false" | ||
backupGlobals="false" | ||
syntaxCheck="true" | ||
> | ||
<testsuite name="InputFilter tests"> | ||
<directory>./tests</directory> | ||
</testsuite> | ||
<filter> | ||
<whitelist addUncoveredFilesFromWhitelist="true"> | ||
<directory suffix=".php">./src</directory> | ||
</whitelist> | ||
</filter> | ||
</phpunit> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
*/ | ||
|
||
namespace InputFilter\Exception; | ||
|
||
interface ExceptionInterface | ||
{ | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
*/ | ||
|
||
namespace InputFilter\Exception; | ||
|
||
class InvalidArgumentException extends \InvalidArgumentException implements ExceptionInterface | ||
{ | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
*/ | ||
|
||
namespace InputFilter\Exception; | ||
|
||
class RuntimeException extends \RuntimeException implements ExceptionInterface | ||
{ | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
*/ | ||
|
||
namespace InputFilter; | ||
|
||
use InputFilter\Result\InputFilterResult; | ||
use Zend\Validator\NotEmpty; | ||
use Zend\Validator\ValidatorChain; | ||
use Zend\Filter\FilterChain; | ||
|
||
/** | ||
* Input | ||
*/ | ||
class Input implements InputInterface | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
protected $name; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
protected $required = false; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
protected $allowEmpty = false; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
protected $breakOnFailure = false; | ||
|
||
/** | ||
* @var FilterChain|null | ||
*/ | ||
protected $filterChain; | ||
|
||
/** | ||
* @var ValidatorChain|null | ||
*/ | ||
protected $validatorChain; | ||
|
||
/** | ||
* @param FilterChain $filterChain | ||
* @param ValidatorChain $validatorChain | ||
*/ | ||
public function __construct(FilterChain $filterChain = null, ValidatorChain $validatorChain = null) | ||
{ | ||
$this->filterChain = $filterChain; | ||
$this->validatorChain = $validatorChain; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function setName($name) | ||
{ | ||
$this->name = (string) $name; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getName() | ||
{ | ||
return $this->name; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function setRequired($required) | ||
{ | ||
if ($required) { | ||
$this->required = true; | ||
$this->validatorChain->attachByName(NotEmpty::class, [], self::REQUIRED_VALIDATOR_PRIORITY); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function isRequired() | ||
{ | ||
return $this->required; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function setAllowEmpty($allowEmpty) | ||
{ | ||
$this->allowEmpty = (bool) $allowEmpty; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function allowEmpty() | ||
{ | ||
return $this->allowEmpty; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function setBreakOnFailure($breakOnFailure) | ||
{ | ||
$this->breakOnFailure = (bool) $breakOnFailure; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function breakOnFailure() | ||
{ | ||
return $this->breakOnFailure; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function setFilterChain(FilterChain $filterChain) | ||
{ | ||
$this->filterChain = $filterChain; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getFilterChain() | ||
{ | ||
return $this->filterChain ?: new FilterChain(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if (null === $this->filterChain) {
$this->filterChain = new FilterChain();
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why? I like oneLiner. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ha yeah, I get your point, sorry. |
||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function setValidatorChain(ValidatorChain $validatorChain) | ||
{ | ||
$this->validatorChain = $validatorChain; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getValidatorChain() | ||
{ | ||
return $this->validatorChain ?: new ValidatorChain(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above |
||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function runAgainst($value, $context = null) | ||
{ | ||
$filteredValue = $this->filterChain->filter($value, $context); | ||
|
||
if ( | ||
$this->validatorChain->isValid($filteredValue, $context) | ||
|| (null === $filteredValue && $this->allowEmpty) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should probably check it first than validator chain. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. True. |
||
) { | ||
return new InputFilterResult($value, $filteredValue); | ||
} | ||
|
||
// If it is not valid, we don't want to store the filtered value, as it's | ||
// incorrect anyway... | ||
return new InputFilterResult($value, null, $this->validatorChain->getMessages()); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unaware of instantiation logic :D