Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Show file tree
Hide file tree
Showing 30 changed files with 2,536 additions and 264 deletions.
9 changes: 9 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@
"zendframework/zend-validator": "self.version",
"zendframework/zend-stdlib": "self.version"
},
"suggest": {
"zendframework/zend-servicemanager": "To support plugin manager support"
},
"extra": {
"branch-alias": {
"dev-master": "2.2-dev",
"dev-develop": "2.3-dev"
}
},
"homepage": "https://github.com/zendframework/zend-input-filter",
"autoload-dev": {
"psr-4": {
Expand Down
69 changes: 69 additions & 0 deletions src/ArrayInput.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?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 Zend\InputFilter;

class ArrayInput extends Input
{
/**
* @var array
*/
protected $value = array();

/**
* @param array $value
* @return Input
*/
public function setValue($value)
{
if (!is_array($value)) {
throw new Exception\InvalidArgumentException(
sprintf('Value must be an array, %s given.', gettype($value))
);
}
return parent::setValue($value);
}

/**
* @return array
*/
public function getValue()
{
$filter = $this->getFilterChain();
$result = array();
foreach ($this->value as $key => $value) {
$result[$key] = $filter->filter($value);
}
return $result;
}

/**
* @param mixed $context Extra "context" to provide the validator
* @return bool
*/
public function isValid($context = null)
{
$this->injectNotEmptyValidator();
$validator = $this->getValidatorChain();
$values = $this->getValue();
$result = true;
foreach ($values as $value) {
$result = $validator->isValid($value, $context);
if (!$result) {
if ($fallbackValue = $this->getFallbackValue()) {
$this->setValue($fallbackValue);
$result = true;
}
break;
}
}

return $result;
}
}
213 changes: 190 additions & 23 deletions src/BaseInputFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,39 @@
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_InputFilter
*/

namespace Zend\InputFilter;

use ArrayAccess;
use Traversable;
use Zend\Stdlib\ArrayUtils;
use Zend\Stdlib\InitializableInterface;

/**
* @todo How should we deal with required input when data is missing?
* should a message be returned? if so, what message?
* @category Zend
* @package Zend_InputFilter
*/
class BaseInputFilter implements InputFilterInterface
class BaseInputFilter implements InputFilterInterface, UnknownInputsCapableInterface, InitializableInterface
{
protected $data;
protected $inputs = array();
protected $invalidInputs;
protected $validationGroup;
protected $validInputs;

/**
* This function is automatically called when creating element with factory. It
* allows to perform various operations (add elements...)
*
* @return void
*/
public function init()
{
}

/**
* Countable: number of inputs in this input filter
*
Expand Down Expand Up @@ -155,33 +163,124 @@ public function isValid()
));
}

$inputs = $this->validationGroup ?: array_keys($this->inputs);
return $this->validateInputs($inputs);
}

/**
* Validate a set of inputs against the current data
*
* @param array $inputs
* @return bool
*/
protected function validateInputs(array $inputs)
{
$this->validInputs = array();
$this->invalidInputs = array();
$valid = true;

$inputs = $this->validationGroup ?: array_keys($this->inputs);
foreach ($inputs as $name) {
$input = $this->inputs[$name];
if (!array_key_exists($name, $this->data)
|| (null === $this->data[$name])
|| (is_string($this->data[$name]) && strlen($this->data[$name]) === 0)
$input = $this->inputs[$name];
$dataExists = array_key_exists($name, $this->data);

// key doesn't exist, but input is not required; valid
if (!$dataExists
&& $input instanceof InputInterface
&& !$input->isRequired()
) {
if ($input instanceof InputInterface) {
// - test if input is required
if (!$input->isRequired()) {
$this->validInputs[$name] = $input;
continue;
}
// - test if input allows empty
if ($input->allowEmpty()) {
$this->validInputs[$name] = $input;
continue;
}
$this->validInputs[$name] = $input;
continue;
}

// key doesn't exist, input is required, allows empty; valid if
// continueIfEmpty is false or input doesn't implement
// that interface; otherwise validation chain continues
if (!$dataExists
&& $input instanceof InputInterface
&& $input->isRequired()
&& $input->allowEmpty()
) {
if(!($input instanceOf EmptyContextInterface && $input->continueIfEmpty())) {
$this->validInputs[$name] = $input;
continue;
}
}

// key exists, is null, input is not required; valid
if ($dataExists
&& null === $this->data[$name]
&& $input instanceof InputInterface
&& !$input->isRequired()
) {
$this->validInputs[$name] = $input;
continue;
}

// key exists, is null, input is required, allows empty; valid if
// continueIfEmpty is false or input doesn't implement
// that interface; otherwise validation chain continues
if ($dataExists
&& null === $this->data[$name]
&& $input instanceof InputInterface
&& $input->isRequired()
&& $input->allowEmpty()
) {
if (!($input instanceof EmptyContextInterface && $input->continueIfEmpty())) {
$this->validInputs[$name] = $input;
continue;
}
// make sure we have a value (empty) for validation
$this->data[$name] = '';
}

// key exists, empty string, input is not required, allows empty; valid
if ($dataExists
&& '' === $this->data[$name]
&& $input instanceof InputInterface
&& !$input->isRequired()
) {
$this->validInputs[$name] = $input;
continue;
}

// key exists, empty string, input is required, allows empty; valid
// if continueIfEmpty is false, otherwise validation continues
if ($dataExists
&& '' === $this->data[$name]
&& $input instanceof InputInterface
&& $input->isRequired()
&& $input->allowEmpty()
) {
if (!($input instanceof EmptyContextInterface && $input->continueIfEmpty())) {
$this->validInputs[$name] = $input;
continue;
}
}

// key exists, is array representing file, no file present, input not
// required or allows empty; valid
if ($dataExists
&& is_array($this->data[$name])
&& (
(isset($this->data[$name]['error'])
&& $this->data[$name]['error'] === UPLOAD_ERR_NO_FILE)
|| (count($this->data[$name]) === 1
&& isset($this->data[$name][0])
&& is_array($this->data[$name][0])
&& isset($this->data[$name][0]['error'])
&& $this->data[$name][0]['error'] === UPLOAD_ERR_NO_FILE)
)
&& $input instanceof InputInterface
&& (!$input->isRequired() || $input->allowEmpty())
) {
$this->validInputs[$name] = $input;
continue;
}

// make sure we have a value (empty) for validation
if (!$dataExists) {
$this->data[$name] = null;
}

// Validate an input filter
if ($input instanceof InputFilterInterface) {
if (!$input->isValid()) {
$this->invalidInputs[$name] = $input;
Expand All @@ -191,6 +290,8 @@ public function isValid()
$this->validInputs[$name] = $input;
continue;
}

// Validate an input
if ($input instanceof InputInterface) {
if (!$input->isValid($this->data)) {
// Validation failure
Expand Down Expand Up @@ -384,6 +485,7 @@ public function getMessages()
foreach ($this->getInvalidInput() as $name => $input) {
$messages[$name] = $input->getMessages();
}

return $messages;
}

Expand Down Expand Up @@ -437,4 +539,69 @@ protected function populate()
$input->setValue($value);
}
}

/**
* Is the data set has unknown input ?
*
* @throws Exception\RuntimeException
* @return bool
*/
public function hasUnknown()
{
if (null === $this->data) {
throw new Exception\RuntimeException(sprintf(
'%s: no data present!',
__METHOD__
));
}

$data = array_keys($this->data);
$inputs = array_keys($this->inputs);
$diff = array_diff($data, $inputs);
if (!empty($diff)) {
return count(array_intersect($diff, $inputs)) == 0;
}

return false;
}

/**
* Return the unknown input
*
* @throws Exception\RuntimeException
* @return array
*/
public function getUnknown()
{
if (null === $this->data) {
throw new Exception\RuntimeException(sprintf(
'%s: no data present!',
__METHOD__
));
}

$data = array_keys($this->data);
$inputs = array_keys($this->inputs);
$diff = array_diff($data, $inputs);

$unknownInputs = array();
$intersect = array_intersect($diff, $data);
if (!empty($intersect)) {
foreach ($intersect as $key) {
$unknownInputs[$key] = $this->data[$key];
}
}

return $unknownInputs;
}

/**
* Get an array of all inputs
*
* @return array
*/
public function getInputs()
{
return $this->inputs;
}
}
Loading

0 comments on commit 022a3e8

Please sign in to comment.