/** * Turn an array into an XML document. Alternative to array_map magic. * * @param \SimpleXMLElement $xml * @param array $array * @return \SimpleXMLElement */ public static function buildXml(SimpleXMLElement &$xml, $array) { if (is_array($array)) { foreach ($array as $key => $value) { // XML_NONE if (!is_array($value)) { $xml->addChild($key, static::unbox($value)); continue; } // Multiple nodes of the same name if (Hash::isNumeric(array_keys($value))) { foreach ($value as $kValue) { if (is_array($kValue)) { static::buildXml($xml, array($key => $kValue)); } else { $xml->addChild($key, static::unbox($kValue)); } } // XML_GROUP } else { if (isset($value['attributes'])) { if (!isset($value['value'])) { $value['value'] = null; } if (is_array($value['value'])) { $node = $xml->addChild($key); static::buildXml($node, $value['value']); } else { $node = $xml->addChild($key, static::unbox($value['value'])); } if (!empty($value['attributes'])) { foreach ($value['attributes'] as $aKey => $aValue) { $node->addAttribute($aKey, static::unbox($aValue)); } } // XML_MERGE } else { if (isset($value['value'])) { $node = $xml->addChild($key, $value['value']); unset($value['value']); if (!empty($value)) { foreach ($value as $aKey => $aValue) { if (is_array($aValue)) { static::buildXml($node, array($aKey => $aValue)); } else { $node->addAttribute($aKey, static::unbox($aValue)); } } } // XML_ATTRIBS } else { $node = $xml->addChild($key); if (!empty($value)) { foreach ($value as $aKey => $aValue) { if (is_array($aValue)) { static::buildXml($node, array($aKey => $aValue)); } else { $node->addChild($aKey, static::unbox($aValue)); } } } } } } } } return $xml; }
/** * Create a validator instance from a set of shorthand or expanded rule sets. * * @param array $data * @param array $fields * @return $this */ public static function makeFromShorthand(array $data = array(), array $fields = array()) { /** @type \Titon\Utility\Validator $obj */ $obj = new static($data); foreach ($fields as $field => $data) { $title = $field; // Convert to array if (is_string($data)) { $data = array('rules' => $data); } else { if (!is_array($data)) { continue; } else { if (Hash::isNumeric(array_keys($data))) { $data = array('rules' => $data); } } } // Prepare for parsing if (isset($data['title'])) { $title = $data['title']; } if (is_string($data['rules'])) { $data['rules'] = explode('|', $data['rules']); } $obj->addField($field, $title); foreach ($data['rules'] as $ruleOpts) { $shorthand = self::splitShorthand($ruleOpts); $obj->addRule($field, $shorthand['rule'], $shorthand['message'], $shorthand['options']); } } return $obj; }
/** * Test that isNumeric() returns true if all values are numbers. */ public function testIsNumeric() { $this->assertTrue(Hash::isNumeric(array('123', 456))); $this->assertTrue(Hash::isNumeric(array('foo' => 123, 'number' => '456'))); $this->assertFalse(Hash::isNumeric(array('foo', 'bar'))); $this->assertFalse(Hash::isNumeric(array('foo' => 'bar', 'number' => '123'))); $this->assertFalse(Hash::isNumeric(array('bar', '123'))); $this->assertFalse(Hash::isNumeric(array(null))); $this->assertFalse(Hash::isNumeric(array(true))); $this->assertFalse(Hash::isNumeric(array(false))); $this->assertFalse(Hash::isNumeric(array(array()))); $this->assertFalse(Hash::isNumeric(array(new stdClass()))); }