XRL  latest
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  $timezone = new \DateTimeZone(@date_default_timezone_get());
68  }
69 
70  $this->indent = $indent;
71  $this->stringTag = $stringTag;
72  $this->timezone = $timezone;
73  }
74 
82  protected function getWriter()
83  {
84  $writer = new \XMLWriter();
85  $writer->openMemory();
86  if ($this->indent) {
87  $writer->setIndent(true);
88  $writer->startDocument('1.0', 'UTF-8');
89  } else {
90  $writer->setIndent(false);
91  $writer->startDocument();
92  }
93  return $writer;
94  }
95 
107  protected function finalizeWrite(\XMLWriter $writer)
108  {
109  $writer->endDocument();
110  $result = $writer->outputMemory(true);
111 
112  if (!$this->indent) {
113  // Remove the XML declaration for an even
114  // more compact result.
115  if (!strncmp($result, '<'.'?xml', 5)) {
116  $pos = strpos($result, '?'.'>');
117  if ($pos !== false) {
118  $result = (string) substr($result, $pos + 2);
119  }
120  }
121  // Remove leading & trailing whitespace.
122  $result = trim($result);
123  }
124 
125  return $result;
126  }
127 
129  public function encodeRequest(\fpoirotte\XRL\Request $request)
130  {
131  $writer = $this->getWriter();
132  $writer->startElement('methodCall');
133  $writer->writeElement('methodName', $request->getProcedure());
134  if (count($request->getParams())) {
135  $writer->startElement('params');
136  foreach ($request->getParams() as $param) {
137  $writer->startElement('param');
138  $writer->startElement('value');
139  $param->write($writer, $this->timezone, $this->stringTag);
140  $writer->endElement();
141  $writer->endElement();
142  }
143  $writer->endElement();
144  }
145  $writer->endElement();
146  $result = $this->finalizeWrite($writer);
147  return $result;
148  }
149 
151  public function encodeError(\Exception $error)
152  {
153  $writer = $this->getWriter();
154  $writer->startElement('methodResponse');
155  $writer->startElement('fault');
156  $writer->startElement('value');
158  $exc->write($writer, $this->timezone, $this->stringTag);
159  $writer->endElement();
160  $writer->endElement();
161  $writer->endElement();
162  $result = $this->finalizeWrite($writer);
163  return $result;
164  }
165 
167  public function encodeResponse($response)
168  {
169  if (!($response instanceof \fpoirotte\XRL\Types\AbstractType)) {
170  throw new \InvalidArgumentException('Invalid response');
171  }
172 
173  $writer = $this->getWriter();
174  $writer->startElement('methodResponse');
175  $writer->startElement('params');
176  $writer->startElement('param');
177  $writer->startElement('value');
178  $response->write($writer, $this->timezone, $this->stringTag);
179  $writer->endElement();
180  $writer->endElement();
181  $writer->endElement();
182  $writer->endElement();
183  $result = $this->finalizeWrite($writer);
184  return $result;
185  }
186 }
$indent
Whether the output should be indented (true) or not (false).
Definition: Encoder.php:24
encodeError(\Exception $error)
Definition: Encoder.php:151
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:167
An exception that is used to represent XML-RPC errors.
Definition: Exception.php:21
finalizeWrite(\XMLWriter $writer)
Definition: Encoder.php:107
__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:129