public function test_reverse() { $arr = array('value1', 'value2'); $this->assertEquals(Core_Arrays::reverse($arr), array('value2', 'value1')); $this->assertEquals(Core_Arrays::reverse($arr, true), array(1 => 'value2', 0 => 'value1')); }
/** * @param string $name * @param array $args * * @return mixed */ public function __call($name, $args) { if (!Core_Regexps::match('{_url$}', $name)) { throw new Core_MissingMethodException($name); } $name = Core_Strings::replace($name, 'single_', 'single-'); $url = ''; $args = Core_Arrays::reverse($args); $parms = Core_Types::is_array($args[0]) ? Core_Arrays::shift($args) : array(); $parts = Core_Arrays::reverse(Core_Strings::split_by('_', Core_Regexps::replace('{_url$}', '', $name))); $last_idx = count($parts) - 1; $target = false; foreach ($parts as $idx => $part) { $part = Core_Strings::replace($part, '-', '_'); if ($target) { if (isset($this->single_names[$part])) { $url = '/' . $this->single_names[$part] . '/' . (Core_Types::is_object($arg = Core_Arrays::shift($args)) ? $arg->id : (string) $arg) . $url; } elseif ($idx == $last_idx && (isset($this->resources[$target]) && isset($this->resources[$target]['collection']) && isset($this->resources[$target]['collection'][$part])) || isset($this->single_names[$target]) && isset($this->resources[$this->single_names[$target]]['instance']) && isset($this->resources[$this->single_names[$target]]['instance'][$part])) { $url .= "/{$part}"; } else { throw new Core_MissingMethodException($name); } } else { if (isset($this->resources[$part])) { $url = "/{$part}{$url}"; $target = $part; } elseif (isset($this->single_names[$part])) { $id = Core_Arrays::shift($args); $url = '/' . $this->single_names[$part] . '/' . (Core_Types::is_object($id) ? $id->id : (string) $id) . $url; $target = $part; } else { throw new Core_MissingMethodException($name); } } } return $this->add_keyword_parameters($this->add_path($url) . (isset($args[0]) ? '.' . $args[0] : ".{$this->default_format}"), $parms); }