repo.php 3.3 KB

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