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

Commit

Permalink
Merge branch 'develop' of git://github.com/zendframework/zf2 into string
Browse files Browse the repository at this point in the history
  • Loading branch information
Show file tree
Hide file tree
Showing 23 changed files with 1,130 additions and 387 deletions.
3 changes: 2 additions & 1 deletion composer.json
@@ -1,13 +1,14 @@
{
"name": "zendframework/zend-inputfilter",
"description": " ",
"license": "BSD-3-Clause",
"keywords": [
"zf2",
"inputfilter"
],
"autoload": {
"psr-4": {
"Zend\\InputFilter": "src/"
"Zend\\InputFilter\\": "src/"
}
},
"require": {
Expand Down
175 changes: 125 additions & 50 deletions src/BaseInputFilter.php
@@ -1,21 +1,11 @@
<?php
/**
* Zend Framework
* Zend Framework (http://framework.zend.com/)
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_InputFilter
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @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;
Expand All @@ -25,14 +15,12 @@
use Zend\Stdlib\ArrayUtils;

/**
* @todo How should we deal with required input when data is missing?
* @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
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class BaseInputFilter implements InputFilterInterface
class BaseInputFilter implements InputFilterInterface, UnknownInputsCapableInterface
{
protected $data;
protected $inputs = array();
Expand All @@ -44,7 +32,7 @@ class BaseInputFilter implements InputFilterInterface
* Countable: number of inputs in this input filter
*
* Only details the number of direct children.
*
*
* @return int
*/
public function count()
Expand All @@ -54,9 +42,10 @@ public function count()

/**
* Add an input to the input filter
*
* @param InputInterface|InputFilterInterface $input
* @param null|string $name Name used to retrieve this input
*
* @param InputInterface|InputFilterInterface $input
* @param null|string $name Name used to retrieve this input
* @throws Exception\InvalidArgumentException
* @return InputFilterInterface
*/
public function add($input, $name = null)
Expand All @@ -71,17 +60,25 @@ public function add($input, $name = null)
));
}

if (is_null($name) || $name === '') {
if ($input instanceof InputInterface && (empty($name) || is_int($name))) {
$name = $input->getName();
}

if (isset($this->inputs[$name]) && $this->inputs[$name] instanceof InputInterface) {
// The element already exists, so merge the config. Please note that the order is important (already existing
// input is merged with the parameter given)
$input->merge($this->inputs[$name]);
}

$this->inputs[$name] = $input;
return $this;
}

/**
* Retrieve a named input
*
* @param string $name
*
* @param string $name
* @throws Exception\InvalidArgumentException
* @return InputInterface|InputFilterInterface
*/
public function get($name)
Expand All @@ -98,19 +95,32 @@ public function get($name)

/**
* Test if an input or input filter by the given name is attached
*
* @param string $name
*
* @param string $name
* @return bool
*/
public function has($name)
{
return (array_key_exists($name, $this->inputs));
}

/**
* Remove a named input
*
* @param string $name
* @return InputFilterInterface
*/
public function remove($name)
{
unset($this->inputs[$name]);
return $this;
}

/**
* Set data to use when validating and filtering
*
* @param array|Traversable $data
*
* @param array|Traversable $data
* @throws Exception\InvalidArgumentException
* @return InputFilterInterface
*/
public function setData($data)
Expand All @@ -132,7 +142,8 @@ public function setData($data)

/**
* Is the data set valid?
*
*
* @throws Exception\RuntimeException
* @return bool
*/
public function isValid()
Expand All @@ -147,13 +158,20 @@ public function isValid()
$this->validInputs = array();
$this->invalidInputs = array();
$valid = true;

$inputs = $this->validationGroup ?: array_keys($this->inputs);
//var_dump($inputs);
foreach ($inputs as $name) {
$input = $this->inputs[$name];
if (!array_key_exists($name, $this->data) || (is_string($this->data[$name]) && strlen($this->data[$name]) === 0)) {
if($input instanceof InputInterface) {
$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]['error']) && $this->data[$name][0]['error'] === UPLOAD_ERR_NO_FILE)
) {
if ($input instanceof InputInterface) {
// - test if input is required
if (!$input->isRequired()) {
$this->validInputs[$name] = $input;
Expand Down Expand Up @@ -207,8 +225,8 @@ public function isValid()
*
* Implementations should allow passing a single array value, or multiple arguments,
* each specifying a single input.
*
* @param mixed $name
*
* @param mixed $name
* @return InputFilterInterface
*/
public function setValidationGroup($name)
Expand Down Expand Up @@ -251,7 +269,7 @@ public function setValidationGroup($name)
*
* Implementations should return an associative array of name/input pairs
* that failed validation.
*
*
* @return InputInterface[]
*/
public function getInvalidInput()
Expand All @@ -264,7 +282,7 @@ public function getInvalidInput()
*
* Implementations should return an associative array of name/input pairs
* that passed validation.
*
*
* @return InputInterface[]
*/
public function getValidInput()
Expand All @@ -274,8 +292,9 @@ public function getValidInput()

/**
* Retrieve a value from a named input
*
* @param string $name
*
* @param string $name
* @throws Exception\InvalidArgumentException
* @return mixed
*/
public function getValue($name)
Expand All @@ -296,7 +315,7 @@ public function getValue($name)
*
* List should be an associative array, with the values filtered. If
* validation failed, this should raise an exception.
*
*
* @return array
*/
public function getValues()
Expand All @@ -317,8 +336,9 @@ public function getValues()

/**
* Retrieve a raw (unfiltered) value from a named input
*
* @param string $name
*
* @param string $name
* @throws Exception\InvalidArgumentException
* @return mixed
*/
public function getRawValue($name)
Expand All @@ -339,7 +359,7 @@ public function getRawValue($name)
*
* List should be an associative array of named input/value pairs,
* with the values unfiltered.
*
*
* @return array
*/
public function getRawValues()
Expand All @@ -360,7 +380,7 @@ public function getRawValues()
*
* Should return an associative array of named input/message list pairs.
* Pairs should only be returned for inputs that failed validation.
*
*
* @return array
*/
public function getMessages()
Expand All @@ -374,8 +394,8 @@ public function getMessages()

/**
* Ensure all names of a validation group exist as input in the filter
*
* @param array $inputs
*
* @param array $inputs
* @return void
* @throws Exception\InvalidArgumentException
*/
Expand All @@ -393,7 +413,7 @@ protected function validateValidationGroup(array $inputs)

/**
* Populate the values of all attached inputs
*
*
* @return void
*/
protected function populate()
Expand Down Expand Up @@ -422,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;
}
}
25 changes: 6 additions & 19 deletions src/Exception/ExceptionInterface.php
@@ -1,22 +1,11 @@
<?php
/**
* Zend Framework
* Zend Framework (http://framework.zend.com/)
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_InputFilter
* @subpackage Exception
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @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\Exception;
Expand All @@ -25,8 +14,6 @@
* @category Zend
* @package Zend_InputFilter
* @subpackage Exception
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
interface ExceptionInterface
{}
{}

0 comments on commit 8bab1da

Please sign in to comment.