orgs.php 1.8 KB

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