public function testSingularizeWords() { $input = file_get_contents(dirname(__FILE__) . '/xml/encode_singularize.xml'); $xml = Decode::xml($input, array('singularize_words' => true, 'include_words' => array('obrazok' => 'obrazok'), 'exclude_words' => array('images'))); $this->assertInternalType('array', $xml->toArray()); $this->assertInternalType('object', $xml->toObject()); $this->assertInstanceOf('\\Serializers\\Encoders\\Json', $xml->toJSON()); $compared = $this->arraysAreEqual(array('shop' => 'supercars.com', 'cars' => array(array('manufacturer' => 'VW', 'model' => 'Golf', 'engine' => '1.9'), array('manufacturer' => 'Škoda', 'model' => 'Rapid', 'engine' => '1.6')), 'images' => array('image' => array(array('name' => 'foo', 'extension' => 'jpg'), array('name' => 'bar', 'extension' => 'jpg'))), 'obrazky' => array(array('name' => 'foo', 'extension' => 'jpg'))), $xml->toArray()); $this->assertEquals($compared, true); }
public function testDecode() { $input = $this->getJSON(); $json = Decode::jsonp($input); $this->assertInternalType('array', $json->toArray()); $this->assertInternalType('object', $json->toObject()); $this->assertInstanceOf('\\Serializers\\Encoders\\Xml', $json->toXml('root')); $this->assertInstanceOf('\\Serializers\\Encoders\\Yaml', $json->toYaml()); $output = $json->toArray(); $this->assertInternalType('string', $output['large']); $compared = $this->arraysAreEqual(array('foo' => 'bar', 'small' => 123456, 'large' => '200000000000009093302', 'text' => 'Example ratio 1000000000000000:1', 'date' => '2015-03-05 12:52:57'), $output); $this->assertEquals($compared, true); }
public function testEvents() { $input = $this->getJSON(); $json = Decode::json($input)->on(\Serializers\Events::JSON_MSDATE_MATCH, function ($date) { list(, $timestamp, , ) = $date; return date('d.m.Y H:i:s', $timestamp); })->on(Serializers\Events::JSON_BIGINT, function ($json) { # -- don't do anything return $json; }); $compared = $this->arraysAreEqual(array('foo' => 'bar', 'small' => 123456, 'large' => 2.0000000000001E+20, 'text' => 'Example ratio 1000000000000000:1', 'date' => '05.03.2015 12:52:57'), $json->toArray()); $this->assertEquals($compared, true); }
/** * Method for converting JSON to PHP array or object data type * * @param bool $toArray - force assoc array * * @return null|array|object * * @throws RuntimeException * * @since 1.0 */ protected function toPrimitive($toArray = false) { if (!$this->isValid()) { throw new RuntimeException('Provided XML is not valid.'); } $xml = new SimpleXMLElement($this->data, LIBXML_NOCDATA); $json = Encode::toJson($xml)->on(Events::JSON_SERIALIZE, array($this, 'serialize')); $method = $toArray === true ? 'toArray' : 'toObject'; return Decode::json($json->load())->{$method}(); }