Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename fake magic methods and rewrite array conversion #704

Merged
merged 1 commit into from
Aug 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 39 additions & 13 deletions lib/StripeObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ public function refreshFrom($values, $opts, $partial = false)
$this->_originalValues = self::deepCopy($values);

if ($values instanceof StripeObject) {
$values = $values->__toArray(true);
$values = $values->toArray(true);
}

// Wipe old state before setting new. This is useful for e.g. updating a
Expand Down Expand Up @@ -401,27 +401,53 @@ public function serializeParamsValue($value, $original, $unsaved, $force, $key =

public function jsonSerialize()
{
return $this->__toArray(true);
return $this->toArray(true);
}

public function __toJSON()
/**
* Returns an associative array with the key and values composing the
* Stripe object.
*
* @return array The associative array.
*/
public function toArray()
{
return json_encode($this->__toArray(true), JSON_PRETTY_PRINT);
$maybeToArray = function ($value) {
if (is_null($value)) {
return null;
}

return method_exists($value, 'toArray') ? $value->toArray() : $value;
};

return array_reduce(array_keys($this->_values), function ($acc, $k) use ($maybeToArray) {
if ($k[0] == '_') {
return $acc;
}
$v = $this->_values[$k];
if (Util\Util::isList($v)) {
$acc[$k] = array_map($maybeToArray, $v);
} else {
$acc[$k] = $maybeToArray($v);
}
return $acc;
}, []);
}

public function __toString()
/**
* Returns a pretty JSON representation of the Stripe object.
*
* @return string The JSON representation of the Stripe object.
*/
public function toJSON()
{
$class = get_class($this);
return $class . ' JSON: ' . $this->__toJSON();
return json_encode($this->toArray(true), JSON_PRETTY_PRINT);
}

public function __toArray($recursive = false)
public function __toString()
{
if ($recursive) {
return Util\Util::convertStripeObjectToArray($this->_values);
} else {
return $this->_values;
}
$class = get_class($this);
return $class . ' JSON: ' . $this->toJSON();
}

/**
Expand Down
25 changes: 0 additions & 25 deletions lib/Util/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,31 +31,6 @@ public static function isList($array)
return true;
}

/**
* Recursively converts the PHP Stripe object to an array.
*
* @param array $values The PHP Stripe object to convert.
* @return array
*/
public static function convertStripeObjectToArray($values)
{
$results = [];
foreach ($values as $k => $v) {
// FIXME: this is an encapsulation violation
if ($k[0] == '_') {
continue;
}
if ($v instanceof StripeObject) {
$results[$k] = $v->__toArray(true);
} elseif (is_array($v)) {
$results[$k] = self::convertStripeObjectToArray($v);
} else {
$results[$k] = $v;
}
}
return $results;
}

/**
* Converts a response from the Stripe API to the corresponding PHP object.
*
Expand Down
44 changes: 27 additions & 17 deletions tests/Stripe/StripeObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,31 +82,41 @@ public function testValues()

public function testToArray()
{
$s = new StripeObject();
$s->foo = 'a';
$array = [
'foo' => 'a',
'list' => [1, 2, 3],
'null' => null,
];
$s = StripeObject::constructFrom($array);

$converted = $s->__toArray();
$converted = $s->toArray();

$this->assertInternalType('array', $converted);
$this->assertArrayHasKey('foo', $converted);
$this->assertEquals('a', $converted['foo']);
$this->assertEquals($array, $converted);
}

public function testRecursiveToArray()
public function testToArrayRecursive()
{
$s = new StripeObject();
$z = new StripeObject();
// deep nested associative array (when contained in an indexed array)
// or StripeObject
$nestedArray = ['id' => 7, 'foo' => 'bar'];
$nested = StripeObject::constructFrom($nestedArray);

$s->child = $z;
$z->foo = 'a';
$obj = StripeObject::constructFrom([
'id' => 1,
// simple associative array that contains a StripeObject to help us
// test deep recursion
'nested' => ['object' => 'list', 'data' => $nested],
'list' => [$nested],
]);

$converted = $s->__toArray(true);
$expected = [
'id' => 1,
'nested' => ['object' => 'list', 'data' => $nestedArray],
'list' => [$nestedArray],
];

$this->assertInternalType('array', $converted);
$this->assertArrayHasKey('child', $converted);
$this->assertInternalType('array', $converted['child']);
$this->assertArrayHasKey('foo', $converted['child']);
$this->assertEquals('a', $converted['child']['foo']);
$this->assertEquals($expected, $obj->toArray());
}

public function testNonexistentProperty()
Expand Down Expand Up @@ -134,7 +144,7 @@ public function testToString()
$s = new StripeObject();
$s->foo = 'a';

$string = $s->__toString();
$string = (string)$s;
$expected = <<<EOS
Stripe\StripeObject JSON: {
"foo": "a"
Expand Down
2 changes: 1 addition & 1 deletion tests/Stripe/Util/UtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function testConvertStripeObjectToArrayIncludesId()
],
null
);
$this->assertTrue(array_key_exists("id", $customer->__toArray(true)));
$this->assertTrue(array_key_exists("id", $customer->toArray(true)));
}

public function testUtf8()
Expand Down