AccessToken.php (2941B)
1 <?php 2 3 /** 4 * This program is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU Lesser General Public License as published by 6 * the Free Software Foundation, either version 3 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU Lesser General Public License for more details. 13 * 14 * You should have received a copy of the GNU Lesser General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 namespace fkooman\OAuth\Client; 19 20 use fkooman\OAuth\Client\Exception\TokenException; 21 22 class AccessToken extends Token 23 { 24 /** access_token VARCHAR(255) NOT NULL */ 25 private $accessToken; 26 27 /** token_type VARCHAR(255) NOT NULL */ 28 private $tokenType; 29 30 /** expires_in INTEGER DEFAULT NULL */ 31 private $expiresIn; 32 33 public function __construct(array $data) 34 { 35 parent::__construct($data); 36 37 foreach (array('token_type', 'access_token') as $key) { 38 if (!array_key_exists($key, $data)) { 39 throw new TokenException(sprintf("missing field '%s'", $key)); 40 } 41 } 42 $this->setAccessToken($data['access_token']); 43 $this->setTokenType($data['token_type']); 44 $expiresIn = array_key_exists('expires_in', $data) ? $data['expires_in'] : null; 45 $this->setExpiresIn($expiresIn); 46 } 47 48 public function setAccessToken($accessToken) 49 { 50 if (!is_string($accessToken) || 0 >= strlen($accessToken)) { 51 throw new TokenException("access_token needs to be a non-empty string"); 52 } 53 $this->accessToken = $accessToken; 54 } 55 56 public function getAccessToken() 57 { 58 return $this->accessToken; 59 } 60 61 public function setTokenType($tokenType) 62 { 63 if (!is_string($tokenType) || 0 >= strlen($tokenType)) { 64 throw new TokenException("token_type needs to be a non-empty string"); 65 } 66 // Google uses "Bearer" instead of "bearer", so we need to lowercase it... 67 if (!in_array(strtolower($tokenType), array("bearer"))) { 68 throw new TokenException(sprintf("unsupported token type '%s'", $tokenType)); 69 } 70 $this->tokenType = $tokenType; 71 } 72 73 public function getTokenType() 74 { 75 return $this->tokenType; 76 } 77 78 public function setExpiresIn($expiresIn) 79 { 80 if (null !== $expiresIn) { 81 if (!is_numeric($expiresIn) || 0 >= $expiresIn) { 82 throw new TokenException("expires_in should be positive integer or null"); 83 } 84 $expiresIn = (int) $expiresIn; 85 } 86 $this->expiresIn = $expiresIn; 87 } 88 89 public function getExpiresIn() 90 { 91 return $this->expiresIn; 92 } 93 }