/** * Retrieves a parameter. * * @param string $name A parameter name * @param mixed $default A default parameter value * * @return mixed A parameter value, if the parameter exists, otherwise null */ public function &get($name, $default = null) { if (array_key_exists($name, $this->parameters)) { $value =& $this->parameters[$name]; } else { $value = sfToolkit::getArrayValueForPath($this->parameters, $name, $default); } return $value; }
public function executePreview(sfWebRequest $request) { if (false === ($markdown = $request->getParameter('markdown_value', false))) { $field = $request->getParameter('markdown_field'); $markdown = sfToolkit::getArrayValueForPath($request->getParameterHolder()->getAll(), $field, false); if ($markdown === false) { throw new sfException("Cannot generate preview: markdown value or markdown form field not found in request"); } } $parser = new MarkdownExtra_Parser(); // $this->markdown = $parser->transform($markdown); $markdown = $markdown ? $markdown : '_No Markdown To Preview!_'; return $this->renderText($parser->transform($markdown)); }
/** * Retrieve a parameter with an optionally specified namespace. * * An isolated namespace may be identified by providing a value for the third * argument. If not specified, the default namespace 'symfony/default' is * used. * * @param string A parameter name. * @param mixed A default parameter value. * @param string A parameter namespace. * * @return mixed A parameter value, if the parameter exists, otherwise null. */ public function &get($name, $default = null, $ns = null) { if (!$ns) { $ns = $this->default_namespace; } if (isset($this->parameters[$ns][$name])) { $value =& $this->parameters[$ns][$name]; } else { if (isset($this->parameters[$ns])) { $value = sfToolkit::getArrayValueForPath($this->parameters[$ns], $name, $default); } else { $value = $default; } } return $value; }
protected function getValue($values, $name) { if (array_key_exists($name, $values)) { return $values[$name]; } return sfToolkit::getArrayValueForPath($values, $name); }
/** * Returns the value of a parameter passed as a URL segment. * * @param string $name The parameter name * @param string $default The default value * * @return string The parameter value */ public function getUrlParameter($name, $default = null) { if (isset($this->requestParameters[$name])) { return $this->requestParameters[$name]; } else { return sfToolkit::getArrayValueForPath($this->requestParameters, $name, $default); } }
protected function hasValue($values, $name) { if (array_key_exists($name, $values)) { return true; } return null !== sfToolkit::getArrayValueForPath($values, $name); }
/** * Simulates a click on the supplied DOM element. * * This method is called internally by the {@link click()} method. * * @param DOMElement $item The element being clicked * @param array $arguments The arguments to pass to the link * @param array $options An array of options * * @return array An array composed of the URI, the method and the arguments to pass to the call() call * * @uses getResponseDomXpath() */ public function doClickElement(DOMElement $item, $arguments = array(), $options = array()) { $method = strtolower(isset($options['method']) ? $options['method'] : 'get'); if ('a' == $item->nodeName) { if (in_array($method, array('post', 'put', 'delete'))) { if (isset($options['_with_csrf']) && $options['_with_csrf']) { $arguments['_with_csrf'] = true; } return array($item->getAttribute('href'), $method, $arguments); } else { return array($item->getAttribute('href'), 'get', $arguments); } } else { if ('button' == $item->nodeName || 'input' == $item->nodeName && in_array($item->getAttribute('type'), array('submit', 'button', 'image'))) { // add the item's value to the arguments $this->parseArgumentAsArray($item->getAttribute('name'), $item->getAttribute('value'), $arguments); // use the ancestor form element do { if (null === ($item = $item->parentNode)) { throw new Exception('The clicked form element does not have a form ancestor.'); } } while ('form' != $item->nodeName); } } // form attributes $url = $item->getAttribute('action'); if (!$url || '#' == $url) { $url = $this->stack[$this->stackPosition]['uri']; } $method = strtolower(isset($options['method']) ? $options['method'] : ($item->getAttribute('method') ? $item->getAttribute('method') : 'get')); // merge form default values and arguments $defaults = array(); $arguments = sfToolkit::arrayDeepMerge($this->fields, $arguments); $xpath = $this->getResponseDomXpath(); foreach ($xpath->query('descendant::input | descendant::textarea | descendant::select', $item) as $element) { if ($element->hasAttribute('disabled')) { continue; } $elementName = $element->getAttribute('name'); $nodeName = $element->nodeName; $value = null; if ($nodeName == 'input' && ($element->getAttribute('type') == 'checkbox' || $element->getAttribute('type') == 'radio')) { if ($element->getAttribute('checked')) { $value = $element->hasAttribute('value') ? $element->getAttribute('value') : '1'; } } else { if ($nodeName == 'input' && $element->getAttribute('type') == 'file') { $filename = array_key_exists($elementName, $arguments) ? $arguments[$elementName] : sfToolkit::getArrayValueForPath($arguments, $elementName, ''); if (is_readable($filename)) { $fileError = UPLOAD_ERR_OK; $fileSize = filesize($filename); } else { $fileError = UPLOAD_ERR_NO_FILE; $fileSize = 0; } unset($arguments[$elementName]); $this->parseArgumentAsArray($elementName, array('name' => basename($filename), 'type' => '', 'tmp_name' => $filename, 'error' => $fileError, 'size' => $fileSize), $this->files); } else { if ('input' == $nodeName && !in_array($element->getAttribute('type'), array('submit', 'button', 'image'))) { $value = $element->getAttribute('value'); } else { if ($nodeName == 'textarea') { $value = ''; foreach ($element->childNodes as $el) { $value .= $this->getResponseDom()->saveXML($el); } } else { if ($nodeName == 'select') { if ($multiple = $element->hasAttribute('multiple')) { $elementName = str_replace('[]', '', $elementName); $value = array(); } else { $value = null; } $found = false; foreach ($xpath->query('descendant::option', $element) as $option) { if ($option->getAttribute('selected')) { $found = true; if ($multiple) { $value[] = $option->getAttribute('value'); } else { $value = $option->getAttribute('value'); } } } // if no option is selected and if it is a simple select box, take the first option as the value $option = $xpath->query('descendant::option', $element)->item(0); if (!$found && !$multiple && $option instanceof DOMElement) { $value = $option->getAttribute('value'); } } } } } } if (null !== $value) { $this->parseArgumentAsArray($elementName, $value, $defaults); } } // create request parameters $arguments = sfToolkit::arrayDeepMerge($defaults, $arguments); if (in_array($method, array('post', 'put', 'delete'))) { return array($url, $method, $arguments); } else { $queryString = http_build_query($arguments, null, '&'); $sep = false === strpos($url, '?') ? '?' : '&'; return array($url . ($queryString ? $sep . $queryString : ''), 'get', array()); } }
$t->is(sfToolkit::getArrayValueForPath($arr, 'barfoo', 'bar'), 'bar', '::getArrayValueForPath() takes a default value as its third argument'); $t->is(sfToolkit::getArrayValueForPath($arr, 'foo[bar][baz]'), 'foo bar', '::getArrayValueForPath() works with deep paths'); $t->is(sfToolkit::getArrayValueForPath($arr, 'foo[bar][bar]'), false, '::getArrayValueForPath() works with deep paths'); $t->is(sfToolkit::getArrayValueForPath($arr, 'foo[bar][bar]', 'bar'), 'bar', '::getArrayValueForPath() works with deep paths'); $t->is(sfToolkit::getArrayValueForPath($arr, 'foo[]'), array('bar' => array('baz' => 'foo bar')), '::getArrayValueForPath() accepts a [] at the end to check for an array'); $t->is(sfToolkit::getArrayValueForPath($arr, 'foobar[]'), null, '::getArrayValueForPath() accepts a [] at the end to check for an array'); $t->is(sfToolkit::getArrayValueForPath($arr, 'barfoo[]'), null, '::getArrayValueForPath() accepts a [] at the end to check for an array'); $t->is(sfToolkit::getArrayValueForPath($arr, 'foobar[]', 'foo'), 'foo', '::getArrayValueForPath() accepts a [] at the end to check for an array'); $t->is(sfToolkit::getArrayValueForPath($arr, 'bar[1]'), 'bar', '::getArrayValueForPath() can take an array indexed by integer'); $t->is(sfToolkit::getArrayValueForPath($arr, 'bar[2]'), null, '::getArrayValueForPath() can take an array indexed by integer'); $t->is(sfToolkit::getArrayValueForPath($arr, 'bar[2]', 'foo'), 'foo', '::getArrayValueForPath() can take an array indexed by integer'); $t->is(sfToolkit::getArrayValueForPath($arr, 'foo[bar][baz][booze]'), null, '::getArrayValueForPath() is not fooled by php mistaking strings and array'); $t->is(sfToolkit::getArrayValueForPathByRef($arr, 'foo[bar][baz][booze]'), null, '::getArrayValueForPathByRef() is not fooled by php mistaking strings and array'); // ::removeArrayValueForPath() $t->diag('::removeArrayValueForPath()'); $t->is(sfToolkit::removeArrayValueForPath($arr, 'foobar'), 'foo', '::removeArrayValueForPath() returns the removed value'); $t->is($arr, array( 'foo' => array( 'bar' => array( 'baz' => 'foo bar', ), ), 'bar' => array( 'foo', 'bar', ),
/** * Simulates a click on a link or button. * * This method is called internally by the click() method. * * Available options: * * * position: The position of the linked to link if several ones have the same name * (the first one is 1, not 0) * * method: The method to used instead of the form ones * (useful when you need to click on a link that is converted to a form with JavaScript code) * * @param string $name The link or button text * @param array $arguments The arguments to pass to the link * @param array $options An array of options * * @return array An array composed of the URI, the method and the arguments to pass to the call() call */ public function doClick($name, $arguments = array(), $options = array()) { $position = isset($options['position']) ? $options['position'] - 1 : 0; $dom = $this->getResponseDom(); if (!$dom) { throw new LogicException('Cannot click because there is no current page in the browser.'); } $xpath = new DomXpath($dom); $method = strtolower(isset($options['method']) ? $options['method'] : 'get'); $query = sprintf('//a[.="%s"]', $name); $query .= sprintf('|//a/img[@alt="%s"]/ancestor::a', $name); $query .= sprintf('|//input[((@type="submit" or @type="button") and @value="%s") or (@type="image" and @alt="%s")]/ancestor::form', $name, $name); $query .= sprintf('|//button[.="%s" or @id="%s" or @name="%s"]/ancestor::form', $name, $name, $name); $list = $xpath->query($query); if (!($item = $list->item($position))) { throw new InvalidArgumentException(sprintf('Cannot find the "%s" link or button.', $name)); } if ('a' == $item->nodeName) { if (in_array($method, array('post', 'put', 'delete'))) { if (isset($options['_with_csrf']) && $options['_with_csrf']) { $arguments['_with_csrf'] = true; } return array($item->getAttribute('href'), $method, $arguments); } else { return array($item->getAttribute('href'), 'get', $arguments); } } // form attributes $url = $item->getAttribute('action'); if (!$url || '#' == $url) { $url = $this->stack[$this->stackPosition]['uri']; } $method = strtolower(isset($options['method']) ? $options['method'] : ($item->getAttribute('method') ? $item->getAttribute('method') : 'get')); // merge form default values and arguments $defaults = array(); $arguments = sfToolkit::arrayDeepMerge($this->fields, $arguments); foreach ($xpath->query('descendant::input | descendant::textarea | descendant::select', $item) as $element) { $elementName = $element->getAttribute('name'); $nodeName = $element->nodeName; $value = null; if ($nodeName == 'input' && ($element->getAttribute('type') == 'checkbox' || $element->getAttribute('type') == 'radio')) { if ($element->getAttribute('checked')) { $value = $element->hasAttribute('value') ? $element->getAttribute('value') : '1'; } } else { if ($nodeName == 'input' && $element->getAttribute('type') == 'file') { $filename = array_key_exists($elementName, $arguments) ? $arguments[$elementName] : sfToolkit::getArrayValueForPath($arguments, $elementName, ''); if (is_readable($filename)) { $fileError = UPLOAD_ERR_OK; $fileSize = filesize($filename); } else { $fileError = UPLOAD_ERR_NO_FILE; $fileSize = 0; } unset($arguments[$elementName]); $this->parseArgumentAsArray($elementName, array('name' => basename($filename), 'type' => '', 'tmp_name' => $filename, 'error' => $fileError, 'size' => $fileSize), $this->files); } else { if ($nodeName == 'input' && ($element->getAttribute('type') != 'submit' && $element->getAttribute('type') != 'button' || $element->getAttribute('value') == $name) && ($element->getAttribute('type') != 'image' || $element->getAttribute('alt') == $name)) { $value = $element->getAttribute('value'); } else { if ($nodeName == 'textarea') { $value = ''; foreach ($element->childNodes as $el) { $value .= $dom->saveXML($el); } } else { if ($nodeName == 'select') { if ($multiple = $element->hasAttribute('multiple')) { $elementName = str_replace('[]', '', $elementName); $value = array(); } else { $value = null; } $found = false; foreach ($xpath->query('descendant::option', $element) as $option) { if ($option->getAttribute('selected')) { $found = true; if ($multiple) { $value[] = $option->getAttribute('value'); } else { $value = $option->getAttribute('value'); } } } // if no option is selected and if it is a simple select box, take the first option as the value $option = $xpath->query('descendant::option', $element)->item(0); if (!$found && !$multiple && $option instanceof DOMElement) { $value = $option->getAttribute('value'); } } } } } } if (null !== $value) { $this->parseArgumentAsArray($elementName, $value, $defaults); } } // create request parameters $arguments = sfToolkit::arrayDeepMerge($defaults, $arguments); if (in_array($method, array('post', 'put', 'delete'))) { return array($url, $method, $arguments); } else { $queryString = http_build_query($arguments, null, '&'); $sep = false === strpos($url, '?') ? '?' : '&'; return array($url . ($queryString ? $sep . $queryString : ''), 'get', array()); } }
/** * Retrieves all the values from a file. * * @param string A file name * * @return array Associative list of the file values */ public function getFileValues($name) { if (strpos($name, '[')) { return sfToolkit::getArrayValueForPath($this->filesInfos, $name); } else { return isset($this->filesInfos[$name]) ? $this->filesInfos[$name] : null; } }