Orgs.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace Gogs\API\Request {
  3. /**
  4. * Orgs is a collection of organizations.
  5. *
  6. * @author Joachim M. Giaever (joachim[]giaever.org)
  7. * @version 0.1
  8. */
  9. final class Orgs extends Collection {
  10. protected $owner;
  11. public function __construct(string $api_url, string $api_token, User $owner) {
  12. $this->owner = $owner;
  13. parent::__construct($api_url, $api_token);
  14. }
  15. /**
  16. * @see Base
  17. */
  18. protected function set_scope(string $method) {
  19. switch ($method) {
  20. case "get":
  21. case "load":
  22. $this->scope = ($this->owner == null || $this->owner->authenticated() ? "/user" : "/users/" . $this->owner->username) . "/orgs";
  23. return true;
  24. default:
  25. return false;
  26. }
  27. }
  28. /**
  29. * Create a new organization
  30. *
  31. * If arguments are given, the User will be created,
  32. * otherise it will return an initialized object,
  33. * leaving the programmer to create the user.
  34. *
  35. * @see Base
  36. * @return User
  37. */
  38. public function create(...$args) {
  39. $org = new Org($this->url, $this->token, $this->owner);
  40. if (count($args) > 0)
  41. $org->create(...$args);
  42. return $org;
  43. }
  44. /**
  45. * Get an organization by indentifier.
  46. *
  47. * Method will first look through organizations
  48. * already loaded. If not found it will return a
  49. * new object.
  50. *
  51. * Method does not ensure the organization in loaded
  52. * from Gogs so the user should call `->load()` on
  53. * returned element.
  54. *
  55. * @param string $s
  56. * @return Org
  57. */
  58. public function get(string $s) {
  59. if (($org = $this->by_key($s)))
  60. return $org;
  61. return new Org($this->url, $this->token, $this->owner, $s);
  62. }
  63. /**
  64. * Search for an organization.
  65. *
  66. * Params can be an array of
  67. * ```php
  68. * $orgs->search(array(
  69. * "name" => "name", // alt. "q". required
  70. * "limit" => 10, // not required, default: 10
  71. * ));
  72. * ```
  73. * By now, this method can be intensive, as it will load
  74. * every organization and then do a match on each entry.
  75. *
  76. * @param array $params Search parameters
  77. * @return Orgs
  78. * @throws Exception\SearchParamException on missing parameters
  79. */
  80. public function search(array $params = array()) {
  81. if (!isset($params["name"]) && !isset($params["q"]))
  82. throw new Exception\SearchParamException("Missing param <name>|<q>");
  83. $q = isset($params["name"]) ? $params["name"] : $params["q"];
  84. $l = isset($params["limit"]) ? $params["limit"] : 10;
  85. $this->load();
  86. $orgs = new Orgs($this->url, $this->token, $this->owner);
  87. foreach ($this->all() as $key => $org) {
  88. if ($org->search($q))
  89. $orgs->add($org, $org->username);
  90. if ($orgs->len() == $l)
  91. break;
  92. }
  93. return $orgs;
  94. }
  95. /**
  96. * @see Base
  97. */
  98. protected function json_set_property($obj) {
  99. $rnames = array();
  100. foreach($obj as $val) {
  101. $org = new Org($this->url, $this->token, $this->owner, $val->username);
  102. $org->json_set_property($val);
  103. $this->add($org, $val->username);
  104. $rnames[] = $val->username;
  105. }
  106. return $rnames;
  107. }
  108. /**
  109. * @see Collection
  110. */
  111. public function sort_by(int $flag = Collection::SORT_INDEX) {
  112. return $this->sort("ksort");
  113. }
  114. }
  115. }