Users.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. */
  10. final class Users extends Collection {
  11. protected function set_scope(string $method) {
  12. switch ($method) {
  13. case "get":
  14. $this->scope = "/users";
  15. break;
  16. case "search":
  17. $this->scope = "/users/search";
  18. default:
  19. return false;
  20. }
  21. return true;
  22. }
  23. /**
  24. * Returns a new user object. If arguments
  25. * is specified the user will be "created".
  26. *
  27. * The arguments can be left out to "create" the
  28. * user through the user object iteself.
  29. *
  30. * @param ...$args User->create arguments
  31. * @return \User
  32. */
  33. public function create(...$args) {
  34. $user = new User($this->url, $this->token, "-");
  35. if (count($args) != 0)
  36. $user->create(...$args);
  37. return $user;
  38. }
  39. public function get(string $s = "") {
  40. if ($this->by_key($s))
  41. return $this->by_key($s);
  42. $user = (new User($this->url, $this->token, $s))->load();
  43. $this->add($user, $user->ulogin);
  44. return $user;
  45. }
  46. public function search(array $params = array()) {
  47. if (!isset($params["name"]) && !isset($params['q']))
  48. throw new Exception\SearchParamException("Missing param <name>|<q>");
  49. if (isset($params["name"])) {
  50. $params["q"] = $params["name"];
  51. unset($params["name"]);
  52. }
  53. $this->set_scope("search");
  54. $jenc = $this->method_get($params);
  55. $old = $this->all();
  56. $this->json_set_property($this->json_decode($jenc));
  57. $users = new \Gogs\Lib\Collection();
  58. $users->set(array_diff_key($this->all(), $old));
  59. return $users;
  60. }
  61. protected function json_set_property($obj) {
  62. if (isset($obj->data)) {
  63. foreach($obj->data as $key => $val) {
  64. $user = new User($this->url, $this->token, $val->login);
  65. $user->json_set_property($val);
  66. $this->add($user, $user->ulogin);
  67. }
  68. }
  69. }
  70. }
  71. }
  72. ?>