Skip to content
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

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
vendor/
composer.lock
composer.phar
22 changes: 22 additions & 0 deletions .travis.yml
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
14 changes: 11 additions & 3 deletions README.md
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!

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


## How to use it?
42 changes: 42 additions & 0 deletions composer.json
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/"
}
}
}
22 changes: 22 additions & 0 deletions phpunit.xml.dist
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>
14 changes: 14 additions & 0 deletions src/InputFilter/Exception/ExceptionInterface.php
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
{
}
14 changes: 14 additions & 0 deletions src/InputFilter/Exception/InvalidArgumentException.php
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
{
}
14 changes: 14 additions & 0 deletions src/InputFilter/Exception/RuntimeException.php
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
{
}
179 changes: 179 additions & 0 deletions src/InputFilter/Input.php
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();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (null === $this->filterChain) {
    $this->filterChain = new FilterChain();
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why? I like oneLiner.

Copy link
Member Author

Choose a reason for hiding this comment

The 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();

Choose a reason for hiding this comment

The 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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably check it first than validator chain.

Copy link
Member Author

Choose a reason for hiding this comment

The 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());
}
}
Loading