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'
Browse files Browse the repository at this point in the history
Merging develop to master in preparation for 2.4.0rc1.
  • Loading branch information
Show file tree
Hide file tree
Showing 14 changed files with 796 additions and 111 deletions.
9 changes: 8 additions & 1 deletion src/ArrayInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,18 @@ public function getValue()
*/
public function isValid($context = null)
{
$this->injectNotEmptyValidator();
if (!$this->continueIfEmpty() && !$this->allowEmpty()) {
$this->injectNotEmptyValidator();
}
$validator = $this->getValidatorChain();
$values = $this->getValue();
$result = true;
foreach ($values as $value) {
$empty = ($value === null || $value === '' || $value === array());
if ($empty && $this->allowEmpty() && !$this->continueIfEmpty()) {
$result = true;
continue;
}
$result = $validator->isValid($value, $context);
if (!$result) {
if ($this->hasFallback()) {
Expand Down
131 changes: 28 additions & 103 deletions src/BaseInputFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,11 @@ public function setData($data)
/**
* Is the data set valid?
*
* @param mixed|null $context
* @throws Exception\RuntimeException
* @return bool
*/
public function isValid()
public function isValid($context = null)
{
$data = $this->getRawValues();
if (null === $data) {
Expand All @@ -203,17 +204,18 @@ public function isValid()
}

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

/**
* Validate a set of inputs against the current data
*
* @param array $inputs
* @param array $data
* @param array $inputs
* @param array $data
* @param mixed|null $context
* @return bool
*/
protected function validateInputs(array $inputs, array $data = array())
protected function validateInputs(array $inputs, array $data = array(), $context = null)
{
// backwards compatibility
if (empty($data)) {
Expand All @@ -226,109 +228,15 @@ protected function validateInputs(array $inputs, array $data = array())

foreach ($inputs as $name) {
$input = $this->inputs[$name];
$dataExists = array_key_exists($name, $data);

// key doesn't exist, but input is not required; valid
if (!$dataExists
&& $input instanceof InputInterface
&& !$input->isRequired()
) {
$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 === $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 === $data[$name]
&& $input instanceof InputInterface
&& $input->isRequired()
&& $input->allowEmpty()
) {
if (!($input instanceof EmptyContextInterface && $input->continueIfEmpty())) {
$this->validInputs[$name] = $input;
continue;
}
}

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

// key exists, empty string, input is required, allows empty; valid
// if continueIfEmpty is false, otherwise validation continues
if ($dataExists
&& '' === $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($data[$name])
&& (
(isset($data[$name]['error'])
&& $data[$name]['error'] === UPLOAD_ERR_NO_FILE)
|| (count($data[$name]) === 1
&& isset($data[$name][0])
&& is_array($data[$name][0])
&& isset($data[$name][0]['error'])
&& $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) {
// make sure we have a value (empty) for validation of context
if (!array_key_exists($name, $data)) {
$data[$name] = null;
}

// Validate an input filter
if ($input instanceof InputFilterInterface) {
if (!$input->isValid()) {
if (!$input->isValid($context)) {
$this->invalidInputs[$name] = $input;
$valid = false;
continue;
Expand All @@ -339,7 +247,9 @@ protected function validateInputs(array $inputs, array $data = array())

// Validate an input
if ($input instanceof InputInterface) {
if (!$input->isValid($data)) {
$inputContext = $context ?: $data;

if (!$input->isValid($inputContext)) {
// Validation failure
$this->invalidInputs[$name] = $input;
$valid = false;
Expand Down Expand Up @@ -531,6 +441,7 @@ public function getRawValues()
$values[$name] = $input->getRawValues();
continue;
}

$values[$name] = $input->getRawValue();
}
return $values;
Expand Down Expand Up @@ -679,4 +590,18 @@ public function getInputs()
{
return $this->inputs;
}

/**
* Merges the inputs from an InputFilter into the current one
*
* @param BaseInputFilter $inputFilter
*
* @return self
*/
public function merge(BaseInputFilter $inputFilter)
{
foreach ($inputFilter->getInputs() as $name => $input) {
$this->add($input, $name);
}
}
}
12 changes: 10 additions & 2 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,17 @@ public function getInputFilterManager()
/**
* Factory for input objects
*
* @param array|Traversable $inputSpecification
* @param array|Traversable|InputProviderInterface $inputSpecification
* @throws Exception\InvalidArgumentException
* @throws Exception\RuntimeException
* @return InputInterface|InputFilterInterface
*/
public function createInput($inputSpecification)
{
if ($inputSpecification instanceof InputProviderInterface) {
$inputSpecification = $inputSpecification->getInputSpecification();
}

if (!is_array($inputSpecification) && !$inputSpecification instanceof Traversable) {
throw new Exception\InvalidArgumentException(sprintf(
'%s expects an array or Traversable; received "%s"',
Expand Down Expand Up @@ -269,13 +273,17 @@ public function createInput($inputSpecification)
/**
* Factory for input filters
*
* @param array|Traversable $inputFilterSpecification
* @param array|Traversable|InputFilterProviderInterface $inputFilterSpecification
* @throws Exception\InvalidArgumentException
* @throws Exception\RuntimeException
* @return InputFilterInterface
*/
public function createInputFilter($inputFilterSpecification)
{
if ($inputFilterSpecification instanceof InputFilterProviderInterface) {
$inputFilterSpecification = $inputFilterSpecification->getInputFilterSpecification();
}

if (!is_array($inputFilterSpecification) && !$inputFilterSpecification instanceof Traversable) {
throw new Exception\InvalidArgumentException(sprintf(
'%s expects an array or Traversable; received "%s"',
Expand Down
32 changes: 31 additions & 1 deletion src/FileInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,46 @@ public function getValue()
return $value;
}

/**
* Checks if the raw input value is an empty file input eg: no file was uploaded
*
* @param $rawValue
* @return bool
*/
public function isEmptyFile($rawValue)
{
if (!is_array($rawValue)) {
return true;
}

if (isset($rawValue['error']) && $rawValue['error'] === UPLOAD_ERR_NO_FILE) {
return true;
}

if (count($rawValue) === 1 && isset($rawValue[0])) {
return $this->isEmptyFile($rawValue[0]);
}

return false;
}

/**
* @param mixed $context Extra "context" to provide the validator
* @return bool
*/
public function isValid($context = null)
{
$rawValue = $this->getRawValue();
$empty = $this->isEmptyFile($rawValue);

if ($empty && $this->allowEmpty() && !$this->continueIfEmpty()) {
return true;
}

$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(
Expand Down
20 changes: 17 additions & 3 deletions src/Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -319,14 +319,21 @@ public function merge(InputInterface $input)
*/
public function isValid($context = null)
{
$value = $this->getValue();
$empty = ($value === null || $value === '' || $value === array());

if ($empty && $this->allowEmpty() && !$this->continueIfEmpty()) {
return true;
}

// Empty value needs further validation if continueIfEmpty is set
// so don't inject NotEmpty validator which would always
// mark that as false
if (!$this->continueIfEmpty()) {
if (!$this->continueIfEmpty() && !$this->allowEmpty()) {
$this->injectNotEmptyValidator();
}
$validator = $this->getValidatorChain();
$value = $this->getValue();

$result = $validator->isValid($value, $context);
if (!$result && $this->hasFallback()) {
$this->setValue($this->getFallbackValue());
Expand Down Expand Up @@ -372,7 +379,14 @@ protected function injectNotEmptyValidator()
}
}

$chain->prependByName('NotEmpty', array(), true);
$this->notEmptyValidator = true;

if (class_exists('Zend\ServiceManager\AbstractPluginManager')) {
$chain->prependByName('NotEmpty', array(), true);

return;
}

$chain->prependValidator(new NotEmpty(), true);
}
}
Loading

0 comments on commit b50078f

Please sign in to comment.