Users.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. return $user;
  37. }
  38. public function get(string $s = "") {
  39. if ($this->by_key($s))
  40. return $this->by_key($s);
  41. $user = (new User($this->url, $this->token, $s))->load();
  42. $this->add($user, $user->login);
  43. return $user;
  44. }
  45. public function search(array $params = array()) {
  46. if (!isset($params["name"]) && !isset($params['q']))
  47. throw new Exception\SearchParamException("Missing param <name>|<q>");
  48. if (isset($params["name"])) {
  49. $params["q"] = $params["name"];
  50. unset($params["name"]);
  51. }
  52. $this->set_scope("search");
  53. $old = $this->all();
  54. $this->json_set_property(
  55. $this->json_decode(
  56. $this->method_get($params)
  57. )
  58. );
  59. $users = new \Gogs\Lib\Collection();
  60. $users->set(array_diff_key($this->all(), $old));
  61. return $users;
  62. }
  63. public function sort_by(int $flag = Collection::SORT_INDEX) {
  64. return $this->sort("ksort");
  65. }
  66. protected function json_set_property($obj) {
  67. $rnames[] = array();
  68. if (isset($obj->data)) {
  69. foreach($obj->data as $key => $val) {
  70. $user = new User($this->url, $this->token, $val->username);
  71. $user->json_set_property($val);
  72. $this->add($user, $user->username);
  73. $rnames[] = $user->username;
  74. }
  75. }
  76. return $rnames;
  77. }
  78. }
  79. }
  80. ?>