From 1ad39f6336dd47a67fc49360560a3d0ae90f8089 Mon Sep 17 00:00:00 2001 From: Adam Rodriguez Date: Tue, 25 May 2021 15:47:09 -0700 Subject: [PATCH] get a specified value in the access token --- src/Token/AccessToken.php | 28 +++++++++++++++++++++ test/src/Token/AccessTokenTest.php | 40 ++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/src/Token/AccessToken.php b/src/Token/AccessToken.php index 81533c30..d24770c3 100644 --- a/src/Token/AccessToken.php +++ b/src/Token/AccessToken.php @@ -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"); + } + + 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 */ diff --git a/test/src/Token/AccessTokenTest.php b/test/src/Token/AccessTokenTest.php index 23ad105f..12778241 100644 --- a/test/src/Token/AccessTokenTest.php +++ b/test/src/Token/AccessTokenTest.php @@ -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(); + } }