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

Commit

Permalink
Merge remote-tracking branch 'upstream/develop' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Show file tree
Hide file tree
Showing 10 changed files with 812 additions and 25 deletions.
62 changes: 61 additions & 1 deletion src/BaseInputFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* @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 +165,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 +442,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;
}
}
4 changes: 2 additions & 2 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,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 +306,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
157 changes: 157 additions & 0 deletions src/FileInput.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
<?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
* @package Zend_InputFilter
*/

namespace Zend\InputFilter;

use Zend\Validator\File\Upload as UploadValidator;

/**
* @category Zend
* @package Zend_InputFilter
*/
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()
{
$filter = $this->getFilterChain();
$value = (is_array($this->value) && isset($this->value['tmp_name']))
? $this->value['tmp_name'] : $this->value;
if (is_scalar($value) && $this->isValid) {
// Single file input
$value = $filter->filter($value);
} elseif (is_array($value)) {
// Multi file input (multiple attribute set)
$newValue = array();
foreach ($value as $multiFileData) {
$fileName = (is_array($multiFileData) && isset($multiFileData['tmp_name']))
? $multiFileData['tmp_name'] : $multiFileData;
$newValue[] = ($this->isValid) ? $filter->filter($fileName) : $fileName;
}
$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

$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('fileupload', 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;
}
}
3 changes: 3 additions & 0 deletions src/Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,9 @@ public function getMessages()
return $validator->getMessages();
}

/**
* @return void
*/
protected function injectNotEmptyValidator()
{
if ((!$this->isRequired() && $this->allowEmpty()) || $this->notEmptyValidator) {
Expand Down
48 changes: 48 additions & 0 deletions src/InputFilterAwareTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?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
* @package Zend_InputFilter
*/

namespace Zend\InputFilter;

use Zend\InputFilter\InputFilterInterface;

/**
* @category Zend
* @package Zend_InputFilter
*/
trait InputFilterAwareTrait
{
/**
* @var InputFilterInterface
*/
protected $inputFilter = null;

/**
* Set input filter
*
* @param InputFilterInterface $inputFilter
* @return mixed
*/
public function setInputFilter(InputFilterInterface $inputFilter)
{
$this->inputFilter = $inputFilter;

return $this;
}

/**
* Retrieve input filter
*
* @return InputFilterInterface
*/
public function getInputFilter()
{
return $this->inputFilter;
}
}
24 changes: 24 additions & 0 deletions src/UnknownInputsCapableInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?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
* @package Zend_InputFilter
*/

namespace Zend\InputFilter;

/**
* Implementors of this interface may report on the existence of unknown input,
* as well as retrieve all unknown values.
*
* @category Zend
* @package Zend_InputFilter
*/
interface UnknownInputsCapableInterface
{
public function hasUnknown();
public function getUnknown();
}
Loading

0 comments on commit cc9ba24

Please sign in to comment.