Tokens.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. namespace Gogs\API\Request {
  3. /**
  4. * Collection of tokens for a given user.
  5. *
  6. * Supports:
  7. * * GET `/users/{username}/tokens`
  8. *
  9. * @author Joachim M. Giaever (joachim[]giaever.org)
  10. * @version 0.1.1
  11. */
  12. final class Tokens extends Collection {
  13. const VERSION = "0.1.1";
  14. private $owner;
  15. /**
  16. * Initialize a token collection.
  17. *
  18. * @see Base
  19. * @param User $user Owner of tokens
  20. */
  21. public function __construct(string $api_url, string $password, User $user) {
  22. parent::__construct($api_url, $password);
  23. if (!$user->email)
  24. $user->load();
  25. $this->basic($user->email);
  26. $this->owner = $user;
  27. }
  28. /**
  29. * @see Base
  30. */
  31. protected function set_scope(string $method) {
  32. switch ($method) {
  33. case "load":
  34. $this->scope = sprintf("/users/%s/tokens", $this->owner->username);
  35. return true;
  36. }
  37. return false;
  38. }
  39. /**
  40. * Create a new token.
  41. *
  42. * Returns a new token object. If arguments is specified the "token"
  43. * will be created.
  44. *
  45. * Arguments can be left empty to "create" the token, leaving
  46. * the programmer to call create on the token object with the arguments
  47. * itself, to create it.
  48. *
  49. * Creating the token through this function will store the function in
  50. * the collection. If not created, it wont be added, and collection
  51. * must be reloaded (`->load(true)`) to add it.
  52. *
  53. * @see Token
  54. * @return Token
  55. */
  56. public function create(...$args) {
  57. $token = new Token($this->url, $this->token, $this->owner);
  58. if (count($args) != 0) {
  59. $token->create(...$args);
  60. $this->add($token, $token->name);
  61. }
  62. return $token;
  63. }
  64. /**
  65. * @see Collection
  66. */
  67. protected function add_object(\stdClass $obj) {
  68. $token = new Token($this->url, $this->token, $this->owner);
  69. $token->json_set_property($obj);
  70. return $this->add($token, $token->name);
  71. }
  72. /**
  73. * Return a token by name
  74. *
  75. * @return Token
  76. */
  77. public function get(string $s) {
  78. if ($token = $this->by_key($s))
  79. return $token;
  80. $token = (new Token($this->url, $this->token, $this->owner))->load();
  81. $this->add($token, $token->name);
  82. return $token;
  83. }
  84. /**
  85. * Search for a token.
  86. *
  87. * Params can be an array of
  88. * ```php
  89. * $orgs->search(array(
  90. * "name" => "name", // alt. "q". required
  91. * "limit" => 10, // not required, default: 10
  92. * ));
  93. * ```
  94. * By now, this method can be intensive, as it will load
  95. * every token and then do a match on each entry.
  96. *
  97. * @see Base
  98. * @see Collection
  99. * @throws Exception\SearchParamException on missing parameters
  100. * @return Token
  101. */
  102. public function search(array $params = array(), bool $strict = false) {
  103. if (!isset($params["name"]) && !isset($params["q"]))
  104. throw new Exception\SearchParamException("Missing parameter <name|q>");
  105. if (!isset($params["name"])) {
  106. $params["name"] = $params["q"];
  107. unset($params["q"]);
  108. }
  109. if (!isset($params["limit"]))
  110. $params["limit"];
  111. if (!$this->loaded)
  112. $this->load();
  113. $tokens = new Tokens($this->url, $this->token, $this->owner);
  114. foreach ($this->all() as $key => $token) {
  115. if ($token->search(array("name" => $params["name"]), $strict))
  116. $tokens->add($token, $token->name);
  117. if ($tokens->len() == $params["limit"])
  118. break;
  119. }
  120. return $tokens;
  121. }
  122. /**
  123. * @see Collection
  124. */
  125. public function sort_by(int $flag = Collection::SORT_INDEX) {
  126. return $this->sort("ksort");
  127. }
  128. }
  129. }