/** * Retrieve a parameter. * * @param string A parameter name. * @param mixed A default parameter value. * * @return mixed A parameter value, if the parameter exists, otherwise * null. * * @author Sean Kerr <*****@*****.**> * @since 0.9.0 */ public function &getParameter($name, $default = null) { if (isset($this->parameters[$name]) || array_key_exists($name, $this->parameters)) { return $this->parameters[$name]; } try { return AgaviArrayPathDefinition::getValue($name, $this->parameters, $default); } catch (InvalidArgumentException $e) { return $default; } }
/** * Retrieve an attribute. * * @param string An attribute name. * @param string An attribute namespace. * @param mixed A default attribute value. * * @return mixed An attribute value, if the attribute exists, otherwise * null. * * @author Sean Kerr <*****@*****.**> * @author Bob Zoller <*****@*****.**> * @since 0.9.0 */ public function &getAttribute($name, $ns = null, $default = null) { if ($ns === null) { $ns = $this->defaultNamespace; } if (isset($this->attributes[$ns])) { if (isset($this->attributes[$ns][$name]) || array_key_exists($name, $this->attributes[$ns])) { return $this->attributes[$ns][$name]; } try { return AgaviArrayPathDefinition::getValue($name, $this->attributes[$ns], $default); } catch (InvalidArgumentException $e) { return $default; } } return $default; }
/** * Corrects the order of $_FILES for arrays of files. * The cleaned up array of AgaviUploadedFile objects is put into $this->files. * * @param array Array of indices used during recursion, initially empty. * * @author David Zülke <*****@*****.**> * @since 0.11.0 */ protected function fixFilesArray(&$input = array(), $index = array()) { $fromIndex = $index; if (count($fromIndex) > 0) { $first = array_shift($fromIndex); array_unshift($fromIndex, $first, 'error'); } else { // first call $input = $this->files; $this->files = array(); } $sub = AgaviArrayPathDefinition::getValue($fromIndex, $input); $theIndices = array(); foreach (array('name', 'type', 'size', 'tmp_name', 'error', 'is_uploaded_file') as $name) { $theIndex = $fromIndex; $first = array_shift($theIndex); array_shift($theIndex); array_unshift($theIndex, $first, $name); $theIndices[$name] = $theIndex; } if (is_array($sub)) { foreach ($sub as $key => $value) { $toIndex = array_merge($index, array($key)); if (is_array($value)) { $this->fixFilesArray($input, $toIndex); } else { $data = new $this->uploadedFileClass(); foreach ($theIndices as $name => $theIndex) { $data[$name] = AgaviArrayPathDefinition::getValue(array_merge($theIndex, array($key)), $input, true); } AgaviArrayPathDefinition::setValue($toIndex, $this->files, $data); } } } else { $data = new $this->uploadedFileClass(); foreach ($theIndices as $name => $theIndex) { $data[$name] = AgaviArrayPathDefinition::getValue($theIndex, $input, true); } AgaviArrayPathDefinition::setValue($index, $this->files, $data); } }
/** * Indicates whether or not a file exists. * * @param string A file name. * * @return bool true, if the file exists, otherwise false. * * @author David Zülke <*****@*****.**> * @since 0.11.0 */ public function hasFile($name) { if (isset($this->files[$name]) || array_key_exists($name, $this->files)) { $val = $this->files[$name]; } else { try { $val = AgaviArrayPathDefinition::getValue($name, $this->files); } catch (InvalidArgumentException $e) { return false; } } return is_array($val) || $val instanceof AgaviUploadedFile; }
/** * Retrieves the value for a given entry from the source. * * @param array An array with the name parts for the entry. * * @return mixed The value. * * @author Dominik del Bondio <*****@*****.**> * @since 0.11.0 */ public function getSource(array $parts) { return AgaviArrayPathDefinition::getValue($parts, $this->data); }
/** * Returns the value at the path of this instance in the given array. * * @param array The array to get the data from. * @param mixed The default value to be used if the path doesn't exist. * * @return mixed The value at the path. * * @author Dominik del Bondio <*****@*****.**> * @since 0.11.0 */ public function &getValue(array &$array, $default = null) { return AgaviArrayPathDefinition::getValue($this->parts, $array, $default); }
/** * Performs as match of the route against the input * * @param array The route info array. * @param string The input. * @param array The array where the matches will be stored to. * * @return bool Whether the regexp matched. * * @author Dominik del Bondio <*****@*****.**> * @since 0.11.0 */ protected function parseInput(array $route, $input, &$matches) { if ($route['opt']['source'] !== null) { $parts = AgaviArrayPathDefinition::getPartsFromPath($route['opt']['source']); $partArray = $parts['parts']; $count = count($partArray); if ($count > 0 && isset($this->sources[$partArray[0]])) { $input = $this->sources[$partArray[0]]; if ($count > 1) { array_shift($partArray); if (is_array($input)) { $input = AgaviArrayPathDefinition::getValue($partArray, $input); } elseif ($input instanceof AgaviIRoutingSource) { $input = $input->getSource($partArray); } } } } return preg_match($route['rxp'], $input, $matches, PREG_OFFSET_CAPTURE); }
/** * Corrects the order of $_FILES for arrays of files. * The cleaned up array of AgaviUploadedFile objects is returned. * * @param array The array to work on. * @param string Name of the wrapper uploaded file class to instantiate. * @param array Array of indices used during recursion, initially empty. * @param array Output buffer used during recursion, initially empty. * * @author David Zülke <*****@*****.**> * @since 1.1.0 */ protected static function fixFilesArray($input, $uploadedFileClass = 'AgaviUploadedFile', $index = array(), &$output = array()) { $fromIndex = $index; if (count($fromIndex) > 0) { $first = array_shift($fromIndex); array_unshift($fromIndex, $first, 'error'); } $sub = AgaviArrayPathDefinition::getValue($fromIndex, $input); $theIndices = array(); foreach (array('name', 'type', 'size', 'tmp_name', 'error', 'is_uploaded_file') as $name) { $theIndex = $fromIndex; $first = array_shift($theIndex); array_shift($theIndex); array_unshift($theIndex, $first, $name); $theIndices[$name] = $theIndex; } if (is_array($sub)) { foreach ($sub as $key => $value) { $toIndex = array_merge($index, array($key)); if (is_array($value)) { static::fixFilesArray($input, $uploadedFileClass, $toIndex, $output); } else { $data = array(); foreach ($theIndices as $name => $theIndex) { $data[$name] = AgaviArrayPathDefinition::getValue(array_merge($theIndex, array($key)), $input, $name == 'is_uploaded_file' ? true : null); } $data = new $uploadedFileClass($data); AgaviArrayPathDefinition::setValue($toIndex, $output, $data); } } } else { $data = array(); foreach ($theIndices as $name => $theIndex) { $data[$name] = AgaviArrayPathDefinition::getValue($theIndex, $input, $name == 'is_uploaded_file' ? true : null); } $data = new $uploadedFileClass($data); AgaviArrayPathDefinition::setValue($index, $output, $data); } return $output; }