Branches.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace Gogs\API\Request {
  3. /**
  4. * Holds a collection of Branches for a Repository.
  5. *
  6. * Supported:
  7. * * GET `/repos/username/repo/branches`
  8. *
  9. * @author Joachim M. Giaever (joachim[]giaever.org)
  10. * @version 0.1
  11. */
  12. final class Branches extends Collection {
  13. protected $repo;
  14. /**
  15. * Initialize Brances for a given repo
  16. *
  17. * @param Repo $repo The repository
  18. */
  19. public function __construct(string $api_url, string $api_token, Repo $repo) {
  20. $this->repo = $repo;
  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. if ($this->repo == null)
  31. throw new Exception\InvalidMethodRequestException("Missing repository for branches");
  32. $this->scope = sprintf("/repos/%s/%s/branches", $this->repo->owner->username, $this->repo->name);
  33. return true;
  34. }
  35. return false;
  36. }
  37. /**
  38. * Search for a branch.
  39. *
  40. * This method doesnt search by a uri, instead it will
  41. * load every branch from Gogs and do a match on this.
  42. *
  43. * Params can be an array of
  44. * ```php
  45. * $branches->search(array(
  46. * "name" => "name", // alt. "q". required
  47. * "limit" => 10, // not required, default: 10
  48. * ));
  49. * ```
  50. * By now, this method can be intensive, as it will load
  51. * every branch and then do a match on each entry.
  52. *
  53. * @see Base
  54. * @see Collection
  55. * @throws Exception\SearchParamException on missing parameters
  56. * @return Branches
  57. */
  58. public function search(array $params = array(), bool $strict = true) {
  59. if (!isset($params["name"]) && !isset($params["q"]))
  60. throw new Exception\SearchParamException("Missing <name|q> parameter");
  61. if (isset($params["name"])) {
  62. $params["q"] = $params["name"];
  63. unset($params["q"]);
  64. }
  65. if (!isset($params["limit"]))
  66. $params["limit"] = 10;
  67. $branches = new Branches($this->url, $this->token, $this->repo);
  68. if (!$this->loaded)
  69. $this->load();
  70. foreach($this->all() as $key => $branch) {
  71. if ($branch->search(array("name" => $params["q"]), $strict))
  72. $branches->add($branch, $branch->name);
  73. if ($branches->len() == $params["limit"])
  74. break;
  75. }
  76. return $branches;
  77. }
  78. /**
  79. * @see Collection
  80. */
  81. public function sort_by(int $flag = Collection::SORT_INDEX) {
  82. return $this->sort("ksort");
  83. }
  84. /**
  85. * @see Collection
  86. */
  87. protected function add_object(\stdClass $obj) {
  88. $branch = new Branch($this->url, $this->token, $this->repo, $obj->name);
  89. $branch->json_set_property($obj);
  90. return $this->add($branch, $branch->name);
  91. }
  92. }
  93. }