Orgs.php 4.0 KB

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