Repo.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace Gogs\API\Request {
  3. final class Repo extends Base {
  4. public $rid;
  5. public $rowner;
  6. public $rname;
  7. public $rfull_name;
  8. public $rdescription;
  9. public $rprivate;
  10. public $rfork;
  11. public $rparent;
  12. public $rempty;
  13. public $rmirror;
  14. public $rsize;
  15. public $rhtml_url;
  16. public $rssh_url;
  17. public $rclone_url;
  18. public $rwebsite;
  19. public $rstars_count;
  20. public $rforks_count;
  21. public $rwatchers_count;
  22. public $ropen_issues_count;
  23. public $rdefault_branch;
  24. public $rcreated_at;
  25. public $rupdated_at;
  26. public $rpermissions;
  27. public $radmin;
  28. public $rpush;
  29. public $rpull;
  30. public function __construct(string $api_url, string $api_token, User $owner = null, string $name = null) {
  31. $this->rowner = $owner;
  32. $this->rname = $name;
  33. parent::__construct($api_url, $api_token);
  34. }
  35. protected function set_scope(string $method) {
  36. switch ($method) {
  37. case "create":
  38. if ($this->rowner instanceof Org)
  39. $this->scope = "/org/" . $this->rowner->uusername . "/repos";
  40. elseif ($this->rowner->authenticated())
  41. $this->scope = "/user/repos";
  42. else
  43. $this->scope = "/admin/users/" . $this->rowner->uusername . "/repos";
  44. break;
  45. case "delete":
  46. $this->scope = "/repos/" . $this->rowner->uusername . "/" . $this->rname;
  47. break;
  48. case "get":
  49. $this->scope = "/repos/" . ($this->rowner ? $this->rowner->uusername . "/" . $this->rname : $this->rfull_name);
  50. break;
  51. default:
  52. return false;
  53. }
  54. return true;
  55. }
  56. protected function json_set_property($obj) {
  57. foreach($obj as $key => $val) {
  58. $key = 'r' . $key;
  59. if (property_exists($this, $key)) {
  60. switch ($key) {
  61. case 'rowner':
  62. if (!$this->rowner) {
  63. $user = new User($this->url, $this->token);
  64. $user->json_set_property($val);
  65. $this->{$key} = $user;
  66. }
  67. break;
  68. default:
  69. $this->{$key} = $val;
  70. }
  71. }
  72. }
  73. $this->loaded = true;
  74. }
  75. public function search(string $q) {
  76. $searchable = sprintf("%s %s", $this->rname, $this->rdescription);
  77. return stripos($searchable, $q) !== false;
  78. }
  79. public function create(...$args) {
  80. $params = array(
  81. "name" => isset($args[0]) && is_string($args[0]) ? $args[0] : null,
  82. "description" => isset($args[1]) && is_string($args[1]) ? $args[1] : null,
  83. "private" => isset($args[2]) && is_bool($args[2]) ? $args[2] : false,
  84. "auto_init" => isset($args[3]) && is_bool($args[3]) ? $args[3] : false,
  85. "gitignores" => isset($args[4]) && is_string($args[4]) ? $args[4] : null,
  86. "licence" => isset($args[5]) && is_string($args[5]) ? $args[5] : null,
  87. "readme" => isset($args[6]) && is_string($args[6]) ? $args[6] : "Default"
  88. );
  89. $params = array_filter($params, function($val) {
  90. return $val != null;
  91. });
  92. parent::create($params);
  93. }
  94. }
  95. }
  96. ?>