Orgs.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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.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. $this->add($org, $org->username);
  43. }
  44. return $org;
  45. }
  46. /**
  47. * Get an organization by indentifier.
  48. *
  49. * Method will first look through organizations
  50. * already loaded. If not found it will return a
  51. * new object.
  52. *
  53. * Method does not ensure the organization in loaded
  54. * from Gogs so the user should call `->load()` on
  55. * returned element.
  56. *
  57. * @param string $s
  58. * @return Org
  59. */
  60. public function get(string $s) {
  61. if (($org = $this->by_key($s)))
  62. return $org;
  63. return new Org($this->url, $this->token, $this->owner, $s);
  64. }
  65. /**
  66. * Search for an organization.
  67. *
  68. * Params can be an array of
  69. * ```php
  70. * $orgs->search(array(
  71. * "name" => "name", // alt. "q". required
  72. * "limit" => 10, // not required, default: 10
  73. * ));
  74. * ```
  75. * By now, this method can be intensive, as it will load
  76. * every organization and then do a match on each entry.
  77. *
  78. * @param array $params Search parameters
  79. * @return Orgs
  80. * @throws Exception\SearchParamException on missing parameters
  81. */
  82. public function search(array $params = array()) {
  83. if (!isset($params["name"]) && !isset($params["q"]))
  84. throw new Exception\SearchParamException("Missing param <name>|<q>");
  85. $q = isset($params["name"]) ? $params["name"] : $params["q"];
  86. $l = isset($params["limit"]) ? $params["limit"] : 10;
  87. $this->load();
  88. $orgs = new Orgs($this->url, $this->token, $this->owner);
  89. foreach ($this->all() as $key => $org) {
  90. if ($org->search($q))
  91. $orgs->add($org, $org->username);
  92. if ($orgs->len() == $l)
  93. break;
  94. }
  95. return $orgs;
  96. }
  97. /**
  98. * @see Base
  99. */
  100. protected function json_set_property($obj) {
  101. $rnames = array();
  102. foreach($obj as $val) {
  103. $org = new Org($this->url, $this->token, $this->owner, $val->username);
  104. $org->json_set_property($val);
  105. $this->add($org, $val->username);
  106. $rnames[] = $val->username;
  107. }
  108. return $rnames;
  109. }
  110. /**
  111. * @see Collection
  112. */
  113. public function sort_by(int $flag = Collection::SORT_INDEX) {
  114. return $this->sort("ksort");
  115. }
  116. }
  117. }