Branches.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 string $api_url The API URL
  18. * @param string $api_token The API token
  19. * @param Repo $repo The repository
  20. */
  21. public function __construct(string $api_url, string $api_token, Repo $repo) {
  22. $this->repo = $repo;
  23. parent::__construct($api_url, $api_token);
  24. }
  25. /**
  26. * @see Base
  27. */
  28. protected function set_scope(string $method) {
  29. switch ($method) {
  30. case "get":
  31. case "load":
  32. if ($this->repo == null)
  33. throw new Exception\InvalidMethodRequestException("Missing repository for branches");
  34. $this->scope = sprintf("/repos/%s/%s/branches", $this->repo->owner->username, $this->repo->name);
  35. return true;
  36. }
  37. return false;
  38. }
  39. /**
  40. * Search for a branch.
  41. *
  42. * This method doesnt search by a uri, instead it will
  43. * load every branch from Gogs and do a match on this.
  44. *
  45. * Params can be an array of
  46. * ```php
  47. * $branches->search(array(
  48. * "name" => "name", // alt. "q". required
  49. * "limit" => 10, // not required, default: 10
  50. * ));
  51. * ```
  52. * By now, this method can be intensive, as it will load
  53. * every branch and then do a match on each entry.
  54. *
  55. * @see Base
  56. * @see Collection
  57. * @throws Exception\SearchParamException on missing parameters
  58. * @return Branches
  59. */
  60. public function search(array $params = array(), bool $strict = true) {
  61. if (!isset($params["name"]) && !isset($params["q"]))
  62. throw new Exception\SearchParamException("Missing <name|q> parameter");
  63. if (isset($params["name"])) {
  64. $params["q"] = $params["name"];
  65. unset($params["q"]);
  66. }
  67. if (!isset($params["limit"]))
  68. $params["limit"] = 10;
  69. $branches = new Branches($this->url, $this->token, $this->repo);
  70. if (!$this->loaded)
  71. $this->load();
  72. foreach($this->all() as $key => $branch) {
  73. if ($branch->search(array("name" => $params["q"]), $strict))
  74. $branches->add($branch, $branch->name);
  75. if ($branches->len() == $params["limit"])
  76. break;
  77. }
  78. return $branches;
  79. }
  80. /**
  81. * @see Collection
  82. */
  83. public function sort_by(int $flag = Collection::SORT_INDEX) {
  84. return $this->sort("ksort");
  85. }
  86. /**
  87. * @see Collection
  88. */
  89. protected function add_object(\stdClass $obj) {
  90. $branch = new Branch($this->url, $this->token, $this->repo, $obj->name);
  91. $branch->json_set_property($obj);
  92. return $this->add($branch, $branch->name);
  93. }
  94. }
  95. }