Org.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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.4
  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. * @see Base
  23. * @param User $owner The owner of the organization
  24. * @param string $oname Organization name
  25. */
  26. public function __construct(string $api_url, string $api_token, User $owner = null, string $oname = null) {
  27. parent::__construct($api_url, $api_token);
  28. $this->username = $oname;
  29. $this->owner = $owner;
  30. }
  31. /**
  32. * @see Base
  33. * @throws Exception\InvalidMethodRequestException when owner is not set
  34. * @throws Exception\RequestErrorException when missing organization data
  35. */
  36. protected function set_scope(string $method) {
  37. switch ($method) {
  38. case "create":
  39. if ($this->owner == null)
  40. throw new Exception\InvalidMethodRequestException("Cant create organization without a related User");
  41. $this->scope = "/admin/users/" . $this->owner->username . "/orgs";
  42. return true;
  43. case "get":
  44. if (!$this->username)
  45. throw new Exception\RequestErrorException("Missing organization-data 'username'.");
  46. $this->scope = "/orgs/" . $this->username;
  47. return true;
  48. }
  49. return false;
  50. }
  51. /**
  52. * Create a new user
  53. *
  54. * Valid parameters:
  55. *
  56. * 1. username
  57. * 2. full_name
  58. * 3. description
  59. * 4. website
  60. * 5. location
  61. *
  62. * This reflects the API v1 doc, but is in an order
  63. * where the required fields is first.
  64. *
  65. * @todo Create team within org with user
  66. * @param ...$args The parameter values.
  67. * @return Org
  68. */
  69. public function create(...$args) {
  70. $params = array();
  71. $this->set_param($params, "username", $args, 0, "string", null);
  72. $this->set_param($params, "full_name", $args, 1, "string", null);
  73. $this->set_param($params, "description", $args, 2, "string", null);
  74. $this->set_param($params, "website", $args, 3, "string", null);
  75. $this->set_param($params, "location", $args, 4, "string", null);
  76. $this->filter_params($params);
  77. return Base::create($params);
  78. }
  79. }
  80. }
  81. ?>