12 namespace fpoirotte\XRL;
48 protected static function isUTF8($text)
52 return (
bool) preg_match(
54 [\x09\x0A\x0D\x20-\x7E] # ASCII
55 | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
56 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
57 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
58 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
59 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
60 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
61 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
113 switch (gettype($value)) {
117 return new \fpoirotte\XRL\Types\Nil(null);
120 return new \fpoirotte\XRL\Types\Boolean($value);
124 return new \fpoirotte\XRL\Types\I4($value);
125 }
catch (\InvalidArgumentException $e) {
127 return new \fpoirotte\XRL\Types\I8($value);
130 return new \fpoirotte\XRL\Types\Double($value);
134 if (static::isUTF8($value)) {
135 return new \fpoirotte\XRL\Types\StringType($value);
137 return new \fpoirotte\XRL\Types\Base64($value);
140 $newValue = array_map(
"static::convert", $value);
142 return new \fpoirotte\XRL\Types\ArrayType($newValue);
143 }
catch (\InvalidArgumentException $e) {
145 return new \fpoirotte\XRL\Types\Struct($newValue);
154 if ($value instanceof \fpoirotte\XRL\Types\AbstractType) {
158 if ($value instanceof \GMP ||
159 (is_resource($value) && get_resource_type($value) ===
'GMP integer')) {
161 '\\fpoirotte\\XRL\\Types\\I4',
162 '\\fpoirotte\\XRL\\Types\\I8',
163 '\\fpoirotte\\XRL\\Types\\BigInteger',
165 foreach ($candidates as $candidate) {
167 return new $candidate($value);
168 }
catch (\InvalidArgumentException $e) {
173 if ($value instanceof \DateTime) {
174 return new \fpoirotte\XRL\Types\DateTimeIso8601($value);
177 if (($value instanceof \DOMNode) ||
178 ($value instanceof \XMLWriter) ||
179 ($value instanceof \SimpleXMLElement)) {
180 return new \fpoirotte\XRL\Types\Dom($value);
184 return new \fpoirotte\XRL\Types\Struct(
186 'faultCode' =>
new \fpoirotte\XRL\Types\IntType($value->getCode()),
187 'faultString' =>
new \fpoirotte\XRL\Types\StringType(
188 get_class($value).
': '.$value->getMessage()
194 if (is_object($value) && (
195 ($value instanceof \Serializable) ||
196 method_exists($value,
'__sleep'))) {
197 $value = serialize($value);
200 if (static::isUTF8($value)) {
201 return new \fpoirotte\XRL\Types\StringType($value);
203 return new \fpoirotte\XRL\Types\Base64($value);
206 throw new \InvalidArgumentException(
'Unconvertible type');
212 $newParams = array_map(
'static::convert', $request->getParams());
213 return $this->encoder->encodeRequest(
214 new \fpoirotte\XRL\
Request($request->getProcedure(), $newParams)
221 return $this->encoder->encodeError($error);
227 return $this->encoder->encodeResponse(static::convert($response));
encodeResponse($response)
Interface for an XML-RPC encoder.
An exception that is used to represent XML-RPC errors.
__construct(\fpoirotte\XRL\EncoderInterface $encoder)
encodeError(\Exception $error)
An XML-RPC encoder that transparently converts PHP types to their XML-RPC counterpart.
encodeRequest(\fpoirotte\XRL\Request $request)
A class that represents an XML-RPC request.