/** * Convert array collection to XML * @param array $array * @param array $options */ public function arrayToXml($array, $options = array()) { $firstElt = current($array); $firstType = is_object($firstElt) ? get_class($firstElt) : gettype($firstElt); $sameTypes = true; foreach ($array as $element) { // either an array or object with toXml method if (!is_array($element) && !is_callable(array($element, 'toXml'))) { throw new Mad_Support_Exception("Not all elements respond to toXml"); } if (get_class($element) != $firstType) { $sameTypes = false; } } if (!isset($options['root'])) { if ($sameTypes && count($array) > 0) { $options['root'] = Mad_Support_Inflector::pluralize($firstType); } else { $options['root'] = 'records'; } } if (!isset($options['children'])) { $options['children'] = Mad_Support_Inflector::singularize($options['root']); } if (!isset($options['indent'])) { $options['indent'] = 2; } if (!isset($options['skipTypes'])) { $options['skipTypes'] = false; } if (empty($options['builder'])) { $options['builder'] = new Mad_Support_Builder(array('indent' => $options['indent'])); } $root = $options['root']; unset($options['root']); $children = $options['children']; unset($options['children']); if (!array_key_exists('dasherize', $options) || !empty($options['dasherize'])) { $root = Mad_Support_Inflector::dasherize($root); } if (empty($options['skipInstruct'])) { $options['builder']->instruct(); } $opts = array_merge($options, array('root' => $children)); $builder = $options['builder']; $attrs = $options['skipTypes'] ? array() : array('type' => 'array'); // no elements in array if (count($array) == 0) { $builder->tag($root, '', $attrs); // build xml from elements } else { $tag = $builder->startTag($root, '', $attrs); $opts['skipInstruct'] = true; foreach ($array as $element) { // associative array if (is_array($element) && !is_int(key($element))) { $this->hashToXml($element, $opts); // array } elseif (is_array($element)) { $this->arrayToXml($element, $opts); // object } else { $element->toXml($opts); } } $tag->end(); } return $builder->__toString(); }
public function testDasherize() { $this->assertEquals('derek', Mad_Support_Inflector::dasherize('Derek')); $this->assertEquals('dereks-test', Mad_Support_Inflector::dasherize('dereksTest')); $this->assertEquals('dereks-test', Mad_Support_Inflector::dasherize('DereksTest')); $this->assertEquals('dereks-test', Mad_Support_Inflector::dasherize('Dereks_Test')); $this->assertEquals('dereks-name-test', Mad_Support_Inflector::dasherize('DereksName_Test')); $this->assertEquals('derek', Mad_Support_Inflector::dasherize('derek')); $this->assertEquals('dereks-test', Mad_Support_Inflector::dasherize('dereks_test')); }
/** * proxy to support dasherize * @param string $name * @return string */ public function dasherize($name) { return $this->isDasherized() ? Mad_Support_Inflector::dasherize($name) : $name; }
/** * Render a response that has no content (merely headers). The options * argument is interpreted to be a hash of header names and values. * This allows you to easily return a response that consists only of * significant headers: * * head('created', array('location' => 'http://foo')) * head(array('status' => 'created', 'location' => 'http://foo')) * * @param integer|string|array $first Status code or options array * @param array $second Options array * @return void */ protected function head($first, $second = array()) { if (is_array($first)) { $options = $first; if (isset($options['status'])) { $status = $options['status']; unset($options['status']); } else { $status = 'ok'; } } else { $status = $first; $options = $second; } $status = $this->interpretStatus($status); foreach ($options as $key => $value) { $dashed = Mad_Support_Inflector::dasherize($key); $spaced = str_replace('-', ' ', $dashed); $spaced = ucwords($spaced); $dashed = str_replace(' ', '-', $spaced); $this->_response->setHeader("{$dashed}: {$value}", $replace = true); } $this->_response->setStatus($status); $this->render(array('nothing' => true)); }