Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3802247

Browse files
committedNov 23, 2016
[bc break] BusinessModel removal, Serializer addition
1 parent 5b205bf commit 3802247

25 files changed

+264
-883
lines changed
 

‎CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77
## [Unreleased]
88
### Added
99
- [Api] Add `getHttpStatusCode` to `ApiClientException` and `ApiServerException`. This method returns the response HTTP status code.
10+
- [Serializer] New component focused only in assist the conversion of PHP types to JSON compatible types.
1011

1112
### Changed
12-
- [BusinessModel] Remove constructors
13+
- [BusinessModel] This component has been replaced by [Serializer]
1314
- [Api] Change in `ApiClientException` and `ApiServerException` constructor signature.
1415

1516
### Deprecated
1617

1718
### Removed
19+
- [BusinessModel] This component has been replaced by [Serializer]
1820

1921
### Fixed
2022

‎README.md

Lines changed: 54 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ This SDK provides a tree of objects for guide you about to craft the checkout mo
2626
/*
2727
* Merchant model
2828
*/
29-
$merchant = new Aplazame\BusinessModel\Merchant();
29+
$merchant = new stdClass();
3030
$merchant->confirmation_url = "/confirm"; // url that the JS client sent to confirming the order.
3131
$merchant->cancel_url = "/cancel"; // url that the customer is sent to if there is an error in the checkout.
3232
$merchant->success_url = "/success"; // url that the customer is sent to after confirming their order.
@@ -36,17 +36,17 @@ $merchant->checkout_url = "/checkout"; // url that the customer is sent to if
3636
/*
3737
* Article model
3838
*/
39-
$article = new Aplazame\BusinessModel\Article();
40-
$article->id = "89793238462643383279"; // The article ID.
41-
$article->name = "Reloj en oro blanco de 18 quilates y diamantes"; // Article name.
42-
$article->url = "http://shop.example.com/product.html"; // Article url.
43-
$article->image_url = "http://shop.example.com/product_image.png"; // Article image url.
44-
$article->quantity = 2; // Article quantity.
45-
$article->price = Aplazame\BusinessModel\Decimal::fromFloat(4020.00); // Article price (tax is not included). (4,020.00 €)
46-
$article->description = "Movimiento de cuarzo de alta precisión"; // Article description.
47-
$article->tax_rate = Aplazame\BusinessModel\Decimal::fromFloat(21.00); // Article tax rate. (21.00%)
48-
$article->discount = Aplazame\BusinessModel\Decimal::fromFloat(5.00); // The discount amount of the article. (5.00 €)
49-
$article->discount_rate = Aplazame\BusinessModel\Decimal::fromFloat(2.00); // The rate discount of the article. (2.00 %)
39+
$article = new stdClass();
40+
$article->id = "89793238462643383279"; // The article ID.
41+
$article->name = "Reloj en oro blanco de 18 quilates y diamantes"; // Article name.
42+
$article->url = "http://shop.example.com/product.html"; // Article url.
43+
$article->image_url = "http://shop.example.com/product_image.png"; // Article image url.
44+
$article->quantity = 2; // Article quantity.
45+
$article->price = Aplazame\Serializer\Decimal::fromFloat(4020.00); // Article price (tax is not included). (4,020.00 €)
46+
$article->description = "Movimiento de cuarzo de alta precisión"; // Article description.
47+
$article->tax_rate = Aplazame\Serializer\Decimal::fromFloat(21.00); // Article tax rate. (21.00%)
48+
$article->discount = Aplazame\Serializer\Decimal::fromFloat(5.00); // The discount amount of the article. (5.00 €)
49+
$article->discount_rate = Aplazame\Serializer\Decimal::fromFloat(2.00); // The rate discount of the article. (2.00 %)
5050

5151
// ... rest of articles in the shopping cart.
5252

