1<?php
2/**
3 * Zend Framework (http://framework.zend.com/)
4 *
5 * @link      http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license   http://framework.zend.com/license/new-bsd New BSD License
8 */
9
10namespace Zend\InputFilter;
11
12class ArrayInput extends Input
13{
14    /**
15     * @var array
16     */
17    protected $value = array();
18
19    /**
20     * @param  array $value
21     * @throws Exception\InvalidArgumentException
22     * @return Input
23     */
24    public function setValue($value)
25    {
26        if (!is_array($value)) {
27            throw new Exception\InvalidArgumentException(
28                sprintf('Value must be an array, %s given.', gettype($value))
29            );
30        }
31        return parent::setValue($value);
32    }
33
34    /**
35     * {@inheritdoc}
36     */
37    public function resetValue()
38    {
39        $this->value = array();
40        $this->hasValue = false;
41        return $this;
42    }
43
44    /**
45     * @return array
46     */
47    public function getValue()
48    {
49        $filter = $this->getFilterChain();
50        $result = array();
51        foreach ($this->value as $key => $value) {
52            $result[$key] = $filter->filter($value);
53        }
54        return $result;
55    }
56
57    /**
58     * @param  mixed $context Extra "context" to provide the validator
59     * @return bool
60     */
61    public function isValid($context = null)
62    {
63        $hasValue = $this->hasValue();
64        $required = $this->isRequired();
65        $hasFallback = $this->hasFallback();
66
67        if (! $hasValue && $hasFallback) {
68            $this->setValue($this->getFallbackValue());
69            return true;
70        }
71
72        if (! $hasValue && $required) {
73            if ($this->errorMessage === null) {
74                $this->errorMessage = $this->prepareRequiredValidationFailureMessage();
75            }
76            return false;
77        }
78
79        if (!$this->continueIfEmpty() && !$this->allowEmpty()) {
80            $this->injectNotEmptyValidator();
81        }
82        $validator = $this->getValidatorChain();
83        $values    = $this->getValue();
84        $result    = true;
85        foreach ($values as $value) {
86            $empty = ($value === null || $value === '' || $value === array());
87            if ($empty && !$this->isRequired() && !$this->continueIfEmpty()) {
88                $result = true;
89                continue;
90            }
91            if ($empty && $this->allowEmpty() && !$this->continueIfEmpty()) {
92                $result = true;
93                continue;
94            }
95            $result = $validator->isValid($value, $context);
96            if (!$result) {
97                if ($hasFallback) {
98                    $this->setValue($this->getFallbackValue());
99                    return true;
100                }
101                break;
102            }
103        }
104
105        return $result;
106    }
107}
108