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

Commit

Permalink
Merge branch 'master' of https://github.com/zendframework/zf2 into zf…
Browse files Browse the repository at this point in the history
…-3371
  • Loading branch information
Show file tree
Hide file tree
Showing 19 changed files with 862 additions and 86 deletions.
65 changes: 61 additions & 4 deletions src/BaseInputFilter.php
Expand Up @@ -5,7 +5,6 @@
* @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
* @package Zend_InputFilter
*/

namespace Zend\InputFilter;
Expand All @@ -17,10 +16,8 @@
/**
* @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
{
protected $data;
protected $inputs = array();
Expand Down Expand Up @@ -165,6 +162,11 @@ public function isValid()
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]['error']) && $this->data[$name][0]['error'] === UPLOAD_ERR_NO_FILE)
) {
if ($input instanceof InputInterface) {
// - test if input is required
Expand Down Expand Up @@ -437,4 +439,59 @@ 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;
}
}
6 changes: 0 additions & 6 deletions src/Exception/ExceptionInterface.php
Expand Up @@ -5,15 +5,9 @@
* @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
* @package Zend_InputFilter
*/

namespace Zend\InputFilter\Exception;

/**
* @category Zend
* @package Zend_InputFilter
* @subpackage Exception
*/
interface ExceptionInterface
{}
6 changes: 0 additions & 6 deletions src/Exception/InvalidArgumentException.php
Expand Up @@ -5,16 +5,10 @@
* @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
* @package Zend_InputFilter
*/

namespace Zend\InputFilter\Exception;

/**
* @category Zend
* @package Zend_InputFilter
* @subpackage Exception
*/
class InvalidArgumentException extends \InvalidArgumentException implements
ExceptionInterface
{}
6 changes: 0 additions & 6 deletions src/Exception/RuntimeException.php
Expand Up @@ -5,15 +5,9 @@
* @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
* @package Zend_InputFilter
*/

namespace Zend\InputFilter\Exception;

/**
* @category Zend
* @package Zend_InputFilter
* @subpackage Exception
*/
class RuntimeException extends \RuntimeException implements ExceptionInterface
{}
9 changes: 2 additions & 7 deletions src/Factory.php
Expand Up @@ -5,7 +5,6 @@
* @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
* @package Zend_InputFilter
*/

namespace Zend\InputFilter;
Expand All @@ -16,10 +15,6 @@
use Zend\Validator\ValidatorChain;
use Zend\Validator\ValidatorInterface;