@@ -59,21 +59,21 @@ $articles = array( $article, ... );
5959
/*
6060
* Order model
6161
*/
62-
$order = new Aplazame\BusinessModel\Order();
63-
$order->id = "28475648233786783165"; // Your order ID.
64-
$order->currency = "EUR"; // Currency code of the order.
65-
$order->tax_rate = Aplazame\BusinessModel\Decimal::fromFloat(21.00); // Order tax rate. (21.00%)
66-
$order->total_amount = Aplazame\BusinessModel\Decimal::fromFloat(4620.00); // Order total amount. (4,620.00 €)
67-
$order->articles = $articles; // Articles in cart.
68-
$order->discount = Aplazame\BusinessModel\Decimal::fromFloat(160.00); // The discount amount of the order. (160.00 €)
69-
$order->discount_rate = Aplazame\BusinessModel\Decimal::fromFloat(2.00); // The rate discount of the order. (2.00 %)
70-
$order->cart_discount = Aplazame\BusinessModel\Decimal::fromFloat(0.50); // The discount amount of the cart. (0.50 €)
71-
$order->cart_discount_rate = Aplazame\BusinessModel\Decimal::fromFloat(3.00); // The rate discount of the cart. (3.00 %)
62+
$order = new stdClass();
63+
$order->id = "28475648233786783165"; // Your order ID.
64+
$order->currency = "EUR"; // Currency code of the order.
65+
$order->tax_rate = Aplazame\Serializer\Decimal::fromFloat(21.00); // Order tax rate. (21.00%)
66+
$order->total_amount = Aplazame\Serializer\Decimal::fromFloat(4620.00); // Order total amount. (4,620.00 €)
67+
$order->articles = $articles; // Articles in cart.
68+
$order->discount = Aplazame\Serializer\Decimal::fromFloat(160.00); // The discount amount of the order. (160.00 €)
69+
$order->discount_rate = Aplazame\Serializer\Decimal::fromFloat(2.00); // The rate discount of the order. (2.00 %)
70+
$order->cart_discount = Aplazame\Serializer\Decimal::fromFloat(0.50); // The discount amount of the cart. (0.50 €)
71+
$order->cart_discount_rate = Aplazame\Serializer\Decimal::fromFloat(3.00); // The rate discount of the cart. (3.00 %)
7272

7373
/*
7474
* Customer address model
7575
*/
76-
$customerAddress = new Aplazame\BusinessModel\Address();
76+
$customerAddress = new stdClass();
7777
$customerAddress->first_name = "John"; // Address first name.
7878
$customerAddress->last_name = "Coltrane"; // Address last name.
7979
$customerAddress->street = "Plaza del Angel nº10"; // Address street.
@@ -88,24 +88,24 @@ $customerAddress->address_addition = "Cerca de la plaza Santa Ana"; // Address a
8888
/*
8989
* Customer model
9090
*/
91-
$customer = new Aplazame\BusinessModel\Customer();
92-
$customer->id = "1618"; // Customer ID.
93-
$customer->email = "dev@aplazame.com"; // The customer email.
94-
$customer->type = Aplazame\BusinessModel\Customer::TYPE_EXISTING; // Customer type. Other options are: TYPE_GUEST and TYPE_NEW.
95-
$customer->gender = Aplazame\BusinessModel\Customer::GENDER_UNKNOWN; // Customer gender. Other options are: GENDER_MALE, GENDER_FEMALE and GENDER_NOT_APPLICABLE.
96-
$customer->first_name = "John"; // Customer first name.
97-
$customer->last_name = "Coltrane"; // Customer last name.
98-
$customer->birthday = DateTime::createFromFormat(DateTime::ISO8601, "1990-08-21T13:56:45+0000"); // Customer birthday.
99-
$customer->language = "es"; // Customer language preferences.
100-
$customer->date_joined = DateTime::createFromFormat(DateTime::ISO8601, "2014-08-21T13:56:45+0000"); // A datetime designating when the customer account was created.
101-
$customer->last_login = DateTime::createFromFormat(DateTime::ISO8601, "2014-08-27T19:57:56+0000"); // A datetime of the customer last login.
102-
$customer->address = $customerAddress; // Customer address.
91+
$customer = new stdClass();
92+
$customer->id = "1618"; // Customer ID.
93+
$customer->email = "dev@aplazame.com"; // The customer email.
94+
$customer->type = 'existing'; // Customer type. Other options are: 'guest' and 'new'.
95+
$customer->gender = 'unknown'; // Customer gender. Other options are: 'male', 'female'and 'gender_not_aplicable'.
96+
$customer->first_name = "John"; // Customer first name.
97+
$customer->last_name = "Coltrane"; // Customer last name.
98+
$customer->birthday = Aplazame\Serializer\Date::fromDateTime(new DateTime("1990-08-21 13:56:45")); // Customer birthday.
99+
$customer->language = "es"; // Customer language preferences.
100+
$customer->date_joined = Aplazame\Serializer\Date::fromDateTime(new DateTime("2014-08-21 13:56:45")); // A datetime designating when the customer account was created.
101+
$customer->last_login = Aplazame\Serializer\Date::fromDateTime(new DateTime("2014-08-27 19:57:56")); // A datetime of the customer last login.
102+
$customer->address = $customerAddress; // Customer address.
103103

104104

