<?php
namespace Moto\InputFilter; use Moto; use Zend; use Zend\InputFilter\InputFilter; use Zend\Stdlib\ArrayUtils; class AbstractInputFilter extends InputFilter { protected $_defaultsValue = null; protected $_name; protected $_fileFields = array(); protected $_options = array(); public function __construct($options = []) { $this->setOptions($options); $this->init(); if ($this->_name !== null) { Moto\Hook::trigger(Moto\Hook::INPUT_FILTER_CREATED, $this); } } public function setOptions($options) { if (!is_array($options)) { return false; } $this->_options = $options; return true; } public function getOption($name, $default = null) { return Moto\Util::getValue($this->_options, $name, $default); } public function getName() { return $this->_name; } public function getMessagesKeys($elementName = null) { $messages = parent::getMessages($elementName); $findMessagesKeys = function (array $tree) use (&$findMessagesKeys) { $result = array(); foreach ($tree as $key => $value) { if (is_string($key) && $key[0] === '@') { $key = substr($key, 1); $result[] = array( 'name' => $key, 'errors' => $value, ); continue; } if (is_array($value)) { $result[$key] = $findMessagesKeys($tree[$key]); } elseif (is_string($value)) { $result[] = $key; } } return $result; }; $messages = $findMessagesKeys($messages); return $messages; } public function setData($data) { if (is_array($this->_defaultsValue)) { $data = ArrayUtils::merge($this->_defaultsValue, $data); } return parent::setData($data); } public function getValues($keys = null, $ignoreDefault = false) { $values = parent::getValues(); if (is_bool($keys)) { $ignoreDefault = $keys; } if ($ignoreDefault) { if (is_array($this->data)) { $diffKeys = array_diff(array_keys($values), array_keys($this->data)); if (is_array($diffKeys)) { foreach ($diffKeys as $key) { unset($values[$key]); } } } } if (is_array($keys)) { $_values = array(); foreach ($keys as $key) { if (array_key_exists($key, $values)) { $_values[$key] = $values[$key]; } } $values = $_values; } return $values; } public function add($input, $name = null) { if ($input instanceof Zend\InputFilter\FileInput) { if (empty($name) || is_int($name)) { $name = $input->getName(); } $this->_fileFields[$name] = true; } return parent::add($input, $name); } public function remove($name) { if (array_key_exists($name, $this->_fileFields)) { unset($this->_fileFields[$name]); } return parent::remove($name); } public function getFileFields() { return array_keys($this->_fileFields); } }