commit 4569cacd65eefa9728228bc38934fbd30a433411
parent 5b95fa98ba7dc4d56aac97196ddf43191c637465
Author: finwo <finwo@pm.me>
Date: Tue, 19 Apr 2016 01:17:37 +0200
Added data transformer
Diffstat:
4 files changed, 135 insertions(+), 8 deletions(-)
diff --git a/config/config.yml b/config/config.yml
@@ -9,9 +9,9 @@ authors:
routes:
rest_proxy:
type: rest
- path: ^\/(?<resource>[a-z\/]+)(\.(?<extension>[a-z]+))?$
+ path: ^\/(?<resource>[a-z\/]+)(\.(?<format>[a-z]+))?$
method: GET
prefix: /rest
controller: Finwo\RestProxy:Rest:get
defaults:
- extension: json
-\ No newline at end of file
+ format: json
+\ No newline at end of file
diff --git a/src/Finwo/Framework/Application.php b/src/Finwo/Framework/Application.php
@@ -150,8 +150,41 @@ class Application
)
));
- var_dump($answer);
- die();
+ switch($route->getType()) {
+ case 'rest':
+ // Must transform data
+
+ // Check how to transform
+ $format = null;
+ if (isset($route->getParameters()['format'])) {
+ $format = $route->getParameters()['format'];
+ } else {
+ preg_match('/\.(?<format>[a-z]+)\?/i', $route->getRequestUri(), $matches);
+ if (isset($matches['format'])) {
+ $format = $matches['format'];
+ }
+ }
+ if (is_null($format)) {
+ throw new \Exception('Target format not given');
+ }
+
+ switch(strtolower($format)) {
+ case 'json':
+ die(json_encode($answer));
+ case 'xml':
+ $transformer = new XmlTransformer();
+ die($transformer->objToXML($answer));
+ default:
+ throw new \Exception('Target format not supported');
+ die();
+ }
+
+ die('transform');
+ default:
+ // Must return a view
+ die('Not implemented yet');
+ break;
+ }
}
protected function routeToCallable( Route $route )
diff --git a/src/Finwo/Framework/XmlTransformer.php b/src/Finwo/Framework/XmlTransformer.php
@@ -0,0 +1,84 @@
+<?php
+
+/**
+ * See http://www.akchauhan.com/php-class-for-converting-xml-to-object-and-object-to-xml/
+ */
+
+namespace Finwo\Framework;
+
+use XMLWriter;
+
+class XmlTransformer {
+ private $xml;
+
+ // Constructor
+ public function __construct() {
+ $this->xml = new XMLWriter();
+ $this->xml->openMemory();
+ $this->xml->startDocument('1.0');
+ $this->xml->setIndent(true);
+ $this->xml->setIndentString(' ');
+ }
+
+ // Method to convert Object into XML string
+ public function objToXML($obj) {
+
+ if(is_array($obj)) {
+ $obj = json_decode(json_encode($obj));
+ }
+
+ $this->getObject2XML($this->xml, $obj);
+
+ $this->xml->endElement();
+
+ return $this->xml->outputMemory(true);
+ }
+
+ // Method to convert XML string into Object
+ public function xmlToObj($xmlString) {
+ return simplexml_load_string($xmlString);
+ }
+
+ private function getObject2XML(XMLWriter $xml, $data) {
+ foreach($data as $key => $value) {
+ if(is_object($value)) {
+ $xml->startElement($key);
+ $this->getObject2XML($xml, $value);
+ $xml->endElement();
+ continue;
+ }
+ else if(is_array($value)) {
+ $this->getArray2XML($xml, $key, $value);
+ }
+
+ if (is_string($value)) {
+ $xml->writeElement($key, $value);
+ }
+ }
+ }
+
+ private function getArray2XML(XMLWriter $xml, $keyParent, $data) {
+ foreach($data as $key => $value) {
+ if (is_string($value)) {
+ $xml->writeElement($keyParent, $value);
+ continue;
+ }
+
+ if (is_numeric($key)) {
+ $xml->startElement($keyParent);
+ }
+
+ if(is_object($value)) {
+ $this->getObject2XML($xml, $value);
+ }
+ else if(is_array($value)) {
+ $this->getArray2XML($xml, $key, $value);
+ continue;
+ }
+
+ if (is_numeric($key)) {
+ $xml->endElement();
+ }
+ }
+ }
+}
+\ No newline at end of file
diff --git a/src/Finwo/RestProxy/Controller/RestController.php b/src/Finwo/RestProxy/Controller/RestController.php
@@ -9,8 +9,17 @@ class RestController extends BaseController
{
public function getAction($resource = '', $query = array())
{
- var_dump($resource);
- var_dump($query);
- return;
+ return array(
+ 'html' => array(
+ 'head' => array(
+ 'I\'m fine, thank you'
+ ),
+ 'body' => array(
+ 'div' => array(
+ 'How are you doing?'
+ )
+ )
+ )
+ );
}
}
\ No newline at end of file