105105
/*
106106
* Billing address model
107107
*/
108-
$billingAddress = new Aplazame\BusinessModel\Address();
108+
$billingAddress = new stdClass();
109109
$billingAddress->first_name = "Bill"; // Billing first name.
110110
$billingAddress->last_name = "Evans"; // Billing last name.
111111
$billingAddress->street = "Calle de Las Huertas 22"; // Billing street.
@@ -121,28 +121,28 @@ $billingAddress->address_addition = "Cerca de la pizzería"; // Billing address
121121
/*
122122
* Shipping info model
123123
*/
124-
$shippingInfo = new Aplazame\BusinessModel\Address();
125-
$shippingInfo->first_name = "Django"; // Shipping first name.
126-
$shippingInfo->last_name = "Reinhard"; // Shipping last name.
127-
$shippingInfo->street = "Plaza del Angel nº10"; // Shipping street.
128-
$shippingInfo->city = "Madrid"; // Shipping city.
129-
$shippingInfo->state = "Madrid"; // Shipping state.
130-
$shippingInfo->country = "ES"; // Shipping country code.
131-
$shippingInfo->postcode = "28012"; // Shipping postcode.
132-
$shippingInfo->name = "Planet Express"; // Shipping name.
133-
$shippingInfo->price = Aplazame\BusinessModel\Decimal::fromFloat(5.00); // Shipping price (tax is not included). (5.00 €)
134-
$shippingInfo->phone = "616123456"; // Shipping phone number.
135-
$shippingInfo->alt_phone = "+34917909930"; // Shipping alternative phone.
136-
$shippingInfo->address_addition = "Cerca de la plaza Santa Ana"; // Shipping address addition.
137-
$shippingInfo->tax_rate = Aplazame\BusinessModel\Decimal::fromFloat(21.00); // Shipping tax rate. (21.00%)
138-
$shippingInfo->discount = Aplazame\BusinessModel\Decimal::fromFloat(1.00); // The discount amount of the shipping. (1.00 €)
139-
$shippingInfo->discount_rate = Aplazame\BusinessModel\Decimal::fromFloat(2.00); // The rate discount of the shipping. (2.00 %)
124+
$shippingInfo = new stdClass();
125+
$shippingInfo->first_name = "Django"; // Shipping first name.
126+
$shippingInfo->last_name = "Reinhard"; // Shipping last name.
127+
$shippingInfo->street = "Plaza del Angel nº10"; // Shipping street.
128+
$shippingInfo->city = "Madrid"; // Shipping city.
129+
$shippingInfo->state = "Madrid"; // Shipping state.
130+
$shippingInfo->country = "ES"; // Shipping country code.
131+
$shippingInfo->postcode = "28012"; // Shipping postcode.
132+
$shippingInfo->name = "Planet Express"; // Shipping name.
133+
$shippingInfo->price = Aplazame\Serializer\Decimal::fromFloat(5.00); // Shipping price (tax is not included). (5.00 €)
134+
$shippingInfo->phone = "616123456"; // Shipping phone number.
135+
$shippingInfo->alt_phone = "+34917909930"; // Shipping alternative phone.
136+
$shippingInfo->address_addition = "Cerca de la plaza Santa Ana"; // Shipping address addition.
137+
$shippingInfo->tax_rate = Aplazame\Serializer\Decimal::fromFloat(21.00); // Shipping tax rate. (21.00%)
138+
$shippingInfo->discount = Aplazame\Serializer\Decimal::fromFloat(1.00); // The discount amount of the shipping. (1.00 €)
139+
$shippingInfo->discount_rate = Aplazame\Serializer\Decimal::fromFloat(2.00); // The rate discount of the shipping. (2.00 %)
140140

141141

142142
/*
143143
* Checkout model
144144
*/
145-
$checkout = new Aplazame\BusinessModel\Checkout();
145+
$checkout = new stdClass();
146146
$checkout->toc = true;
147147
$checkout->merchant = $merchant;
148148
$checkout->order = $order;
@@ -154,7 +154,7 @@ $checkout->shipping = $shippingInfo;
154154
In your view you will need to put an snippet similar to this one.
155155
```html
156156
<script>
157-
aplazame.checkout( <?php echo json_encode($checkout->jsonSerialize()); ?> );
157+
aplazame.checkout( <?php echo json_encode(Aplazame\Serializer\JsonSerializer::serializeValue($checkout)); ?> );
158158
</script>
159159
```
160160

‎src/BusinessModel/AbstractModel.php

Lines changed: 0 additions & 64 deletions
This file was deleted.

‎src/BusinessModel/Address.php

