Skip to content

Commit

Permalink
CustomerHydrator, improved Resource::create
Browse files Browse the repository at this point in the history
  • Loading branch information
jguittard committed Jun 26, 2015
1 parent 710355e commit 338d565
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 11 deletions.
19 changes: 18 additions & 1 deletion Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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',
]
];
}


}
18 changes: 18 additions & 0 deletions src/Hydrator/CustomerHydrator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Zoho\Subscriptions\Hydrator;

use Zend\Stdlib\Hydrator\ClassMethods as ClassMethodsHydrator;
use Zoho\Subscriptions\Hydrator\Strategy\AddressStrategy;

class CustomerHydrator extends ClassMethodsHydrator
{
public function __construct()
{
parent::__construct();

$addressStrategy = new AddressStrategy();
$this->addStrategy('billing_address', $addressStrategy);
$this->addStrategy('shipping_address', $addressStrategy);
}
}
31 changes: 21 additions & 10 deletions src/Service/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);

}

Expand Down

0 comments on commit 338d565

Please sign in to comment.