Token.php (2704B)
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 use fkooman\OAuth\Common\Scope; 22 23 class Token 24 { 25 /** @var string */ 26 private $clientConfigId; 27 28 /** @var string */ 29 private $userId; 30 31 /** @var Scope | null */ 32 private $scope; 33 34 /** @var int */ 35 private $issueTime; 36 37 public function __construct(array $data) 38 { 39 foreach (array('client_config_id', 'user_id', 'scope', 'issue_time') as $key) { 40 if (!array_key_exists($key, $data)) { 41 throw new TokenException(sprintf("missing field '%s'", $key)); 42 } 43 } 44 $this->setClientConfigId($data['client_config_id']); 45 $this->setUserId($data['user_id']); 46 $this->setScope($data['scope']); 47 $this->setIssueTime($data['issue_time']); 48 } 49 50 public function setClientConfigId($clientConfigId) 51 { 52 if (!is_string($clientConfigId) || 0 >= strlen($clientConfigId)) { 53 throw new TokenException("client_config_id needs to be a non-empty string"); 54 } 55 $this->clientConfigId = $clientConfigId; 56 } 57 58 public function getClientConfigId() 59 { 60 return $this->clientConfigId; 61 } 62 63 public function setUserId($userId) 64 { 65 if (!is_string($userId) || 0 >= strlen($userId)) { 66 throw new TokenException("client_config_id needs to be a non-empty string"); 67 } 68 $this->userId = $userId; 69 } 70 71 public function getUserId() 72 { 73 return $this->userId; 74 } 75 76 public function setScope(Scope $scope) 77 { 78 $this->scope = $scope; 79 } 80 81 public function getScope() 82 { 83 return $this->scope; 84 } 85 86 public function setIssueTime($issueTime) 87 { 88 if (!is_numeric($issueTime) || 0 >= $issueTime) { 89 throw new TokenException("issue_time should be positive integer"); 90 } 91 $this->issueTime = (int) $issueTime; 92 } 93 94 public function getIssueTime() 95 { 96 return $this->issueTime; 97 } 98 }