Lines changed: 0 additions & 76 deletions
This file was deleted.

‎src/BusinessModel/Article.php

Lines changed: 0 additions & 79 deletions
This file was deleted.

‎src/BusinessModel/Checkout.php

Lines changed: 0 additions & 44 deletions
This file was deleted.

‎src/BusinessModel/Customer.php

Lines changed: 0 additions & 101 deletions
This file was deleted.

‎src/BusinessModel/Merchant.php

Lines changed: 0 additions & 37 deletions
This file was deleted.

‎src/BusinessModel/Order.php

Lines changed: 0 additions & 72 deletions
This file was deleted.

‎src/BusinessModel/ShippingInfo.php

Lines changed: 0 additions & 44 deletions
This file was deleted.

‎src/Serializer/Date.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace Aplazame\Serializer;
4+
5+
use DateTime;
6+
7+
/**
8+
* DateTime Type.
9+
*/
10+
class Date implements JsonSerializable
11+
{
12+
/**
13+
* @param DateTime $value
14+
*
15+
* @return self
16+
*/
17+
public static function fromDateTime($value)
18+
{
19+
return new self($value->format(DateTime::ISO8601));
20+
}
21+
22+
/**
23+
* @var null|string
24+
*/
25+
public $value;
26+
27+
/**
28+
* @param string $value
29+
*/
30+
public function __construct($value)
31+
{
32+
$this->value = $value;
33+
}
34+
35+
/**
36+
* @return DateTime
37+
*/
38+
public function asDateTime()
39+
{
40+
$dateTime = DateTime::createFromFormat(DateTime::ISO8601, $this->value);
41+
42+
return $dateTime;
43+
}
44+
45+
public function jsonSerialize()
46+
{
47+
return $this->value;
48+
}
49+
}

‎src/BusinessModel/Decimal.php renamed to ‎src/Serializer/Decimal.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<?php
22

3-
namespace Aplazame\BusinessModel;
3+
namespace Aplazame\Serializer;
44

