Token.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. namespace Gogs\API\Request {
  3. /**
  4. * A token related to a user
  5. *
  6. * Supports:
  7. * * POST `/users/{username}/tokens`
  8. *
  9. * Note! Tokens doesnt have a "GET" method. @see Tokens
  10. * as this can load them.
  11. *
  12. * @author Joachim M. Giaever (joachim[]giaever.org)
  13. * @version 0.1.2
  14. */
  15. final class Token extends Base {
  16. const VERSION = "0.1.1";
  17. protected $owner;
  18. public $token_name;
  19. public $token_sha1;
  20. /**
  21. * Initializes a token
  22. *
  23. * @see Base
  24. * @param User $user
  25. */
  26. public function __construct(string $api_url, string $password, User $user) {
  27. parent::__construct($api_url, $password);
  28. if (!$user->email)
  29. $user->load();
  30. $this->basic($user->email);
  31. $this->owner = $user;
  32. }
  33. /**
  34. * @see Base
  35. */
  36. protected function set_scope(string $method) {
  37. switch ($method) {
  38. case "create":
  39. $this->scope = sprintf("/users/%s/tokens", $this->owner->username);
  40. return true;
  41. }
  42. return false;
  43. }
  44. /**
  45. * Create a new token
  46. *
  47. * Valid parameters:
  48. *
  49. * 1. name
  50. *
  51. * This reflects the API v1 documentation.
  52. *
  53. * @param ...$args The parameter values
  54. * @return Token
  55. */
  56. public function create(...$args) {
  57. $params = array();
  58. $this->set_param($params, "name", $args, 0, "string", null);
  59. $this->filter_params($params);
  60. return parent::create($params);
  61. }
  62. }
  63. }