12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- <?php
- namespace Gogs\API\Request {
- final class User extends Base {
- private $authenticated;
- public $uid;
- public $ulogin;
- public $ufull_name;
- public $uemail;
- public $uavatar_url;
- public $uusername;
- public function __construct(string $api_url, string $api_token, string $user = "") {
- $this->uusername = (strlen($user) == 0 || $user == "me" ? "me" : $user);
- $this->authenticated = $this->uusername == "me";
- parent::__construct($api_url, $api_token);
- }
- protected function set_scope(string $method) {
- switch($method) {
- case "create":
- if ($this->loaded)
- throw new Exception\InvalidMethodRequest("Cannot create user of existing user");
- $this->scope = "/admin/users";
-
- return true;
- case "delete":
- $this->scope = "/admin/users/" . $this->uusername;
- return true;
- default:
- $this->scope = ($this->authenticated ? "/user" : "/users/" . $this->uusername);
- }
- }
- public function authenticated() {
- return $this->authenticated;
- }
- public function repos() {
- return new Repos($this->url, $this->token, $this);
- }
- public function repo(string $name) {
- return (new Repo($this->url, $this->token, $this, $name))->load();
- }
- public function organizations() {
- return new Orgs($this->url, $this->token, $this);
- }
- public function create(...$args) {
- $params = array(
- "username" => isset($args[0]) && is_string($args[0]) ? $args[0] : null,
- "email" => isset($args[1]) && is_string($args[1]) ? $args[1] : null,
- "source_id" => isset($args[2]) && is_numeric($args[2]) ? $args[2] : null,
- "login_name" => isset($args[3]) && is_string($args[3]) ? $args[3] : null,
- "password" => isset($args[4]) && is_string($args[4]) ? $args[4] : null,
- "send_notify" => isset($args[5]) && is_bool($args[5]) ? $args[5] : null
- );
- $params = array_filter($params, function($val) {
- return $val != null;
- });
- parent::create($params);
- }
- protected function json_set_property($obj) {
- foreach ($obj as $key => $value) {
- $key = 'u' . $key;
- if (property_exists($this, $key))
- $this->{$key} = $value;
- }
- $this->loaded = true;
- }
- }
- }
- ?>
|