users.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php namespace Client\Request;
  2. /**
  3. * Returns one or more users in the Gogs installation,
  4. * depending on the called method.
  5. *
  6. * @author Joachim M. Giaever (joachim[]giaever.org)
  7. * @package request
  8. */
  9. class Users extends Collection {
  10. protected function set_scope(string $method) {
  11. var_dump("METHOD", $method);
  12. switch ($method) {
  13. default:
  14. $this->scope = "/users";
  15. }
  16. }
  17. /**
  18. * Returns a new user object. If arguments
  19. * is specified the user will be "created".
  20. *
  21. * The arguments can be left out to "create" the
  22. * user through the user object iteself.
  23. *
  24. * @param ...$args User->create arguments
  25. * @return \User
  26. */
  27. public function create(...$args) {
  28. $user = new User($this->url, $this->token, "-");
  29. if (count($args) != 0)
  30. $user->create(...$args);
  31. return $user;
  32. }
  33. public function get(string $s = "") {
  34. if ($this->by_key($s))
  35. return $this->by_key($s);
  36. $user = (new User($this->url, $this->token, $s))->load();
  37. $this->add($user, $user->ulogin);
  38. return $user;
  39. }
  40. public function search(array $params = array()) {
  41. if (!isset($params["name"]) && !isset($params['q']))
  42. throw new SearchParamException("Missing param <name>|<q>");
  43. if (isset($params["name"])) {
  44. $params["q"] = $params["name"];
  45. unset($params["name"]);
  46. }
  47. $jenc = $this->method_get("/search", $params);
  48. $this->json_set_property($this->json_decode($jenc));
  49. return $this;
  50. }
  51. protected function json_set_property($obj) {
  52. if (isset($obj->data)) {
  53. foreach($obj->data as $key => $val) {
  54. $user = new User($this->url, $this->token, $val->login);
  55. $user->json_set_property($val);
  56. $this->add($user, $user->ulogin);
  57. }
  58. }
  59. }
  60. }