55
/**
66
* Decimal Type.
77
*/
8-
class Decimal extends AbstractModel
8+
class Decimal implements JsonSerializable
99
{
1010
public static function fromFloat($value)
1111
{

‎src/Serializer/JsonSerializable.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Aplazame\Serializer;
4+
5+
interface JsonSerializable
6+
{
7+
/**
8+
* Serializes the object to a value that can be serialized natively by json_encode().
9+
*
10+
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php
11+
*
12+
* @return mixed data which can be serialized by json_encode(), which is a value of any type other than a resource.
13+
*/
14+
public function jsonSerialize();
15+
}

‎src/Serializer/JsonSerializer.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace Aplazame\Serializer;
4+
5+
use DateTime;
6+
use DomainException;
7+
8+
/**
9+
* This class assist to compose API models serializable as JSON for PHP versions prior to v5.4
10+
*/
11+
class JsonSerializer
12+
{
13+
/**
14+
* Important: This method does not return a JSON string, the return of this method must be encoded with `json_encode()`
15+
*
16+
* @param mixed $value
17+
*
18+
* @return mixed a value valid for to be used with native `json_encode()` function
19+
*/
20+
public static function serializeValue($value)
21+
{
22+
if ($value instanceof JsonSerializable || $value instanceof \JsonSerializable) {
23+
return $value->jsonSerialize();
24+
}
25+
26+
if (is_object($value)) {
27+
$value = (array) $value;
28+
}
29+
30+
if (is_array($value)) {
31+
foreach ($value as &$nestedValue) {
32+
$nestedValue = self::serializeValue($nestedValue);
33+
}
34+
35+
return $value;
36+
}
37+
38+
if ($value instanceof DateTime || $value instanceof \DateTimeInterface) {
39+
throw new DomainException('Please wrap your DateTime objects with Aplazame\Serializer\Date::fromDateTime');
40+
}
41+
42+
if (is_float($value)) {
43+
throw new DomainException('Please wrap your float values with Aplazame\Serializer\Decimal::fromFloat');
44+
}
45+
46+
return $value;
47+
}
48+
}

‎test/BusinessModel/AbstractModelTestCase.php

Lines changed: 0 additions & 18 deletions
This file was deleted.

‎test/BusinessModel/AddressTest.php

Lines changed: 0 additions & 41 deletions
This file was deleted.

‎test/BusinessModel/ArticleTest.php

Lines changed: 0 additions & 41 deletions
This file was deleted.

‎test/BusinessModel/CheckoutTest.php

Lines changed: 0 additions & 37 deletions
This file was deleted.

‎test/BusinessModel/CustomerTest.php

Lines changed: 0 additions & 46 deletions
This file was deleted.

‎test/BusinessModel/MerchantTest.php

Lines changed: 0 additions & 29 deletions
This file was deleted.

‎test/BusinessModel/OrderTest.php

Lines changed: 0 additions & 43 deletions
This file was deleted.

‎test/BusinessModel/ShippingInfoTest.php

Lines changed: 0 additions & 51 deletions
This file was deleted.

‎test/Serializer/DateTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Aplazame\Serializer;
4+
5+
use PHPUnit_Framework_TestCase as TestCase;
6+
7+
/**
8+
* @covers Aplazame\Serializer\Date
9+
*/
10+
class DateTest extends TestCase
11+
{
12+
public function testJsonSerialize()
13+
{
14+
$date = new Date('123');
15+
16+
self::assertSame('123', $date->jsonSerialize());
17+
}
18+
19+
/**
20+
* @dataProvider valuesProvider
21+
*/
22+
public function testConversion($dateTime, $string)
23+
{
24+
$date = Date::fromDateTime($dateTime);
25+
26+
self::assertSame($string, $date->value, 'string value not match');
27+
self::assertEquals($dateTime, $date->asDateTime(), 'datetime value not match');
28+
}
29+
30+
public function valuesProvider()
31+
{
32+
return array(
33+
// Description => [DateTime, string]
34+
'1999-12-31' => array(new \DateTime('1999-12-31'), '1999-12-31T00:00:00+0100'),
35+
'1999-12-31 23:59:59' => array(new \DateTime('1999-12-31 23:59:59'), '1999-12-31T23:59:59+0100'),
36+
);
37+
}
38+
}

‎test/BusinessModel/DecimalTest.php renamed to ‎test/Serializer/DecimalTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
<?php
22

3-
namespace Aplazame\BusinessModel;
3+
namespace Aplazame\Serializer;
4+
5+
use PHPUnit_Framework_TestCase as TestCase;
46

57
/**
6-
* @covers Aplazame\BusinessModel\Decimal
8+
* @covers Aplazame\Serializer\Decimal
79
*/
8-
class DecimalTest extends AbstractModelTestCase
10+
class DecimalTest extends TestCase
911
{
1012
public function testJsonSerialize()
1113
{
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace Aplazame\Serializer;
4+
5+
use DateTime;
6+
use PHPUnit_Framework_TestCase as TestCase;
7+
8+
/**
9+
* @covers Aplazame\Serializer\Decimal
10+
*/
11+
class JsonSerializerTest extends TestCase
12+
{
13+
/**
14+
* @dataProvider valuesProvider
15+
*/
16+
public function testSerializeValue($value, $serialized)
17+
{
18+
self::assertEquals($serialized, json_encode(JsonSerializer::serializeValue($value)));
19+
}
20+
21+
public function valuesProvider()
22+
{
23+
$date = Date::fromDateTime(new DateTime('2000-01-02 03:04:05'));
24+
$decimal = Decimal::fromFloat(1.23);
25+
26+
return array(
27+
// Description => [value, serialized]
28+
'string' => array('foo', '"foo"'),
29+
'[string]' => array(array('foo'), '["foo"]'),
30+
'{string}' => array((object) array('a' => 'foo'), '{"a":"foo"}'),
31+
'int' => array(1, 1),
32+
'[int]' => array(array(1), '[1]'),
33+
'{int}' => array((object) array('a' => 1), '{"a":1}'),
34+
// 'float' is omitted as this type is forbidden
35+
'Decimal' => array($decimal, '123'),
36+
'[Decimal]' => array(array($decimal), '[123]'),
37+
'{Decimal}' => array((object) array('a' => $decimal), '{"a":123}'),
38+
'true' => array(true, 'true'),
39+
'[true]' => array(array(true), '[true]'),
40+
'{true}' => array((object) array('a' => true), '{"a":true}'),
41+
'false' => array(false, 'false'),
42+
'[false]' => array(array(false), '[false]'),
43+
'{false}' => array((object) array('a' => false), '{"a":false}'),
44+
// 'DateTime' is omitted as this type is forbidden
45+
'Date' => array($date, '"2000-01-02T03:04:05+0100"'),
46+
'[Date]' => array(array($date), '["2000-01-02T03:04:05+0100"]'),
47+
'{Date}' => array((object) array('a' => $date), '{"a":"2000-01-02T03:04:05+0100"}'),
48+
);
49+
}
50+
}

0 commit comments

Comments
 (0)
Please sign in to comment.