XRL  2.0.0
Simple XML-RPC Library (both client and server)
Encoder.php
1 <?php
2 /*
3  * This file is part of XRL, a simple XML-RPC Library for PHP.
4  *
5  * Copyright (c) 2012, XRL Team. All rights reserved.
6  * XRL is licensed under the 3-clause BSD License.
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11 
12 namespace fpoirotte\XRL;
13 
22 {
24  protected $indent;
25 
27  protected $stringTag;
28 
30  protected $timezone;
31 
54  public function __construct(
55  \DateTimeZone $timezone = null,
56  $indent = false,
57  $stringTag = false
58  ) {
59  if (!is_bool($indent)) {
60  throw new \InvalidArgumentException('$indent must be a boolean');
61  }
62  if (!is_bool($stringTag)) {
63  throw new \InvalidArgumentException('$stringTag must be a boolean');
64  }
65 
66  if ($timezone === null) {
67  try {
68  $timezone = new \DateTimeZone(@date_default_timezone_get());
69  } catch (\Exception $e) {
70  throw new \InvalidArgumentException($e->getMessage(), $e->getCode());
71  }
72  }
73 
74  $this->indent = $indent;
75  $this->stringTag = $stringTag;
76  $this->timezone = $timezone;
77  }
78 
86  protected function getWriter()
87  {
88  $writer = new \XMLWriter();
89  $writer->openMemory();
90  if ($this->indent) {
91  $writer->setIndent(true);
92  $writer->startDocument('1.0', 'UTF-8');
93  } else {
94  $writer->setIndent(false);
95  $writer->startDocument();
96  }
97  return $writer;
98  }
99 
111  protected function finalizeWrite(\XMLWriter $writer)
112  {
113  $writer->endDocument();
114  $result = $writer->outputMemory(true);
115 
116  if (!$this->indent) {
117  // Remove the XML declaration for an even
118  // more compact result.
119  if (!strncmp($result, '<'.'?xml', 5)) {
120  $pos = strpos($result, '?'.'>');
121  if ($pos !== false) {
122  $result = (string) substr($result, $pos + 2);
123  }
124  }
125  // Remove leading & trailing whitespace.
126  $result = trim($result);
127  }
128 
129  return $result;
130  }
131 
133  public function encodeRequest(\fpoirotte\XRL\Request $request)
134  {
135  $writer = $this->getWriter();
136  $writer->startElement('methodCall');
137  $writer->writeElement('methodName', $request->getProcedure());
138  if (count($request->getParams())) {
139  $writer->startElement('params');
140  foreach ($request->getParams() as $param) {
141  $writer->startElement('param');
142  $writer->startElement('value');
143  $param->write($writer, $this->timezone, $this->stringTag);
144  $writer->endElement();
145  $writer->endElement();
146  }
147  $writer->endElement();
148  }
149  $writer->endElement();
150  $result = $this->finalizeWrite($writer);
151  return $result;
152  }
153 
155  public function encodeError(\Exception $error)
156  {
157  $writer = $this->getWriter();
158  $writer->startElement('methodResponse');
159  $writer->startElement('fault');
160  $writer->startElement('value');
162  $exc->write($writer, $this->timezone, $this->stringTag);
163  $writer->endElement();
164  $writer->endElement();
165  $writer->endElement();
166  $result = $this->finalizeWrite($writer);
167  return $result;
168  }
169 
171  public function encodeResponse($response)
172  {
173  if (!($response instanceof \fpoirotte\XRL\Types\AbstractType)) {
174  throw new \InvalidArgumentException('Invalid response');
175  }
176 
177  $writer = $this->getWriter();
178  $writer->startElement('methodResponse');
179  $writer->startElement('params');
180  $writer->startElement('param');
181  $writer->startElement('value');
182  $response->write($writer, $this->timezone, $this->stringTag);
183  $writer->endElement();
184  $writer->endElement();
185  $writer->endElement();
186  $writer->endElement();
187  $result = $this->finalizeWrite($writer);
188  return $result;
189  }
190 }
$indent
Whether the output should be indented (true) or not (false).
Definition: Encoder.php:24
encodeError(\Exception $error)
Definition: Encoder.php:155
Interface for an XML-RPC encoder.
An XML-RPC encoder that can produce either compact documents or pretty documents. ...
Definition: Encoder.php:21
$timezone
Timezone used to encode date/times.
Definition: Encoder.php:30
encodeResponse($response)
Definition: Encoder.php:171
An exception that is used to represent XML-RPC errors.
Definition: Exception.php:21
finalizeWrite(\XMLWriter $writer)
Definition: Encoder.php:111
__construct(\DateTimeZone $timezone=null, $indent=false, $stringTag=false)
Definition: Encoder.php:54
$stringTag
Whether the "\<string\>" tag should be used (true) or not (false).
Definition: Encoder.php:27
A class that represents an XML-RPC request.
Definition: Request.php:20
encodeRequest(\fpoirotte\XRL\Request $request)
Definition: Encoder.php:133