示例#1
0
 /**
  * @covers \Cougar\RestService\RestService::body
  */
 public function testBodyXmlAsObject()
 {
     global $_BODY;
     $xml = new \SimpleXMLElement("<unit_test/>");
     $xml->addChild("key", "value");
     $_BODY = $xml->asXML();
     $this->assertEquals(Xml::toObject($xml), $this->object->body("object"));
 }
示例#2
0
文件: XmlTest.php 项目: alfmel/cougar
 /**
  * @covers \Cougar\Util\Xml::toObject
  * @depends testToXmlWithComplexObject
  */
 public function testToObjectWithComplexObject()
 {
     $object = new \stdClass();
     $object->property1 = "value1";
     $object->property2 = "value2";
     $object->property3 = "value3";
     $object->property4 = "value4";
     $object->subClass = new \stdClass();
     $object->subClass->subProperty1 = "subvalue1";
     $object->subClass->subProperty2 = "subvalue2";
     $object->array = array("a", "b", "c", "d", "e");
     $object->jaggedArray = array("a", "b", "c", "d", "e", array("x", "y", "z"));
     $object->associativeArray = $array = array("a" => 1, "b" => 2, "c" => 3, "d" => 4, "e" => 5);
     $object->jaggedAssociativeArray = array("a" => 1, "b" => 2, "c" => 3, "d" => 4, "e" => 5, "extra" => array("x" => "foo", "y" => "bar", "z" => "baz"));
     $xml = Xml::toXml($object);
     $new_object = Xml::toObject($xml);
     $object->array = json_decode(json_encode($object->array));
     $object->jaggedArray = json_decode(json_encode($object->jaggedArray));
     $object->associativeArray = json_decode(json_encode($object->associativeArray));
     $object->jaggedAssociativeArray = json_decode(json_encode($object->jaggedAssociativeArray));
     $this->assertEquals($object, $new_object);
 }
示例#3
0
 /**
  * Returns the body of the request, optionally parsing it as a specified
  * type of object. These are:
  *
  *   XML    - Parse the body as XML and return as a SimpleXML object
  *   OBJECT - Parse the body as a JSON, XML or PHP serialized object and
  *            return as an object
  *   ARRAY  - Parse the body as a JSON or XML object and return as an assoc.
  *            array
  *   PHP    - Parse the body as serialized PHP data
  *
  * If parsing fails the call will throw a BadRequestException.
  *
  * If no parse type is specified, the body will be returned as a string.
  *
  * @history
  * 2013.09.30:
  *   (AT)  Initial release
  * 2014.08:06:
  *   (AT)  Allow conversion of XML to object or array, or PHP to object
  *
  * @version 2014.08.06
  * @author (AT) Alberto Trevino, Brigham Young Univ. <*****@*****.**>
  *
  * @param string $parse_type xml|object|array|php
  * @throws \Cougar\Exceptions\BadRequestException
  * @return mixed Body
  */
 public function body($parse_type = null)
 {
     if ($this->body === null) {
         # Get the body
         if ($this->__testMode) {
             # In test mode, read the body from the $_BODY variable
             global $_BODY;
             $this->body = trim($_BODY);
         } else {
             $this->body = trim(file_get_contents("php://input"));
         }
     }
     # See if we will be parsing the data
     if ($this->body) {
         switch (strtolower($parse_type)) {
             case "xml":
                 return new \SimpleXMLElement($this->body);
                 break;
             case "object":
                 try {
                     return Xml::toObject(new \SimpleXMLElement($this->body));
                 } catch (\Exception $e) {
                     try {
                         return unserialize($this->body);
                     } catch (\Exception $e) {
                         $object = json_decode($this->body);
                         if ($object === null) {
                             throw new BadRequestException("Body must be a valid JSON, XML or " . "serialized PHP object");
                         }
                         return $object;
                     }
                 }
                 break;
             case "array":
                 try {
                     return Xml::toArray(new \SimpleXMLElement($this->body));
                 } catch (\Exception $e) {
                     $object = json_decode($this->body, true);
                     if ($object === null) {
                         throw new BadRequestException("Body must be a valid JSON or XML object");
                     }
                     return $object;
                 }
                 break;
             case "php":
                 return unserialize($this->body);
                 break;
             default:
                 return $this->body;
                 break;
         }
     } else {
         return $this->body;
     }
 }