Users.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace Gogs\API\Request {
  3. /**
  4. * Returns one or more users in the Gogs installation,
  5. * depending on the called method.
  6. *
  7. * @author Joachim M. Giaever (joachim[]giaever.org)
  8. * @package request
  9. * @version 0.1.3
  10. */
  11. final class Users extends Collection {
  12. /**
  13. * @see Base
  14. */
  15. protected function set_scope(string $method) {
  16. switch ($method) {
  17. case "search":
  18. $this->scope = "/users/search";
  19. break;
  20. default:
  21. return false;
  22. }
  23. return true;
  24. }
  25. /**
  26. * Returns a new user object. If arguments
  27. * is specified the user will be "created".
  28. *
  29. * The arguments can be left out to "create" the
  30. * user through the user object iteself.
  31. *
  32. * @param ...$args User->create arguments
  33. * @return \User
  34. */
  35. public function create(...$args) {
  36. $user = new User($this->url, $this->token, "-");
  37. if (count($args) != 0) {
  38. $user->create(...$args);
  39. $this->add($user, $user->username);
  40. }
  41. return $user;
  42. }
  43. /**
  44. * Return a user by username.
  45. *
  46. * @param string $s The username
  47. * @return
  48. */
  49. public function get(string $s) {
  50. if ($this->by_key($s))
  51. return $this->by_key($s);
  52. $user = (new User($this->url, $this->token, $s))->load();
  53. $this->add($user, $user->login);
  54. return $user;
  55. }
  56. /**
  57. * Search for an user
  58. *
  59. * Params can be an array of
  60. * ```php
  61. * $orgs->search(array(
  62. * "name" => "name", // alt. "q". required
  63. * "limit" => 10, // not required, default: 10
  64. * ));
  65. * ```
  66. * By now, this method can be intensive, as it will load
  67. * every organization and then do a match on each entry.
  68. *
  69. * @see Base
  70. * @see Collection
  71. * @throws Exception\SearchParamException on missing parameters
  72. * @return Orgs
  73. */
  74. public function search(array $params = array(), bool $strict = false) {
  75. if (!isset($params["name"]) && !isset($params['q']))
  76. throw new Exception\SearchParamException("Missing param <name>|<q>");
  77. if (isset($params["name"])) {
  78. $params["q"] = $params["name"];
  79. unset($params["name"]);
  80. }
  81. $this->set_scope("search");
  82. $old = $this->all();
  83. $this->json_set_property(
  84. $this->json_decode(
  85. $this->method_get($params)
  86. )
  87. );
  88. $users = new Users($this->url, $this->token);
  89. $users->add(array_diff_key($this->all(), $old));
  90. return $users;
  91. }
  92. /**
  93. * @see Collection
  94. */
  95. public function sort_by(int $flag = Collection::SORT_INDEX) {
  96. return $this->sort("ksort");
  97. }
  98. /**
  99. * @see Collection
  100. */
  101. protected function add_object(\stdClass $obj) {
  102. $user = new User($this->url, $this->token, $obj->username);
  103. $user->json_set_property($obj);
  104. $this->add($user, $user->username);
  105. }
  106. }
  107. }
  108. ?>