diff --git a/CHANGELOG.md b/CHANGELOG.md index be309f1..058503a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,14 +7,16 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] ### Added - [Api] Add `getHttpStatusCode` to `ApiClientException` and `ApiServerException`. This method returns the response HTTP status code. +- [Serializer] New component focused only in assist the conversion of PHP types to JSON compatible types. ### Changed -- [BusinessModel] Remove constructors +- [BusinessModel] This component has been replaced by [Serializer] - [Api] Change in `ApiClientException` and `ApiServerException` constructor signature. ### Deprecated ### Removed +- [BusinessModel] This component has been replaced by [Serializer] ### Fixed diff --git a/README.md b/README.md index 199c013..29c23e2 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ This SDK provides a tree of objects for guide you about to craft the checkout mo /* * Merchant model */ -$merchant = new Aplazame\BusinessModel\Merchant(); +$merchant = new stdClass(); $merchant->confirmation_url = "/confirm"; // url that the JS client sent to confirming the order. $merchant->cancel_url = "/cancel"; // url that the customer is sent to if there is an error in the checkout. $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 /* * Article model */ -$article = new Aplazame\BusinessModel\Article(); -$article->id = "89793238462643383279"; // The article ID. -$article->name = "Reloj en oro blanco de 18 quilates y diamantes"; // Article name. -$article->url = "http://shop.example.com/product.html"; // Article url. -$article->image_url = "http://shop.example.com/product_image.png"; // Article image url. -$article->quantity = 2; // Article quantity. -$article->price = Aplazame\BusinessModel\Decimal::fromFloat(4020.00); // Article price (tax is not included). (4,020.00 €) -$article->description = "Movimiento de cuarzo de alta precisión"; // Article description. -$article->tax_rate = Aplazame\BusinessModel\Decimal::fromFloat(21.00); // Article tax rate. (21.00%) -$article->discount = Aplazame\BusinessModel\Decimal::fromFloat(5.00); // The discount amount of the article. (5.00 €) -$article->discount_rate = Aplazame\BusinessModel\Decimal::fromFloat(2.00); // The rate discount of the article. (2.00 %) +$article = new stdClass(); +$article->id = "89793238462643383279"; // The article ID. +$article->name = "Reloj en oro blanco de 18 quilates y diamantes"; // Article name. +$article->url = "http://shop.example.com/product.html"; // Article url. +$article->image_url = "http://shop.example.com/product_image.png"; // Article image url. +$article->quantity = 2; // Article quantity. +$article->price = Aplazame\Serializer\Decimal::fromFloat(4020.00); // Article price (tax is not included). (4,020.00 €) +$article->description = "Movimiento de cuarzo de alta precisión"; // Article description. +$article->tax_rate = Aplazame\Serializer\Decimal::fromFloat(21.00); // Article tax rate. (21.00%) +$article->discount = Aplazame\Serializer\Decimal::fromFloat(5.00); // The discount amount of the article. (5.00 €) +$article->discount_rate = Aplazame\Serializer\Decimal::fromFloat(2.00); // The rate discount of the article. (2.00 %) // ... rest of articles in the shopping cart. @@ -59,21 +59,21 @@ $articles = array( $article, ... ); /* * Order model */ -$order = new Aplazame\BusinessModel\Order(); -$order->id = "28475648233786783165"; // Your order ID. -$order->currency = "EUR"; // Currency code of the order. -$order->tax_rate = Aplazame\BusinessModel\Decimal::fromFloat(21.00); // Order tax rate. (21.00%) -$order->total_amount = Aplazame\BusinessModel\Decimal::fromFloat(4620.00); // Order total amount. (4,620.00 €) -$order->articles = $articles; // Articles in cart. -$order->discount = Aplazame\BusinessModel\Decimal::fromFloat(160.00); // The discount amount of the order. (160.00 €) -$order->discount_rate = Aplazame\BusinessModel\Decimal::fromFloat(2.00); // The rate discount of the order. (2.00 %) -$order->cart_discount = Aplazame\BusinessModel\Decimal::fromFloat(0.50); // The discount amount of the cart. (0.50 €) -$order->cart_discount_rate = Aplazame\BusinessModel\Decimal::fromFloat(3.00); // The rate discount of the cart. (3.00 %) +$order = new stdClass(); +$order->id = "28475648233786783165"; // Your order ID. +$order->currency = "EUR"; // Currency code of the order. +$order->tax_rate = Aplazame\Serializer\Decimal::fromFloat(21.00); // Order tax rate. (21.00%) +$order->total_amount = Aplazame\Serializer\Decimal::fromFloat(4620.00); // Order total amount. (4,620.00 €) +$order->articles = $articles; // Articles in cart. +$order->discount = Aplazame\Serializer\Decimal::fromFloat(160.00); // The discount amount of the order. (160.00 €) +$order->discount_rate = Aplazame\Serializer\Decimal::fromFloat(2.00); // The rate discount of the order. (2.00 %) +$order->cart_discount = Aplazame\Serializer\Decimal::fromFloat(0.50); // The discount amount of the cart. (0.50 €) +$order->cart_discount_rate = Aplazame\Serializer\Decimal::fromFloat(3.00); // The rate discount of the cart. (3.00 %) /* * Customer address model */ -$customerAddress = new Aplazame\BusinessModel\Address(); +$customerAddress = new stdClass(); $customerAddress->first_name = "John"; // Address first name. $customerAddress->last_name = "Coltrane"; // Address last name. $customerAddress->street = "Plaza del Angel nº10"; // Address street. @@ -88,24 +88,24 @@ $customerAddress->address_addition = "Cerca de la plaza Santa Ana"; // Address a /* * Customer model */ -$customer = new Aplazame\BusinessModel\Customer(); -$customer->id = "1618"; // Customer ID. -$customer->email = "dev@aplazame.com"; // The customer email. -$customer->type = Aplazame\BusinessModel\Customer::TYPE_EXISTING; // Customer type. Other options are: TYPE_GUEST and TYPE_NEW. -$customer->gender = Aplazame\BusinessModel\Customer::GENDER_UNKNOWN; // Customer gender. Other options are: GENDER_MALE, GENDER_FEMALE and GENDER_NOT_APPLICABLE. -$customer->first_name = "John"; // Customer first name. -$customer->last_name = "Coltrane"; // Customer last name. -$customer->birthday = DateTime::createFromFormat(DateTime::ISO8601, "1990-08-21T13:56:45+0000"); // Customer birthday. -$customer->language = "es"; // Customer language preferences. -$customer->date_joined = DateTime::createFromFormat(DateTime::ISO8601, "2014-08-21T13:56:45+0000"); // A datetime designating when the customer account was created. -$customer->last_login = DateTime::createFromFormat(DateTime::ISO8601, "2014-08-27T19:57:56+0000"); // A datetime of the customer last login. -$customer->address = $customerAddress; // Customer address. +$customer = new stdClass(); +$customer->id = "1618"; // Customer ID. +$customer->email = "dev@aplazame.com"; // The customer email. +$customer->type = 'e'; // Customer type, the choices are g:guest, n:new, e:existing. +$customer->gender = 0; // Customer gender, the choices are 0: not known, 1: male, 2:female, 3: not applicable. +$customer->first_name = "John"; // Customer first name. +$customer->last_name = "Coltrane"; // Customer last name. +$customer->birthday = Aplazame\Serializer\Date::fromDateTime(new DateTime("1990-08-21 13:56:45")); // Customer birthday. +$customer->language = "es"; // Customer language preferences. +$customer->date_joined = Aplazame\Serializer\Date::fromDateTime(new DateTime("2014-08-21 13:56:45")); // A datetime designating when the customer account was created. +$customer->last_login = Aplazame\Serializer\Date::fromDateTime(new DateTime("2014-08-27 19:57:56")); // A datetime of the customer last login. +$customer->address = $customerAddress; // Customer address. /* * Billing address model */ -$billingAddress = new Aplazame\BusinessModel\Address(); +$billingAddress = new stdClass(); $billingAddress->first_name = "Bill"; // Billing first name. $billingAddress->last_name = "Evans"; // Billing last name. $billingAddress->street = "Calle de Las Huertas 22"; // Billing street. @@ -121,28 +121,28 @@ $billingAddress->address_addition = "Cerca de la pizzería"; // Billing address /* * Shipping info model */ -$shippingInfo = new Aplazame\BusinessModel\Address(); -$shippingInfo->first_name = "Django"; // Shipping first name. -$shippingInfo->last_name = "Reinhard"; // Shipping last name. -$shippingInfo->street = "Plaza del Angel nº10"; // Shipping street. -$shippingInfo->city = "Madrid"; // Shipping city. -$shippingInfo->state = "Madrid"; // Shipping state. -$shippingInfo->country = "ES"; // Shipping country code. -$shippingInfo->postcode = "28012"; // Shipping postcode. -$shippingInfo->name = "Planet Express"; // Shipping name. -$shippingInfo->price = Aplazame\BusinessModel\Decimal::fromFloat(5.00); // Shipping price (tax is not included). (5.00 €) -$shippingInfo->phone = "616123456"; // Shipping phone number. -$shippingInfo->alt_phone = "+34917909930"; // Shipping alternative phone. -$shippingInfo->address_addition = "Cerca de la plaza Santa Ana"; // Shipping address addition. -$shippingInfo->tax_rate = Aplazame\BusinessModel\Decimal::fromFloat(21.00); // Shipping tax rate. (21.00%) -$shippingInfo->discount = Aplazame\BusinessModel\Decimal::fromFloat(1.00); // The discount amount of the shipping. (1.00 €) -$shippingInfo->discount_rate = Aplazame\BusinessModel\Decimal::fromFloat(2.00); // The rate discount of the shipping. (2.00 %) +$shippingInfo = new stdClass(); +$shippingInfo->first_name = "Django"; // Shipping first name. +$shippingInfo->last_name = "Reinhard"; // Shipping last name. +$shippingInfo->street = "Plaza del Angel nº10"; // Shipping street. +$shippingInfo->city = "Madrid"; // Shipping city. +$shippingInfo->state = "Madrid"; // Shipping state. +$shippingInfo->country = "ES"; // Shipping country code. +$shippingInfo->postcode = "28012"; // Shipping postcode. +$shippingInfo->name = "Planet Express"; // Shipping name. +$shippingInfo->price = Aplazame\Serializer\Decimal::fromFloat(5.00); // Shipping price (tax is not included). (5.00 €) +$shippingInfo->phone = "616123456"; // Shipping phone number. +$shippingInfo->alt_phone = "+34917909930"; // Shipping alternative phone. +$shippingInfo->address_addition = "Cerca de la plaza Santa Ana"; // Shipping address addition. +$shippingInfo->tax_rate = Aplazame\Serializer\Decimal::fromFloat(21.00); // Shipping tax rate. (21.00%) +$shippingInfo->discount = Aplazame\Serializer\Decimal::fromFloat(1.00); // The discount amount of the shipping. (1.00 €) +$shippingInfo->discount_rate = Aplazame\Serializer\Decimal::fromFloat(2.00); // The rate discount of the shipping. (2.00 %) /* * Checkout model */ -$checkout = new Aplazame\BusinessModel\Checkout(); +$checkout = new stdClass(); $checkout->toc = true; $checkout->merchant = $merchant; $checkout->order = $order; @@ -154,7 +154,7 @@ $checkout->shipping = $shippingInfo; In your view you will need to put an snippet similar to this one. ```html ``` diff --git a/src/BusinessModel/AbstractModel.php b/src/BusinessModel/AbstractModel.php deleted file mode 100644 index b809ac6..0000000 --- a/src/BusinessModel/AbstractModel.php +++ /dev/null @@ -1,64 +0,0 @@ - &$value) { - if ($value === null) { - unset($model[$field]); - - continue; - } - - $value = $this->jsonSerializeValue($value); - } - - return $model; - } - - /** - * @param mixed $value - * - * @return mixed - * - * @codeCoverageIgnore - */ - private function jsonSerializeValue($value) - { - if ($value instanceof self) { - return $value->jsonSerialize(); - } - - if (is_array($value)) { - foreach ($value as &$nestedValue) { - $nestedValue = $this->jsonSerializeValue($nestedValue); - } - - return $value; - } - - if ($value instanceof DateTime) { - return $value->format(DateTime::ISO8601); - } - - return $value; - } -} diff --git a/src/BusinessModel/Address.php b/src/BusinessModel/Address.php deleted file mode 100644 index 7519785..0000000 --- a/src/BusinessModel/Address.php +++ /dev/null @@ -1,76 +0,0 @@ -format(DateTime::ISO8601)); + } + + /** + * @var null|string + */ + public $value; + + /** + * @param string $value + */ + public function __construct($value) + { + $this->value = $value; + } + + /** + * @return DateTime + */ + public function asDateTime() + { + $dateTime = DateTime::createFromFormat(DateTime::ISO8601, $this->value); + + return $dateTime; + } + + public function jsonSerialize() + { + return $this->value; + } +} diff --git a/src/BusinessModel/Decimal.php b/src/Serializer/Decimal.php similarity index 87% rename from src/BusinessModel/Decimal.php rename to src/Serializer/Decimal.php index 36e07c0..4be961f 100644 --- a/src/BusinessModel/Decimal.php +++ b/src/Serializer/Decimal.php @@ -1,11 +1,11 @@ jsonSerialize(); + } + + if (is_object($value)) { + $value = (array) $value; + } + + if (is_array($value)) { + foreach ($value as &$nestedValue) { + $nestedValue = self::serializeValue($nestedValue); + } + + return $value; + } + + if ($value instanceof DateTime || $value instanceof \DateTimeInterface) { + throw new DomainException('Please wrap your DateTime objects with Aplazame\Serializer\Date::fromDateTime'); + } + + if (is_float($value)) { + throw new DomainException('Please wrap your float values with Aplazame\Serializer\Decimal::fromFloat'); + } + + return $value; + } +} diff --git a/test/BusinessModel/AbstractModelTestCase.php b/test/BusinessModel/AbstractModelTestCase.php deleted file mode 100644 index a01f614..0000000 --- a/test/BusinessModel/AbstractModelTestCase.php +++ /dev/null @@ -1,18 +0,0 @@ -getMockBuilder($class)->disableOriginalConstructor()->getMock(); - $mock->method('jsonSerialize')->willReturn($jsonSerialize); - // @codingStandardsIgnoreEnd - - return $mock; - } -} diff --git a/test/BusinessModel/AddressTest.php b/test/BusinessModel/AddressTest.php deleted file mode 100644 index b8f0bb3..0000000 --- a/test/BusinessModel/AddressTest.php +++ /dev/null @@ -1,41 +0,0 @@ -first_name = 'first name foo'; - $address->last_name = 'last name foo'; - $address->street = 'street foo'; - $address->city = 'city foo'; - $address->state = 'state foo'; - $address->country = 'ES'; - $address->postcode = '10001'; - $address->phone = '0099123456789'; - $address->alt_phone = '+99123456789'; - $address->address_addition = 'address_addition foo'; - - $expected = <<jsonSerialize()); - } -} diff --git a/test/BusinessModel/ArticleTest.php b/test/BusinessModel/ArticleTest.php deleted file mode 100644 index 53c7dbb..0000000 --- a/test/BusinessModel/ArticleTest.php +++ /dev/null @@ -1,41 +0,0 @@ -id = 'id1234'; - $article->name = 'name lorem ipsum'; - $article->url = 'http://url.foo'; - $article->image_url = 'http://url_image.foo'; - $article->quantity = 1; - $article->price = Decimal::fromFloat(2); - $article->description = 'description lorem ipsum'; - $article->tax_rate = Decimal::fromFloat(3); - $article->discount = Decimal::fromFloat(4); - $article->discount_rate = Decimal::fromFloat(5); - - $expected = <<jsonSerialize()); - } -} diff --git a/test/BusinessModel/CheckoutTest.php b/test/BusinessModel/CheckoutTest.php deleted file mode 100644 index 68795f4..0000000 --- a/test/BusinessModel/CheckoutTest.php +++ /dev/null @@ -1,37 +0,0 @@ -toc = true; - $checkout->merchant = $this->mockClassAndJsonSerialize('Aplazame\\BusinessModel\\Merchant', 'Merchant model'); - $checkout->order = $this->mockClassAndJsonSerialize('Aplazame\\BusinessModel\\Order', 'Order model'); - $checkout->customer = $this->mockClassAndJsonSerialize('Aplazame\\BusinessModel\\Customer', 'Customer model'); - $checkout->shipping = $this->mockClassAndJsonSerialize('Aplazame\\BusinessModel\\ShippingInfo', 'ShippingInfo model'); - $checkout->billing = $this->mockClassAndJsonSerialize('Aplazame\\BusinessModel\\Address', 'Address model'); - $checkout->meta = array('foo' => 'baz'); - - $expected = <<jsonSerialize()); - } -} diff --git a/test/BusinessModel/CustomerTest.php b/test/BusinessModel/CustomerTest.php deleted file mode 100644 index 8b76e3a..0000000 --- a/test/BusinessModel/CustomerTest.php +++ /dev/null @@ -1,46 +0,0 @@ -id = 'id1234'; - $customer->email = 'foo@example.com'; - $customer->type = Customer::TYPE_NEW; - $customer->gender = Customer::GENDER_UNKNOWN; - $customer->first_name = 'description lorem ipsum'; - $customer->last_name = 'last name'; - $customer->birthday = new DateTime('2000-12-31 23:59:59', new DateTimeZone('UTC')); - $customer->language = 'de'; - $customer->date_joined = new DateTime('2001-12-31 23:59:59', new DateTimeZone('Europe/Berlin')); - $customer->last_login = new DateTime('2002-12-31 23:59:59', new DateTimeZone('America/Los_Angeles')); - $customer->address = $this->mockClassAndJsonSerialize('Aplazame\\BusinessModel\\Address', 'Address model'); - - $expected = <<jsonSerialize()); - } -} diff --git a/test/BusinessModel/MerchantTest.php b/test/BusinessModel/MerchantTest.php deleted file mode 100644 index f60a824..0000000 --- a/test/BusinessModel/MerchantTest.php +++ /dev/null @@ -1,29 +0,0 @@ -confirmation_url = 'http://confirmation_url.foo'; - $merchant->cancel_url = 'http://cancel_url.foo'; - $merchant->success_url = 'http://success_url.foo'; - $merchant->checkout_url = 'http://checkout_url.foo'; - - $expected = <<jsonSerialize()); - } -} diff --git a/test/BusinessModel/OrderTest.php b/test/BusinessModel/OrderTest.php deleted file mode 100644 index f5908a0..0000000 --- a/test/BusinessModel/OrderTest.php +++ /dev/null @@ -1,43 +0,0 @@ -id = 'id1234'; - $order->currency = 'EUR'; - $order->tax_rate = Decimal::fromFloat(1); - $order->total_amount = Decimal::fromFloat(2); - $order->articles = array( - $this->mockClassAndJsonSerialize('Aplazame\\BusinessModel\\Article', 'Article model'), - ); - $order->discount = Decimal::fromFloat(3); - $order->discount_rate = Decimal::fromFloat(4); - $order->cart_discount = Decimal::fromFloat(5); - $order->cart_discount_rate = Decimal::fromFloat(6); - - $expected = <<jsonSerialize()); - } -} diff --git a/test/BusinessModel/ShippingInfoTest.php b/test/BusinessModel/ShippingInfoTest.php deleted file mode 100644 index dfe2623..0000000 --- a/test/BusinessModel/ShippingInfoTest.php +++ /dev/null @@ -1,51 +0,0 @@ -first_name = 'first name foo'; - $shippingInfo->last_name = 'last name foo'; - $shippingInfo->street = 'street foo'; - $shippingInfo->city = 'city foo'; - $shippingInfo->state = 'state foo'; - $shippingInfo->country = 'ES'; - $shippingInfo->postcode = '10001'; - $shippingInfo->name = 'name foo'; - $shippingInfo->price = Decimal::fromFloat(1); - $shippingInfo->phone = '0099123456789'; - $shippingInfo->alt_phone = '+99123456789'; - $shippingInfo->address_addition = 'address_addition foo'; - $shippingInfo->tax_rate = Decimal::fromFloat(2); - $shippingInfo->discount = Decimal::fromFloat(3); - $shippingInfo->discount_rate = Decimal::fromFloat(4); - - $expected = <<jsonSerialize()); - } -} diff --git a/test/Serializer/DateTest.php b/test/Serializer/DateTest.php new file mode 100644 index 0000000..88c0c6f --- /dev/null +++ b/test/Serializer/DateTest.php @@ -0,0 +1,38 @@ +jsonSerialize()); + } + + /** + * @dataProvider valuesProvider + */ + public function testConversion($dateTime, $string) + { + $date = Date::fromDateTime($dateTime); + + self::assertSame($string, $date->value, 'string value not match'); + self::assertEquals($dateTime, $date->asDateTime(), 'datetime value not match'); + } + + public function valuesProvider() + { + return array( + // Description => [DateTime, string] + '1999-12-31' => array(new \DateTime('1999-12-31'), '1999-12-31T00:00:00+0100'), + '1999-12-31 23:59:59' => array(new \DateTime('1999-12-31 23:59:59'), '1999-12-31T23:59:59+0100'), + ); + } +} diff --git a/test/BusinessModel/DecimalTest.php b/test/Serializer/DecimalTest.php similarity index 86% rename from test/BusinessModel/DecimalTest.php rename to test/Serializer/DecimalTest.php index a238c68..776e0fb 100644 --- a/test/BusinessModel/DecimalTest.php +++ b/test/Serializer/DecimalTest.php @@ -1,11 +1,13 @@ [value, serialized] + 'string' => array('foo', '"foo"'), + '[string]' => array(array('foo'), '["foo"]'), + '{string}' => array((object) array('a' => 'foo'), '{"a":"foo"}'), + 'int' => array(1, 1), + '[int]' => array(array(1), '[1]'), + '{int}' => array((object) array('a' => 1), '{"a":1}'), + // 'float' is omitted as this type is forbidden + 'Decimal' => array($decimal, '123'), + '[Decimal]' => array(array($decimal), '[123]'), + '{Decimal}' => array((object) array('a' => $decimal), '{"a":123}'), + 'true' => array(true, 'true'), + '[true]' => array(array(true), '[true]'), + '{true}' => array((object) array('a' => true), '{"a":true}'), + 'false' => array(false, 'false'), + '[false]' => array(array(false), '[false]'), + '{false}' => array((object) array('a' => false), '{"a":false}'), + // 'DateTime' is omitted as this type is forbidden + 'Date' => array($date, '"2000-01-02T03:04:05+0100"'), + '[Date]' => array(array($date), '["2000-01-02T03:04:05+0100"]'), + '{Date}' => array((object) array('a' => $date), '{"a":"2000-01-02T03:04:05+0100"}'), + ); + } +}