repo.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. protected $owner;
  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. $this->scope = "/user" . ($this->rowner->authenticated() ? "" : "/" . $this->rowner->uusername) . "/repos";
  39. break;
  40. case "delete":
  41. $this->scope = "/repos/" . $this->rowner->uusername . "/" . $this->rname;
  42. break;
  43. default:
  44. $this->scope = "/repos/" . ($this->rowner ? $this->rowner->uusername . "/" . $this->rname : $this->rfull_name);
  45. }
  46. }
  47. protected function json_set_property($obj) {
  48. foreach($obj as $key => $val) {
  49. $key = 'r' . $key;
  50. if (property_exists($this, $key)) {
  51. switch ($key) {
  52. case 'rowner':
  53. if (!$this->rowner) {
  54. $user = new User($this->url, $this->token);
  55. $user->json_set_property($val);
  56. $this->{$key} = $user;
  57. }
  58. break;
  59. default:
  60. $this->{$key} = $val;
  61. }
  62. }
  63. }
  64. $this->loaded = true;
  65. }
  66. public function search(string $q) {
  67. $searchable = sprintf("%s %s", $this->rname, $this->rdescription);
  68. return stripos($searchable, $q) !== false;
  69. }
  70. public function create(...$args) {
  71. if ($this->loaded)
  72. return false;
  73. $params = array(
  74. "name" => isset($args[0]) && is_string($args[0]) ? $args[0] : null,
  75. "description" => isset($args[1]) && is_string($args[1]) ? $args[1] : null,
  76. "private" => isset($args[2]) && is_bool($args[2]) ? $args[2] : false,
  77. "auto_init" => isset($args[3]) && is_bool($args[3]) ? $args[3] : false,
  78. "gitignores" => isset($args[4]) && is_string($args[4]) ? $args[4] : null,
  79. "licence" => isset($args[5]) && is_string($args[5]) ? $args[5] : null,
  80. "readme" => isset($args[6]) && is_string($args[6]) ? $args[6] : "Default"
  81. );
  82. $params = array_filter($params, function($val) {
  83. return $val != null;
  84. });
  85. parent::create($params);
  86. }
  87. }