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

get a specified value in the access token #888

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
28 changes: 28 additions & 0 deletions src/Token/AccessToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,34 @@ public function getValues()
return $this->values;
}

/**
* Determine if the access token has a value by the offset
* @param string|int $offset The offset to check
* @return bool
*/
public function hasValue($offset)
{
if (!(is_string($offset) || is_numeric($offset))) {
throw new InvalidArgumentException("Values offset must be a string or int");
}
Comment on lines +217 to +219
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the point of this check?


return array_key_exists($offset, $this->values);
}

/**
* Get an access token value by the offset
* @param string|int $offset The offset to find
* @return mixed|null
*/
public function getValue($offset)
{
if ($this->hasValue($offset)) {
return $this->values[$offset];
}

return null;
}

/**
* @inheritdoc
*/
Expand Down
40 changes: 40 additions & 0 deletions test/src/Token/AccessTokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,4 +227,44 @@ public function testValues()

self::tearDownForBackwardsCompatibility();
}

public function testValue()
{
$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);

$value = $token->getValue('custom_thing');
$this->assertSame($options['custom_thing'], $value);

$value = $token->getValue('doesnt_exist');
$this->assertNull($value);

self::tearDownForBackwardsCompatibility();
}

public function testInvalidValueOffsetType()
{
$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->expectException(InvalidArgumentException::class);

$value = $token->getValue([]);

self::tearDownForBackwardsCompatibility();
}
}