Users.php 2.8 KB

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