From 338d56548e10ac726edd57936a39085851525c30 Mon Sep 17 00:00:00 2001 From: Julien Guittard Date: Sat, 27 Jun 2015 01:11:58 +0200 Subject: [PATCH] CustomerHydrator, improved Resource::create --- Module.php | 19 ++++++++++++++++++- src/Hydrator/CustomerHydrator.php | 18 ++++++++++++++++++ src/Service/Resource.php | 31 +++++++++++++++++++++---------- 3 files changed, 57 insertions(+), 11 deletions(-) create mode 100644 src/Hydrator/CustomerHydrator.php diff --git a/Module.php b/Module.php index c0643a8..b5afdd5 100644 --- a/Module.php +++ b/Module.php @@ -8,13 +8,14 @@ use Zend\ModuleManager\Feature\AutoloaderProviderInterface; use Zend\ModuleManager\Feature\ConfigProviderInterface; +use Zend\ModuleManager\Feature\HydratorProviderInterface; /** * Class Module * * @package Zoho\Subscriptions */ -class Module implements AutoloaderProviderInterface, ConfigProviderInterface +class Module implements AutoloaderProviderInterface, ConfigProviderInterface, HydratorProviderInterface { /** * Return an array for passing to Zend\Loader\AutoloaderFactory. @@ -42,4 +43,20 @@ public function getConfig() return include __DIR__ . '/config/module.config.php'; } + /** + * Expected to return \Zend\ServiceManager\Config object or array to + * seed such an object. + * + * @return array|\Zend\ServiceManager\Config + */ + public function getHydratorConfig() + { + return [ + 'invokables' => [ + 'Zoho\Subscriptions\Hydrator\CustomerHydrator' => 'Zoho\Subscriptions\Hydrator\CustomerHydrator', + ] + ]; + } + + } \ No newline at end of file diff --git a/src/Hydrator/CustomerHydrator.php b/src/Hydrator/CustomerHydrator.php new file mode 100644 index 0000000..4bdc7d5 --- /dev/null +++ b/src/Hydrator/CustomerHydrator.php @@ -0,0 +1,18 @@ +addStrategy('billing_address', $addressStrategy); + $this->addStrategy('shipping_address', $addressStrategy); + } +} \ No newline at end of file diff --git a/src/Service/Resource.php b/src/Service/Resource.php index b87b5e8..09001f8 100644 --- a/src/Service/Resource.php +++ b/src/Service/Resource.php @@ -220,22 +220,33 @@ public function create($data) $data = ArrayUtils::iteratorToArray($data); } - if (!is_array($data)) { - // throw 422 - } + $json = json_encode($data); - $fields = http_build_query($data); + if (false === $json) { + throw new DomainException("Unprocessable entity", 422); + } curl_setopt($this->curl, CURLOPT_URL, self::ZOHO_API_ENDPOINT . $this->getPath()); - curl_setopt($this->curl, CURLOPT_POST, count($data)); - curl_setopt($this->curl, CURLOPT_POSTFIELDS, $fields); + curl_setopt($this->curl, CURLOPT_POST, true); + curl_setopt($this->curl, CURLOPT_POSTFIELDS, $json); - $result = curl_exec($this->curl); - $result = json_decode($result); + + $response = curl_exec($this->curl); + $result = json_decode($response); + $api_response_info = curl_getinfo($this->curl); curl_close($this->curl); - $entityName = $this->getEntityName(); - return $result->$entityName; + if ($api_response_info['http_code'] == 201) { + //print_r($result);exit; + $entityClass = $this->getEntityClass(); + $entityName = $this->getEntityName(); + $entity = new $entityClass; + $result = $result->$entityName; + $data = (array)$result; + $entity = $this->getHydrator()->hydrate($data, $entity); + return $entity; + } + throw new DomainException($result->message, $api_response_info['http_code']); }