Org.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace Gogs\API\Request {
  3. /**
  4. * Stores data and methods related to a single organization.
  5. *
  6. * By now the following are supported:
  7. *
  8. * * GET `/orgs/username`
  9. * * POST `/admin/users/username/orgs` (**Requires** admin rights. Curl will throw NotAuthorized exception if not).
  10. *
  11. * @author Joachim M. Giaever (joachim[]giaever.org)
  12. * @version 0.1.1
  13. */
  14. final class Org extends User {
  15. public $org_description;
  16. public $org_website;
  17. public $org_location;
  18. private $owner;
  19. /**
  20. * Initialize an organization.
  21. *
  22. * @param string $api_url The api-url
  23. * @param string $api_token The api-token
  24. * @param User $owner The owner of the organization
  25. * @param string $oname Organization name
  26. */
  27. public function __construct(string $api_url, string $api_token, User $owner = null, string $oname = null) {
  28. parent::__construct($api_url, $api_token);
  29. $this->username = $oname;
  30. $this->owner = $owner;
  31. }
  32. /**
  33. * @see Base
  34. * @throws Exception\InvalidMethodRequestException when owner is not set
  35. * @throws Exception\RequestErrorException when missing organization data
  36. */
  37. protected function set_scope(string $method) {
  38. switch ($method) {
  39. case "create":
  40. if ($this->owner == null)
  41. throw new Exception\InvalidMethodRequestException("Cant create organization without a related User");
  42. $this->scope = "/admin/users/" . $this->owner->username . "/orgs";
  43. return true;
  44. case "get":
  45. if (!$this->username)
  46. throw new Exception\RequestErrorException("Missing organization-data 'username'.");
  47. $this->scope = "/orgs/" . $this->username;
  48. return true;
  49. }
  50. return false;
  51. }
  52. /**
  53. * @see Base
  54. */
  55. public function search(string $q) {
  56. $searchable = sprintf("%s %s %s", $this->full_name, $this->username, $this->description);
  57. return stripos($searchable, $q) !== false;
  58. }
  59. /**
  60. * Create a new user
  61. *
  62. * Valid parameters:
  63. *
  64. * 1. username
  65. * 2. full_name
  66. * 3. description
  67. * 4. website
  68. * 5. location
  69. *
  70. * This reflects the API v1 doc, but is in an order
  71. * where the required fields is first.
  72. *
  73. * @todo Create team within org with user
  74. * @param ...$args The parameter values.
  75. * @return bool
  76. */
  77. public function create(...$args) {
  78. $params = array(
  79. "username" => isset($args[0]) && is_string($args[0]) ? $args[0] : null,
  80. "full_name" => isset($args[1]) && is_string($args[1]) ? $args[1] : null,
  81. "description" => isset($args[2]) && is_string($args[2]) ? $args[2] : null,
  82. "website" => isset($args[3]) && is_string($args[3]) ? $args[3] : null,
  83. "location" => isset($args[4]) && is_string($args[4]) ? $args[4] : null
  84. );
  85. $params = array_filter($params, function($val) {
  86. return $val != null;
  87. });
  88. Base::create($params);
  89. }
  90. }
  91. }
  92. ?>