Org.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. namespace Gogs\API\Request {
  3. final class Org extends User {
  4. public $udescription;
  5. public $uwebsite;
  6. public $ulocation;
  7. private $owner;
  8. public function __construct(string $api_url, string $api_token, string $oname = null, User $owner = null) {
  9. $this->ousername = $oname;
  10. $this->owner = $owner;
  11. parent::__construct($api_url, $api_token);
  12. }
  13. protected function set_scope(string $method) {
  14. switch ($method) {
  15. case "create":
  16. if ($this->owner == null)
  17. throw new Exception\InvalidMethodRequestException("Cant create organization without a related User");
  18. $this->scope = "/admin/users/" . $this->owner->uusername . "/orgs";
  19. return true;
  20. case "get":
  21. $this->scope = "/orgs/" . $this->uusername;
  22. return true;
  23. }
  24. }
  25. public function search(string $q) {
  26. $searchable = sprintf("%s %s %s", $this->ufull_name, $this->uusername, $this->udescription);
  27. return stripos($searchable, $q) !== false;
  28. }
  29. public function create(...$args) {
  30. $params = array(
  31. "username" => isset($args[0]) && is_string($args[0]) ? $args[0] : null,
  32. "ufull_name" => isset($args[1]) && is_string($args[1]) ? $args[1] : null,
  33. "description" => isset($args[2]) && is_string($args[2]) ? $args[2] : null,
  34. "website" => isset($args[3]) && is_string($args[3]) ? $args[3] : null,
  35. "location" => isset($args[4]) && is_string($args[4]) ? $args[4] : null
  36. );
  37. $params = array_filter($params, function($val) {
  38. return $val != null;
  39. });
  40. parent::create($params);
  41. }
  42. }
  43. }
  44. ?>