Org.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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.3
  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 bool
  68. */
  69. public function create(...$args) {
  70. $params = array(
  71. "username" => isset($args[0]) && is_string($args[0]) ? $args[0] : null,
  72. "full_name" => isset($args[1]) && is_string($args[1]) ? $args[1] : null,
  73. "description" => isset($args[2]) && is_string($args[2]) ? $args[2] : null,
  74. "website" => isset($args[3]) && is_string($args[3]) ? $args[3] : null,
  75. "location" => isset($args[4]) && is_string($args[4]) ? $args[4] : null
  76. );
  77. $params = array_filter($params, function($val) {
  78. return $val != null;
  79. });
  80. Base::create($params);
  81. }
  82. }
  83. }
  84. ?>