From 436db1f8e78c9fd8136c2f687b93b7aa5b3accee Mon Sep 17 00:00:00 2001 From: William Cheng Date: Wed, 17 Jun 2015 19:00:29 +0800 Subject: [PATCH] add __toString and update php sample --- .../swagger-codegen/src/main/resources/php/model.mustache | 8 ++++++++ .../lib/{APIClient.php => ApiClient.php} | 0 .../php/SwaggerClient-php/lib/models/Category.php | 8 ++++++++ .../petstore/php/SwaggerClient-php/lib/models/Order.php | 8 ++++++++ .../petstore/php/SwaggerClient-php/lib/models/Pet.php | 8 ++++++++ .../petstore/php/SwaggerClient-php/lib/models/Tag.php | 8 ++++++++ .../petstore/php/SwaggerClient-php/lib/models/User.php | 8 ++++++++ .../petstore/php/SwaggerClient-php/tests/PetApiTest.php | 3 +++ samples/client/petstore/php/test.php | 3 ++- 9 files changed, 53 insertions(+), 1 deletion(-) rename samples/client/petstore/php/SwaggerClient-php/lib/{APIClient.php => ApiClient.php} (100%) diff --git a/modules/swagger-codegen/src/main/resources/php/model.mustache b/modules/swagger-codegen/src/main/resources/php/model.mustache index 9ce348ad131..e2b98aa23ce 100644 --- a/modules/swagger-codegen/src/main/resources/php/model.mustache +++ b/modules/swagger-codegen/src/main/resources/php/model.mustache @@ -65,6 +65,14 @@ class {{classname}} implements ArrayAccess { public function offsetUnset($offset) { unset($this->$offset); } + + public function __toString() { + if (defined('JSON_PRETTY_PRINT')) { + return json_encode($this, JSON_PRETTY_PRINT); + } else { + return json_encode($this); + } + } } {{/model}} {{/models}} diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/APIClient.php b/samples/client/petstore/php/SwaggerClient-php/lib/ApiClient.php similarity index 100% rename from samples/client/petstore/php/SwaggerClient-php/lib/APIClient.php rename to samples/client/petstore/php/SwaggerClient-php/lib/ApiClient.php diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/models/Category.php b/samples/client/petstore/php/SwaggerClient-php/lib/models/Category.php index 7512921b898..c49c711fa8e 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/models/Category.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/models/Category.php @@ -61,4 +61,12 @@ class Category implements ArrayAccess { public function offsetUnset($offset) { unset($this->$offset); } + + public function __toString() { + if (defined('JSON_PRETTY_PRINT')) { + return json_encode($this, JSON_PRETTY_PRINT); + } else { + return json_encode($this); + } + } } diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/models/Order.php b/samples/client/petstore/php/SwaggerClient-php/lib/models/Order.php index 4314d0df6f3..bf8a0178e4c 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/models/Order.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/models/Order.php @@ -80,4 +80,12 @@ class Order implements ArrayAccess { public function offsetUnset($offset) { unset($this->$offset); } + + public function __toString() { + if (defined('JSON_PRETTY_PRINT')) { + return json_encode($this, JSON_PRETTY_PRINT); + } else { + return json_encode($this); + } + } } diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/models/Pet.php b/samples/client/petstore/php/SwaggerClient-php/lib/models/Pet.php index f74c9a88315..2009cc373cf 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/models/Pet.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/models/Pet.php @@ -80,4 +80,12 @@ class Pet implements ArrayAccess { public function offsetUnset($offset) { unset($this->$offset); } + + public function __toString() { + if (defined('JSON_PRETTY_PRINT')) { + return json_encode($this, JSON_PRETTY_PRINT); + } else { + return json_encode($this); + } + } } diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/models/Tag.php b/samples/client/petstore/php/SwaggerClient-php/lib/models/Tag.php index 309d4087088..37729c68d9e 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/models/Tag.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/models/Tag.php @@ -61,4 +61,12 @@ class Tag implements ArrayAccess { public function offsetUnset($offset) { unset($this->$offset); } + + public function __toString() { + if (defined('JSON_PRETTY_PRINT')) { + return json_encode($this, JSON_PRETTY_PRINT); + } else { + return json_encode($this); + } + } } diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/models/User.php b/samples/client/petstore/php/SwaggerClient-php/lib/models/User.php index bbdaa1082b4..0ec53c409e6 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/models/User.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/models/User.php @@ -88,4 +88,12 @@ class User implements ArrayAccess { public function offsetUnset($offset) { unset($this->$offset); } + + public function __toString() { + if (defined('JSON_PRETTY_PRINT')) { + return json_encode($this, JSON_PRETTY_PRINT); + } else { + return json_encode($this); + } + } } diff --git a/samples/client/petstore/php/SwaggerClient-php/tests/PetApiTest.php b/samples/client/petstore/php/SwaggerClient-php/tests/PetApiTest.php index 69536879d7d..5131fb7649c 100644 --- a/samples/client/petstore/php/SwaggerClient-php/tests/PetApiTest.php +++ b/samples/client/petstore/php/SwaggerClient-php/tests/PetApiTest.php @@ -10,6 +10,9 @@ class PetApiTest extends \PHPUnit_Framework_TestCase // for error reporting (need to run with php5.3 to get no warning) //ini_set('display_errors', 1); //error_reporting(~0); + ini_set('display_startup_errors',1); + ini_set('display_errors',1); + error_reporting(-1); // enable debugging //SwaggerClient\Configuration::$debug = true; diff --git a/samples/client/petstore/php/test.php b/samples/client/petstore/php/test.php index efb6ccaf8f5..0c52f0fdb8a 100644 --- a/samples/client/petstore/php/test.php +++ b/samples/client/petstore/php/test.php @@ -23,7 +23,8 @@ try { $pet_api->getApiClient()->addDefaultHeader("TEST_API_KEY", "09182sdkanafndsl903"); // return Pet (model) $response = $pet_api->getPetById($petId); - var_dump($response); + // to test __toString() + print ($response); // add pet (post json) $new_pet_id = 10005;