public function stringToDateTime($data) { if (get_class($data) == 'SDateTime') { return $data; } try { $date = SDateTime::parse($data); } catch (Exception $e) { return null; } return $date; }
public static function typecast($xmlString) { try { $xml = new SimpleXMLElement($xmlString); } catch (Exception $e) { throw new SXmlRpcValueException("Failed to typecast XML value : {$xmlString}"); } list($type, $value) = each($xml); if (!$type) { $type = 'string'; } switch ($type) { case 'i4': case 'int': return (int) $value; break; case 'double': return (double) $value; break; case 'boolean': return $value == 1; break; case 'string': return $value; break; case 'dateTime.iso8601': return SDateTime::parse($value); break; case 'base64': break; case 'array': if (!$value instanceof SimpleXMLElement) { throw new SXmlRpcValueException('Invalid XML string for array type'); } $values = array(); foreach ($value->data->value as $element) { $values[] = self::typecast($element->asXML()); } return $values; break; case 'struct': if (!$value instanceof SimpleXMLElement) { throw new SXmlRpcValueException('Invalid XML string for struct type'); } $values = array(); foreach ($value->member as $member) { if (!$member->value instanceof SimpleXMLElement || empty($member->value)) { throw new SXmlRpcValueException('Member of a struct must contain a <value> tag'); } $values[(string) $member->name] = self::typecast($member->value->asXML()); } return $values; break; default: throw new SXmlRpcValueException("{$type} is not a native XML-RPC type"); break; } }
public function testParsing() { $this->assertEqual(new SDate(1969, 7, 21), SDate::parse('1969-07-21')); $this->assertEqual(new SDateTime(1969, 7, 21, 20, 35, 05), SDateTime::parse('19690721T20:35:05')); $this->assertEqual(new SDateTime(1969, 7, 21, 20, 35, 05), SDateTime::parse('1969-07-21 20:35:05')); }