Skip to content

Commit 69bbb47

Browse files
committed
Merge branch 'feature/issue-13' into develop
2 parents e589ca1 + 66966c8 commit 69bbb47

File tree

10 files changed

+75
-56
lines changed

10 files changed

+75
-56
lines changed

.php_cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ return PhpCsFixer\Config::create()
99
->setUsingCache(false)
1010
->setRules(array(
1111
'@PSR2' => true,
12+
'array_syntax' => [
13+
'syntax' => 'short',
14+
],
1215
'binary_operator_spaces' => true,
1316
'no_whitespace_in_blank_line' => true,
1417
'ternary_operator_spaces' => true,

.travis.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
language: php
22

33
sudo: false
4-
dist: trusty
54

65
matrix:
76
include:
8-
- php: 5.3
9-
dist: precise
107
- php: 5.4
118
- php: 5.5
129
- php: 5.6

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,11 @@ Here are the methods that must be implemented and why:
9393
### AbstractStructBase
9494
#### Description
9595
This class is the base class for any ```StructType``` class generated by [PackageGenerator](https://github.com/WsdlToPhp/PackageGenerator). It implements our [StructInterface](#structinterface) interface.
96-
It defines three methods:
96+
It defines four methods:
9797
- **__set_state($array)**: Useful when you load the string representation of an object that you stored using ```var_export```. It also allows you to ease the instanciation of an object that contains many properties which would be hard to instanciate using the ```__construct``` method. You can see ```__set_state``` as an hydratation method.
9898
- **_set($name, $value)**: As magic method ```__set``` but used by the ```__set_state``` method. Plus, defining ```__set``` method on used class by the classmap option for the [SoapClient](http://php.net/manual/en/class.soapclient.php) breaks the correct hydratation of your received objects.
99-
- **_get($name)**: As magic method ```__get```. Used by our [AbstractStructArrayBase](#abstractstructarraybase) class
99+
- **_get($name)**: As magic method ```__get```. Used by our [AbstractStructArrayBase](#abstractstructarraybase) class.
100+
- **jsonSerialize()**: by implementing the [\JsonSerializable](http://php.net/manual/en/class.jsonserializable.php) interface, it implements this method that allows to pass the object to the [json_encode](http://php.net/manual/en/function.json-encode.php) method so it will return the properties of the current object in an array.
100101

101102
#### Usage
102103
```php

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"email" : "[email protected]"
2222
},
2323
"require": {
24-
"php" : ">=5.3.3"
24+
"php" : ">=5.4"
2525
},
2626
"require-dev": {
2727
"friendsofphp/php-cs-fixer": "~2.0",

src/AbstractSoapClientBase.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ abstract class AbstractSoapClientBase implements SoapClientInterface
2626
* @param array $wsdlOptions
2727
* @param bool $resetSoapClient allows to disable the SoapClient redefinition
2828
*/
29-
public function __construct(array $wsdlOptions = array(), $resetSoapClient = true)
29+
public function __construct(array $wsdlOptions = [], $resetSoapClient = true)
3030
{
31-
$this->setLastError(array());
31+
$this->setLastError([]);
3232
/**
3333
* Init soap Client
3434
* Set default values
@@ -65,7 +65,7 @@ public static function setSoapClient(\SoapClient $soapClient)
6565
*/
6666
public function initSoapClient(array $options)
6767
{
68-
$wsdlOptions = array();
68+
$wsdlOptions = [];
6969
$defaultWsdlOptions = static::getDefaultWsdlOptions();
7070
foreach ($defaultWsdlOptions as $optionName => $optionValue) {
7171
if (array_key_exists($optionName, $options) && !empty($options[$optionName])) {
@@ -131,7 +131,7 @@ public function getSoapClientClassName($soapClientClassName = null)
131131
*/
132132
public static function getDefaultWsdlOptions()
133133
{
134-
return array(
134+
return [
135135
self::WSDL_CLASSMAP => null,
136136
self::WSDL_CACHE_WSDL => WSDL_CACHE_NONE,
137137
self::WSDL_COMPRESSION => null,
@@ -156,7 +156,7 @@ public static function getDefaultWsdlOptions()
156156
self::WSDL_PASSPHRASE => null,
157157
self::WSDL_AUTHENTICATION => null,
158158
self::WSDL_SSL_METHOD => null,
159-
);
159+
];
160160
}
161161
/**
162162
* Allows to set the SoapClient location to call
@@ -270,7 +270,7 @@ public static function getFormatedXml($string, $asDomDocument = false)
270270
public static function convertStringHeadersToArray($headers)
271271
{
272272
$lines = explode("\r\n", $headers);
273-
$headers = array();
273+
$headers = [];
274274
foreach ($lines as $line) {
275275
if (strpos($line, ':')) {
276276
$headerParts = explode(':', $line);
@@ -294,7 +294,7 @@ public static function convertStringHeadersToArray($headers)
294294
public function setSoapHeader($nameSpace, $name, $data, $mustUnderstand = false, $actor = null)
295295
{
296296
if (static::getSoapClient()) {
297-
$defaultHeaders = (isset(static::getSoapClient()->__default_headers) && is_array(static::getSoapClient()->__default_headers)) ? static::getSoapClient()->__default_headers : array();
297+
$defaultHeaders = (isset(static::getSoapClient()->__default_headers) && is_array(static::getSoapClient()->__default_headers)) ? static::getSoapClient()->__default_headers : [];
298298
foreach ($defaultHeaders as $index => $soapHeader) {
299299
if ($soapHeader->name === $name) {
300300
unset($defaultHeaders[$index]);
@@ -326,13 +326,13 @@ public function setHttpHeader($headerName, $headerValue)
326326
if (static::getSoapClient() && !empty($headerName)) {
327327
$streamContext = $this->getStreamContext();
328328
if ($streamContext === null) {
329-
$options = array();
330-
$options['http'] = array();
329+
$options = [];
330+
$options['http'] = [];
331331
$options['http']['header'] = '';
332332
} else {
333333
$options = stream_context_get_options($streamContext);
334334
if (!array_key_exists('http', $options) || !is_array($options['http'])) {
335-
$options['http'] = array();
335+
$options['http'] = [];
336336
$options['http']['header'] = '';
337337
} elseif (!array_key_exists('header', $options['http'])) {
338338
$options['http']['header'] = '';
@@ -343,7 +343,7 @@ public function setHttpHeader($headerName, $headerValue)
343343
/**
344344
* Ensure there is only one header entry for this header name
345345
*/
346-
$newLines = array();
346+
$newLines = [];
347347
foreach ($lines as $line) {
348348
if (!empty($line) && strpos($line, $headerName) === false) {
349349
array_push($newLines, $line);
@@ -386,7 +386,7 @@ public function getStreamContext()
386386
*/
387387
public function getStreamContextOptions()
388388
{
389-
$options = array();
389+
$options = [];
390390
$context = $this->getStreamContext();
391391
if ($context !== null) {
392392
$options = stream_context_get_options($context);

src/AbstractStructArrayBase.php

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ abstract class AbstractStructArrayBase extends AbstractStructBase implements Str
2121
protected $internArrayOffset;
2222
/**
2323
* Method alias to count
24-
* @uses ApiWsdlClass::count()
24+
* @uses AbstractStructArrayBase::count()
2525
* @return int
2626
*/
2727
public function length()
@@ -30,8 +30,8 @@ public function length()
3030
}
3131
/**
3232
* Method returning item length, alias to length
33-
* @uses ApiWsdlClass::getInternArray()
34-
* @uses ApiWsdlClass::getInternArrayIsArray()
33+
* @uses AbstractStructArrayBase::getInternArray()
34+
* @uses AbstractStructArrayBase::getInternArrayIsArray()
3535
* @return int
3636
*/
3737
public function count()
@@ -40,7 +40,7 @@ public function count()
4040
}
4141
/**
4242
* Method returning the current element
43-
* @uses ApiWsdlClass::offsetGet()
43+
* @uses AbstractStructArrayBase::offsetGet()
4444
* @return mixed
4545
*/
4646
public function current()
@@ -49,8 +49,8 @@ public function current()
4949
}
5050
/**
5151
* Method moving the current position to the next element
52-
* @uses ApiWsdlClass::getInternArrayOffset()
53-
* @uses ApiWsdlClass::setInternArrayOffset()
52+
* @uses AbstractStructArrayBase::getInternArrayOffset()
53+
* @uses AbstractStructArrayBase::setInternArrayOffset()
5454
* @return AbstractStructArrayBase
5555
*/
5656
public function next()
@@ -59,7 +59,7 @@ public function next()
5959
}
6060
/**
6161
* Method resetting itemOffset
62-
* @uses ApiWsdlClass::setInternArrayOffset()
62+
* @uses AbstractStructArrayBase::setInternArrayOffset()
6363
* @return int
6464
*/
6565
public function rewind()
@@ -68,8 +68,8 @@ public function rewind()
6868
}
6969
/**
7070
* Method checking if current itemOffset points to an existing item
71-
* @uses ApiWsdlClass::getInternArrayOffset()
72-
* @uses ApiWsdlClass::offsetExists()
71+
* @uses AbstractStructArrayBase::getInternArrayOffset()
72+
* @uses AbstractStructArrayBase::offsetExists()
7373
* @return bool
7474
*/
7575
public function valid()
@@ -78,7 +78,7 @@ public function valid()
7878
}
7979
/**
8080
* Method returning current itemOffset value, alias to getInternArrayOffset
81-
* @uses ApiWsdlClass::getInternArrayOffset()
81+
* @uses AbstractStructArrayBase::getInternArrayOffset()
8282
* @return int
8383
*/
8484
public function key()
@@ -87,8 +87,8 @@ public function key()
8787
}
8888
/**
8989
* Method alias to offsetGet
90-
* @see ApiWsdlClass::offsetGet()
91-
* @uses ApiWsdlClass::offsetGet()
90+
* @see AbstractStructArrayBase::offsetGet()
91+
* @uses AbstractStructArrayBase::offsetGet()
9292
* @param int $index
9393
* @return mixed
9494
*/
@@ -98,13 +98,13 @@ public function item($index)
9898
}
9999
/**
100100
* Default method adding item to array
101-
* @uses ApiWsdlClass::getAttributeName()
102-
* @uses ApiWsdlClass::__toString()
103-
* @uses ApiWsdlClass::_set()
104-
* @uses ApiWsdlClass::_get()
105-
* @uses ApiWsdlClass::setInternArray()
106-
* @uses ApiWsdlClass::setInternArrayIsArray()
107-
* @uses ApiWsdlClass::setInternArrayOffset()
101+
* @uses AbstractStructArrayBase::getAttributeName()
102+
* @uses AbstractStructArrayBase::__toString()
103+
* @uses AbstractStructArrayBase::_set()
104+
* @uses AbstractStructArrayBase::_get()
105+
* @uses AbstractStructArrayBase::setInternArray()
106+
* @uses AbstractStructArrayBase::setInternArrayIsArray()
107+
* @uses AbstractStructArrayBase::setInternArrayOffset()
108108
* @param mixed $item value
109109
* @return AbstractStructArrayBase
110110
*/
@@ -115,7 +115,7 @@ public function add($item)
115115
* init array
116116
*/
117117
if (!is_array($this->_get($this->getAttributeName()))) {
118-
$this->_set($this->getAttributeName(), array());
118+
$this->_set($this->getAttributeName(), []);
119119
}
120120
/**
121121
* current array
@@ -132,7 +132,7 @@ public function add($item)
132132
}
133133
/**
134134
* Method returning the first item
135-
* @uses ApiWsdlClass::item()
135+
* @uses AbstractStructArrayBase::item()
136136
* @return mixed
137137
*/
138138
public function first()
@@ -141,8 +141,8 @@ public function first()
141141
}
142142
/**
143143
* Method returning the last item
144-
* @uses ApiWsdlClass::item()
145-
* @uses ApiWsdlClass::length()
144+
* @uses AbstractStructArrayBase::item()
145+
* @uses AbstractStructArrayBase::length()
146146
* @return mixed
147147
*/
148148
public function last()
@@ -151,8 +151,8 @@ public function last()
151151
}
152152
/**
153153
* Method testing index in item
154-
* @uses ApiWsdlClass::getInternArrayIsArray()
155-
* @uses ApiWsdlClass::getInternArray()
154+
* @uses AbstractStructArrayBase::getInternArrayIsArray()
155+
* @uses AbstractStructArrayBase::getInternArray()
156156
* @param int $offset
157157
* @return bool
158158
*/
@@ -162,7 +162,7 @@ public function offsetExists($offset)
162162
}
163163
/**
164164
* Method returning the item at "index" value
165-
* @uses ApiWsdlClass::offsetExists()
165+
* @uses AbstractStructArrayBase::offsetExists()
166166
* @param int $offset
167167
* @return mixed
168168
*/
@@ -222,17 +222,17 @@ public function getInternArrayOffset()
222222
}
223223
/**
224224
* Method initiating internArray
225-
* @uses ApiWsdlClass::setInternArray()
226-
* @uses ApiWsdlClass::setInternArrayOffset()
227-
* @uses ApiWsdlClass::setInternArrayIsArray()
228-
* @uses ApiWsdlClass::getAttributeName()
229-
* @uses ApiWsdlClass::initInternArray()
230-
* @uses ApiWsdlClass::__toString()
225+
* @uses AbstractStructArrayBase::setInternArray()
226+
* @uses AbstractStructArrayBase::setInternArrayOffset()
227+
* @uses AbstractStructArrayBase::setInternArrayIsArray()
228+
* @uses AbstractStructArrayBase::getAttributeName()
229+
* @uses AbstractStructArrayBase::initInternArray()
230+
* @uses AbstractStructArrayBase::__toString()
231231
* @param array $array the array to iterate trough
232232
* @param bool $internCall indicates that methods is calling itself
233233
* @return AbstractStructArrayBase
234234
*/
235-
public function initInternArray($array = array(), $internCall = false)
235+
public function initInternArray($array = [], $internCall = false)
236236
{
237237
if (stripos(get_called_class(), 'array') !== false) {
238238
if (is_array($array) && count($array) > 0) {

src/AbstractStructBase.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,20 @@
22

33
namespace WsdlToPhp\PackageBase;
44

5-
abstract class AbstractStructBase implements StructInterface
5+
abstract class AbstractStructBase implements StructInterface, \JsonSerializable
66
{
7+
/**
8+
* Returns the properties of this object
9+
* @return mixed[]
10+
*/
11+
public function jsonSerialize()
12+
{
13+
return \get_object_vars($this);
14+
}
715
/**
816
* Generic method called when an object has been exported with var_export() functions
917
* It allows to return an object instantiated with the values
10-
* @uses ApiWsdlClass::_set()
18+
* @uses AbstractStructBase::_set()
1119
* @param array $array the exported values
1220
* @return Struct
1321
*/

src/SoapClientInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ interface SoapClientInterface
144144
* @param array $wsdlOptions
145145
* @param bool $resetSoapClient allows to disable the SoapClient redefinition
146146
*/
147-
public function __construct(array $wsdlOptions = array(), $resetSoapClient = true);
147+
public function __construct(array $wsdlOptions = [], $resetSoapClient = true);
148148
/**
149149
* Static method getting current SoapClient
150150
* @return \SoapClient

src/StructInterface.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ interface StructInterface
77
/**
88
* Generic method called when an object has been exported with var_export() functions
99
* It allows to return an object instantiated with the values
10-
* @uses ApiWsdlClass::_set()
1110
* @param array $array the exported values
12-
* @return Struct
11+
* @return StructInterface
1312
*/
1413
public static function __set_state(array $array);
1514
}

tests/StructBaseTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,15 @@ public function testSetGetWithException()
5151
->setFoo('bar');
5252
$object->_get('sample');
5353
}
54+
public function testJsonSerialize()
55+
{
56+
$object = new StructObject();
57+
$object
58+
->setBar('foo')
59+
->setFoo('bar');
60+
$this->assertSame([
61+
'foo' => 'bar',
62+
'bar' => 'foo',
63+
], $object->jsonSerialize());
64+
}
5465
}

0 commit comments

Comments
 (0)