Orgs.php 4.1 KB

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