Orgs.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. namespace Gogs\API\Request {
  3. class Orgs extends Collection {
  4. protected $owner;
  5. public function __construct(string $api_url, string $api_token, User $owner) {
  6. $this->owner = $owner;
  7. parent::__construct($api_url, $api_token);
  8. }
  9. protected function set_scope(string $method) {
  10. switch ($method) {
  11. case "get":
  12. $this->scope = ($this->owner == null || $this->owner->authenticated() ? "/user" : "/users/" . $this->owner->uusername) . "/orgs";
  13. return true;
  14. }
  15. }
  16. public function create(...$args) {
  17. $org = new Org($this->url, $this->token, null, $this->owner);
  18. if (count($args) > 0)
  19. $org->create(...$args);
  20. return $org;
  21. }
  22. public function get(string $s) {
  23. if (($org = $this->by_key($s)))
  24. return $org;
  25. return new Org($this->url, $this->token, $s, $this->owner);
  26. }
  27. public function search(array $params = array()) {
  28. if (!isset($params["name"]) && !isset($params["q"]))
  29. throw new Exception\SearchParamException("Missing param <name>|<q>");
  30. $q = isset($params["name"]) ? $params["name"] : $params["q"];
  31. $l = isset($params["limit"]) ? $params["limit"] : 10;
  32. $this->load();
  33. $orgs = new CollectionResult();
  34. foreach ($this->all() as $key => $org) {
  35. if ($org->search($q))
  36. $orgs->set($org);
  37. if ($orgs->len() == $l)
  38. break;
  39. }
  40. return $orgs;
  41. }
  42. protected function json_set_property($obj) {
  43. foreach($obj as $val) {
  44. $org = new Org($this->url, $this->token, null, $this->owner);
  45. $org->json_set_property($val);
  46. $this->add($org, $val->username);
  47. }
  48. }
  49. }
  50. }