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 16 changed files with 1,529 additions and 264 deletions.
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
"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.1-dev",
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;
}
}
155 changes: 128 additions & 27 deletions src/BaseInputFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,30 @@
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?
*/
class BaseInputFilter implements InputFilterInterface, UnknownInputsCapableInterface
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 @@ -152,42 +163,119 @@ 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)
// Single and Multi File Uploads
|| (is_array($this->data[$name])
&& isset($this->data[$name]['error']) && $this->data[$name]['error'] === UPLOAD_ERR_NO_FILE)
|| (is_array($this->data[$name]) && 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 = $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()
// "Not required" should not apply to empty strings (#3983)
&& !(array_key_exists($name, $this->data) && is_string($this->data[$name]))
) {
$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 (!$dataExists
&& $input instanceof InputInterface
&& $input->isRequired()
&& $input->allowEmpty()
) {
if (!$input->allowEmpty()) {
$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 ($dataExists
&& null === $this->data[$name]
&& $input instanceof InputInterface
&& $input->isRequired()
&& $input->allowEmpty()
) {
$this->validInputs[$name] = $input;
continue;
}

// 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 instanceof EmptyContextInterface
&& $input->isRequired()
&& $input->allowEmpty()
) {
if (!$input->continueIfEmpty()) {
$this->validInputs[$name] = $input;
continue;
}
// make sure we have a value (empty) for validation
}

// 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 @@ -197,6 +285,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 @@ -390,6 +480,7 @@ public function getMessages()
foreach ($this->getInvalidInput() as $name => $input) {
$messages[$name] = $input->getMessages();
}

return $messages;
}

Expand Down Expand Up @@ -498,4 +589,14 @@ public function getUnknown()

return $unknownInputs;
}

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

0 comments on commit ca485f4

Please sign in to comment.