Tokens.php 4.4 KB

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