/**
* @category Zend
* @package Zend_InputFilter
*/
class Factory
{
protected $defaultFilterChain;
Expand Down Expand Up @@ -287,7 +282,7 @@ protected function populateValidators(ValidatorChain $chain, $validators)
{
foreach ($validators as $validator) {
if ($validator instanceof ValidatorInterface) {
$chain->addValidator($validator);
$chain->attach($validator);
continue;
}

Expand All @@ -306,7 +301,7 @@ protected function populateValidators(ValidatorChain $chain, $validators)
if (isset($validator['break_chain_on_failure'])) {
$breakChainOnFailure = $validator['break_chain_on_failure'];
}
$chain->addByName($name, $options, $breakChainOnFailure);
$chain->attachByName($name, $options, $breakChainOnFailure);
continue;
}

Expand Down
169 changes: 169 additions & 0 deletions src/FileInput.php
@@ -0,0 +1,169 @@
<?php
/**
* 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)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\InputFilter;

use Zend\Validator\File\UploadFile as UploadValidator;

/**
* FileInput is a special Input type for handling uploaded files.
*
* It differs from Input in a few ways:
*
* 1. It expects the raw value to be in the $_FILES array format.
*
* 2. The validators are run **before** the filters (the opposite behavior of Input).
* This is so is_uploaded_file() validation can be run prior to any filters that
* may rename/move/modify the file.
*
* 3. Instead of adding a NotEmpty validator, it will (by default) automatically add
* a Zend\Validator\File\Upload validator.
*/
class FileInput extends Input
{
/**
* @var boolean
*/
protected $isValid = false;

/**
* @var boolean
*/
protected $autoPrependUploadValidator = true;

/**
* @param boolean $value Enable/Disable automatically prepending an Upload validator
* @return FileInput
*/
public function setAutoPrependUploadValidator($value)
{
$this->autoPrependUploadValidator = $value;
return $this;
}

/**
* @return boolean
*/
public function getAutoPrependUploadValidator()
{
return $this->autoPrependUploadValidator;
}

/**
* @return mixed
*/
public function getValue()
{
$value = $this->value;
if ($this->isValid && is_array($value)) {
// Run filters ~after~ validation, so that is_uploaded_file()
// validation is not affected by filters.
$filter = $this->getFilterChain();
if (isset($value['tmp_name'])) {
// Single file input
$value = $filter->filter($value);
} else {
// Multi file input (multiple attribute set)
$newValue = array();
foreach ($value as $fileData) {
if (is_array($fileData) && isset($fileData['tmp_name'])) {
$newValue[] = $filter->filter($fileData);
}
}
$value = $newValue;
}
}

return $value;
}

/**
* @param mixed $context Extra "context" to provide the validator
* @return boolean
*/
public function isValid($context = null)
{
$this->injectUploadValidator();
$validator = $this->getValidatorChain();
//$value = $this->getValue(); // Do not run the filters yet for File uploads (see getValue())
$rawValue = $this->getRawValue();
if (!is_array($rawValue)) {
// This can happen in an AJAX POST, where the input comes across as a string
$rawValue = array(
'tmp_name' => $rawValue,
'name' => $rawValue,
'size' => 0,
'type' => '',
'error' => UPLOAD_ERR_NO_FILE,
);
}
if (is_array($rawValue) && isset($rawValue['tmp_name'])) {
// Single file input
$this->isValid = $validator->isValid($rawValue, $context);
} elseif (is_array($rawValue) && !empty($rawValue) && isset($rawValue[0]['tmp_name'])) {
// Multi file input (multiple attribute set)
$this->isValid = true;
foreach ($rawValue as $value) {
if (!$validator->isValid($value, $context)) {
$this->isValid = false;
break; // Do not continue processing files if validation fails
}
}
}

return $this->isValid;
}

/**
* @return void
*/
protected function injectUploadValidator()
{
if (!$this->autoPrependUploadValidator) {
return;
}
$chain = $this->getValidatorChain();

// Check if Upload validator is already first in chain
$validators = $chain->getValidators();
if (isset($validators[0]['instance'])
&& $validators[0]['instance'] instanceof UploadValidator
) {
$this->autoPrependUploadValidator = false;
return;
}

$chain->prependByName('fileuploadfile', array(), true);
$this->autoPrependUploadValidator = false;
}

/**
* No-op, NotEmpty validator does not apply for FileInputs.
* See also: BaseInputFilter::isValid()
*
* @return void
*/
protected function injectNotEmptyValidator()
{
$this->notEmptyValidator = true;
}

/**
* @param InputInterface $input
* @return FileInput
*/
public function merge(InputInterface $input)
{
parent::merge($input);
if ($input instanceof FileInput) {
$this->setAutoPrependUploadValidator($input->getAutoPrependUploadValidator());
}
return $this;
}
}
8 changes: 3 additions & 5 deletions src/Input.php
Expand Up @@ -5,7 +5,6 @@
* @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
* @package Zend_InputFilter
*/

namespace Zend\InputFilter;
Expand All @@ -14,10 +13,6 @@
use Zend\Validator\ValidatorChain;
use Zend\Validator\NotEmpty;

/**
* @category Zend
* @package Zend_InputFilter
*/
class Input implements InputInterface
{
/**
Expand Down Expand Up @@ -308,6 +303,9 @@ public function getMessages()
return $validator->getMessages();
}

/**
* @return void
*/
protected function injectNotEmptyValidator()
{
if ((!$this->isRequired() && $this->allowEmpty()) || $this->notEmptyValidator) {
Expand Down
5 changes: 0 additions & 5 deletions src/InputFilter.php
Expand Up @@ -5,17 +5,12 @@
* @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
* @package Zend_InputFilter
*/

namespace Zend\InputFilter;

use Traversable;

/**
* @category Zend
* @package Zend_InputFilter
*/
class InputFilter extends BaseInputFilter
{
/**
Expand Down

0 comments on commit 870c487

Please sign in to comment.