123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- <?php
- namespace Gogs\API\Request {
- /**
- * Returns one or more users in the Gogs installation,
- * depending on the called method.
- *
- * @author Joachim M. Giaever (joachim[]giaever.org)
- * @package request
- */
- final class Users extends Collection {
- protected function set_scope(string $method) {
- switch ($method) {
- case "get":
- $this->scope = "/users";
- break;
- case "search":
- $this->scope = "/users/search";
- default:
- return false;
- }
- return true;
- }
- /**
- * Returns a new user object. If arguments
- * is specified the user will be "created".
- *
- * The arguments can be left out to "create" the
- * user through the user object iteself.
- *
- * @param ...$args User->create arguments
- * @return \User
- */
- public function create(...$args) {
- $user = new User($this->url, $this->token, "-");
- if (count($args) != 0)
- $user->create(...$args);
- return $user;
- }
- public function get(string $s = "") {
- if ($this->by_key($s))
- return $this->by_key($s);
- $user = (new User($this->url, $this->token, $s))->load();
- $this->add($user, $user->ulogin);
- return $user;
- }
- public function search(array $params = array()) {
- if (!isset($params["name"]) && !isset($params['q']))
- throw new Exception\SearchParamException("Missing param <name>|<q>");
- if (isset($params["name"])) {
- $params["q"] = $params["name"];
- unset($params["name"]);
- }
- $this->set_scope("search");
- $jenc = $this->method_get($params);
- $old = $this->all();
- $this->json_set_property($this->json_decode($jenc));
- $users = new \Gogs\Lib\Collection();
- $users->set(array_diff_key($this->all(), $old));
- return $users;
- }
- protected function json_set_property($obj) {
- if (isset($obj->data)) {
- foreach($obj->data as $key => $val) {
- $user = new User($this->url, $this->token, $val->login);
- $user->json_set_property($val);
- $this->add($user, $user->ulogin);
- }
- }
- }
- }
- }
- ?>
|