[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/externals/httpful/src/Httpful/Handlers/ -> XmlHandler.php (source)

   1  <?php
   2  /**
   3   * Mime Type: application/xml
   4   *
   5   * @author Zack Douglas <[email protected]>
   6   * @author Nathan Good <[email protected]>
   7   */
   8  
   9  namespace Httpful\Handlers;
  10  
  11  class XmlHandler extends MimeHandlerAdapter
  12  {
  13      /**
  14       *  @var string $namespace xml namespace to use with simple_load_string
  15       */
  16      private $namespace;
  17  
  18      /**
  19       * @var int $libxml_opts see http://www.php.net/manual/en/libxml.constants.php
  20       */
  21      private $libxml_opts;
  22  
  23      /**
  24       * @param array $conf sets configuration options
  25       */
  26      public function __construct(array $conf = array())
  27      {
  28          $this->namespace =      isset($conf['namespace']) ? $conf['namespace'] : '';
  29          $this->libxml_opts =    isset($conf['libxml_opts']) ? $conf['libxml_opts'] : 0;
  30      }
  31  
  32      /**
  33       * @param string $body
  34       * @return mixed
  35       * @throws Exception if unable to parse
  36       */
  37      public function parse($body)
  38      {
  39          if (empty($body))
  40              return null;
  41          $parsed = simplexml_load_string($body, null, $this->libxml_opts, $this->namespace);
  42          if ($parsed === false)
  43              throw new \Exception("Unable to parse response as XML");
  44          return $parsed;
  45      }
  46  
  47      /**
  48       * @param mixed $payload
  49       * @return string
  50       * @throws Exception if unable to serialize
  51       */
  52      public function serialize($payload)
  53      {
  54          list($_, $dom) = $this->_future_serializeAsXml($payload);
  55          return $dom->saveXml();
  56      }
  57  
  58      /**
  59       * @author Zack Douglas <[email protected]>
  60       */
  61      private function _future_serializeAsXml($value, $node = null, $dom = null)
  62      {
  63          if (!$dom) {
  64              $dom = new \DOMDocument;
  65          }
  66          if (!$node) {
  67              if (!is_object($value)) {
  68                  $node = $dom->createElement('response');
  69                  $dom->appendChild($node);
  70              } else {
  71                  $node = $dom;
  72              }
  73          }
  74          if (is_object($value)) {
  75              $objNode = $dom->createElement(get_class($value));
  76              $node->appendChild($objNode);
  77              $this->_future_serializeObjectAsXml($value, $objNode, $dom);
  78          } else if (is_array($value)) {
  79              $arrNode = $dom->createElement('array');
  80              $node->appendChild($arrNode);
  81              $this->_future_serializeArrayAsXml($value, $arrNode, $dom);
  82          } else if (is_bool($value)) {
  83              $node->appendChild($dom->createTextNode($value?'TRUE':'FALSE'));
  84          } else {
  85              $node->appendChild($dom->createTextNode($value));
  86          }
  87          return array($node, $dom);
  88      }
  89      /**
  90       * @author Zack Douglas <[email protected]>
  91       */
  92      private function _future_serializeArrayAsXml($value, &$parent, &$dom)
  93      {
  94          foreach ($value as $k => &$v) {
  95              $n = $k;
  96              if (is_numeric($k)) {
  97                  $n = "child-{$n}";
  98              }
  99              $el = $dom->createElement($n);
 100              $parent->appendChild($el);
 101              $this->_future_serializeAsXml($v, $el, $dom);
 102          }
 103          return array($parent, $dom);
 104      }
 105      /**
 106       * @author Zack Douglas <[email protected]>
 107       */
 108      private function _future_serializeObjectAsXml($value, &$parent, &$dom)
 109      {
 110          $refl = new \ReflectionObject($value);
 111          foreach ($refl->getProperties() as $pr) {
 112              if (!$pr->isPrivate()) {
 113                  $el = $dom->createElement($pr->getName());
 114                  $parent->appendChild($el);
 115                  $this->_future_serializeAsXml($pr->getValue($value), $el, $dom);
 116              }
 117          }
 118          return array($parent, $dom);
 119      }
 120  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1