123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- <?php
- namespace Gogs\API\Request {
- /**
- * Repos is a collection of repos.
- *
- * @author Joachim M. Giaever (joachim[]giaever.org)
- * @version 0.1
- */
- final class Repos extends Collection {
- protected $owner;
- /**
- * Initialize a repos collection
- *
- * If owner is not set it will query the whole
- * repo archive on Gogs.
- *
- * @param string $api_url The api-url
- * @param string $api_token The api-token
- * @param User $owner The owner of the collection
- */
- public function __construct(string $api_url, string $api_token, User $owner = null) {
- parent::__construct($api_url, $api_token);
- $this->owner = $owner;
- }
- /**
- * @see Base
- */
- protected function set_scope(string $method) {
- switch ($method) {
- case "get":
- if ($this->owner instanceof Org)
- $this->scope = "/orgs/" . $this->owner->username . "/repos";
- else
- $this->scope = ($this->owner == null || $this->owner->authenticated() ? "/user" : "/users/" . $this->owner->username ) . "/repos";
- break;
- case "search":
- $this->scope = "/repos/search";
- break;
- default:
- return false;
- }
- return true;
- }
- /**
- * @see Collection
- */
- public function create(...$args) {
- $repo = new Repo($this->url, $this->token, $this->owner);
- if (count($args) > 0)
- $repo->create(...$args);
- return $repo;
- }
- /**
- * Searches for a repo.
- *
- * If the owner is specified the search will be
- * limited to the actual user.
- *
- * @todo Should rewrite now since set_scope is now the deal
- * @see Collection
- */
- public function search(array $params = array()) {
- $scope = substr($this->scope, 1, strpos($this->scope, "/", 1)-1);
- if (!isset($params["name"]) && !isset($params["q"]))
- throw new Exception\SearchParamException("Missing param <name>|<q>");
- if (isset($params["name"])) {
- $params["q"] = $params["name"];
- unset($params["name"]);
- }
- if (!isset($params["user"]))
- $params["user"] = isset($this->owner) ? $this->owner->id : 0;
- if (!isset($params["limit"]))
- $params["limit"] = 10;
- $repos = new \Gogs\Lib\Collection();
-
- switch ($scope) {
- case "user":
- case "users":
- case "org":
- $this->load();
- foreach($this->all() as $key => $repo) {
- if ($repo->search($params["q"]))
- $repos->set($repo);
- if ($repos->len() == $params["limit"])
- break;
- }
- default:
- $this->set_scope("search");
- $jenc = $this->method_get($params);
- $jdec = $this->json_decode($jenc);
- foreach($this->json_set_property($jdec) as $key)
- $repos->set($this->by_key($key), $key);
- }
-
- return $repos;
- }
- /**
- * @see Base
- */
- protected function json_set_property($obj) {
- if (isset($obj->data))
- return $this->json_set_property($obj->data);
- $rnames = array();
- if (is_array($obj)) {
- foreach ($obj as $key => $val) {
- $repo = new Repo($this->url, $this->token);
- $repo->json_set_property($val);
- $this->add($repo, $repo->rfull_name);
- $rnames[] = $repo->rfull_name;
- }
- }
- return $rnames;
- }
- }
- }
|