Skip to content

Commit

Permalink
add toArray api to access token
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Rodriguez committed Jan 5, 2022
1 parent 77def43 commit c28e518
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/Token/AccessToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*
* @link http://tools.ietf.org/html/rfc6749#section-1.4 Access Token (RFC 6749, §1.4)
*/
class AccessToken implements AccessTokenInterface, ResourceOwnerAccessTokenInterface
class AccessToken implements AccessTokenInterface, ResourceOwnerAccessTokenInterface, ArrayableAccessTokenInterface
{
/**
* @var string
Expand Down Expand Up @@ -214,11 +214,11 @@ public function __toString()
{
return (string) $this->getToken();
}

/**
* @inheritdoc
*/
public function jsonSerialize()
public function toArray()
{
$parameters = $this->values;

Expand All @@ -240,4 +240,12 @@ public function jsonSerialize()

return $parameters;
}

/**
* @inheritdoc
*/
public function jsonSerialize()
{
return $this->toArray();
}
}
16 changes: 16 additions & 0 deletions src/Token/ArrayableAccessTokenInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace League\OAuth2\Client\Token;

use ReturnTypeWillChange;

interface ArrayableAccessTokenInterface
{
/**
* Returns an array of parameters provided to the access token
*
* @return array
*/
#[ReturnTypeWillChange]
public function toArray();
}
17 changes: 17 additions & 0 deletions test/src/Token/AccessTokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,4 +227,21 @@ public function testValues()

self::tearDownForBackwardsCompatibility();
}

public function testToArray()
{
$options = [
'access_token' => 'mock_access_token',
'refresh_token' => 'mock_refresh_token',
'expires' => time(),
'resource_owner_id' => 'mock_resource_owner_id',
'custom_thing' => 'i am a test!',
];

$token = $this->getAccessToken($options);

$this->assertSame($token->toArray(), $token->jsonSerialize());

self::tearDownForBackwardsCompatibility();
}
}

0 comments on commit c28e518

Please sign in to comment.