Org.php 3.0 KB

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