Large changes to inter-object interfaces + More

+ Created a new core interface (called RequestChainable) and applied it to most of the objects using a trait
+ Altered the `__construct()` and `fromJson()` methods of all model classes by replacing the second parameter ($apiRequester) with a $caller parameter
+ Altered the  `__construct()` method of all requester classes to make them accept $client by reference instead of by value
+ Altered the  `__construct()` method of all requester classes by replacing the second parameter ($authToken) with a $caller parameter
+ Changed the name of several methods and properties
+ Altered several docblocks
This commit is contained in:
Benjamin Blake
2020-02-26 20:23:06 -07:00
parent 57bf45938f
commit 70f978847e
21 changed files with 568 additions and 309 deletions

View File

@ -4,15 +4,21 @@ namespace Gitea\Api\Abstracts;
use Gitea\Client; use Gitea\Client;
// Traits
use Gitea\Core\Traits\RequestChainable;
use Gitea\Api\Interfaces\ApiRequesterInterface; use Gitea\Api\Interfaces\ApiRequesterInterface;
use Gitea\Core\Interfaces\RequestChainableInterface;
/** /**
* Abstract class for Api classes * Abstract class for Api classes
* *
* @author Benjamin Blake (sitelease.ca) * @author Benjamin Blake (sitelease.ca)
*/ */
abstract class AbstractApiRequester implements ApiRequesterInterface abstract class AbstractApiRequester implements ApiRequesterInterface, RequestChainableInterface
{ {
use RequestChainable;
/** /**
* The client * The client
* *
@ -42,7 +48,7 @@ abstract class AbstractApiRequester implements ApiRequesterInterface
* @var array * @var array
*/ */
protected $defaultHeaders = [ protected $defaultHeaders = [
'Accept' => 'application/json' 'Accept' => 'application/json'
]; ];
/** /**
@ -68,7 +74,7 @@ abstract class AbstractApiRequester implements ApiRequesterInterface
* @var array * @var array
*/ */
protected $postDefaultHeaders = [ protected $postDefaultHeaders = [
'Content-Type' => 'application/json' 'Content-Type' => 'application/json'
]; ];
/** /**
@ -78,7 +84,7 @@ abstract class AbstractApiRequester implements ApiRequesterInterface
* @var array * @var array
*/ */
protected $putDefaultHeaders = [ protected $putDefaultHeaders = [
'Content-Type' => 'application/json' 'Content-Type' => 'application/json'
]; ];
/** /**
@ -91,25 +97,66 @@ abstract class AbstractApiRequester implements ApiRequesterInterface
/** /**
* @param Client $client * @param Client $client
* @param object|null $caller
*/ */
public function __construct(Client $client, $authToken) public function __construct(Client &$client , ?object $caller)
{ {
$this->client = $client; $this->setClient($client);
$this->authToken = $authToken; $this->setCaller($caller);
$this->setAuthToken($client->getAuthToken());
// Set authorization headers and parameters // Set authorization headers and parameters
$authToken = $client->getAuthToken();
$this->getDefaultParameters["access_token"] = $authToken; $this->getDefaultParameters["access_token"] = $authToken;
$this->postDefaultHeaders["Authorization"] = "token $authToken"; $this->postDefaultHeaders["Authorization"] = "token $authToken";
$this->putDefaultHeaders["Authorization"] = "token $authToken"; $this->putDefaultHeaders["Authorization"] = "token $authToken";
$this->deleteDefaultHeaders["Authorization"] = "token $authToken"; $this->deleteDefaultHeaders["Authorization"] = "token $authToken";
} }
public function getClient() { /**
return $this->client; * Get the gitea client (by reference)
*
* @author Benjamin Blake (sitelease.ca)
*
* @return Client
*/
public function getClient(): Client {
return $this->client;
} }
public function getAuthToken() { /**
return $this->authToken; * Set the gitea client (by reference)
*
* @author Benjamin Blake (sitelease.ca)
* @param Client $client
* @return self
*/
public function setClient(Client &$client): self {
$this->client = $client;
return $this;
}
/**
* Set the authentication token
*
* @author Benjamin Blake (sitelease.ca)
*
* @return string
*/
public function getAuthToken(): string {
return $this->authToken;
}
/**
* Set the authentication token
*
* @author Benjamin Blake (sitelease.ca)
* @param string $authToken
* @return self
*/
public function setAuthToken(string $authToken): self {
$this->authToken = $authToken;
return $this;
} }
/** /**
@ -124,16 +171,16 @@ abstract class AbstractApiRequester implements ApiRequesterInterface
* @return array * @return array
*/ */
public function getDefaultParametersForType($type = "all") { public function getDefaultParametersForType($type = "all") {
if (!$type || $type == "all") { if (!$type || $type == "all") {
return $this->defaultParameters; return $this->defaultParameters;
} else {
$propertyName = $type."DefaultParameters";
if (property_exists(__CLASS__, $propertyName) && is_array($this->$propertyName)) {
return array_merge($this->defaultParameters, $this->$propertyName);
} else { } else {
return []; $propertyName = $type."DefaultParameters";
if (property_exists(__CLASS__, $propertyName) && is_array($this->$propertyName)) {
return array_merge($this->defaultParameters, $this->$propertyName);
} else {
return [];
}
} }
}
} }
/** /**
@ -148,16 +195,16 @@ abstract class AbstractApiRequester implements ApiRequesterInterface
* @return array * @return array
*/ */
public function getDefaultHeadersForType($type = "all") { public function getDefaultHeadersForType($type = "all") {
if (!$type || $type == "all") { if (!$type || $type == "all") {
return $this->defaultHeaders; return $this->defaultHeaders;
} else {
$propertyName = $type."DefaultHeaders";
if (property_exists(__CLASS__, $propertyName) && is_array($this->$propertyName)) {
return array_merge($this->defaultParameters, $this->$propertyName);
} else { } else {
return []; $propertyName = $type."DefaultHeaders";
if (property_exists(__CLASS__, $propertyName) && is_array($this->$propertyName)) {
return array_merge($this->defaultParameters, $this->$propertyName);
} else {
return [];
}
} }
}
} }
/** /**
@ -177,29 +224,29 @@ abstract class AbstractApiRequester implements ApiRequesterInterface
*/ */
public function get($path, array $parameters = array(), $requestHeaders = array(), $debugRequest = false) public function get($path, array $parameters = array(), $requestHeaders = array(), $debugRequest = false)
{ {
$client = $this->getClient(); $client = $this->getClient();
$guzzleClient = $client->getGuzzleClient(); $guzzleClient = $client->getGuzzleClient();
$defaultParameters = $this->getDefaultParametersForType("get"); $defaultParameters = $this->getDefaultParametersForType("get");
$defaultHeaders = $this->getDefaultParametersForType("get"); $defaultHeaders = $this->getDefaultParametersForType("get");
// Create request options array // Create request options array
// and populate it with defaults // and populate it with defaults
$requestOptions = []; $requestOptions = [];
$requestOptions['query'] = $defaultParameters; $requestOptions['query'] = $defaultParameters;
$requestOptions['headers'] = $defaultHeaders; $requestOptions['headers'] = $defaultHeaders;
if ($debugRequest) { if ($debugRequest) {
$requestOptions['debug'] = true; $requestOptions['debug'] = true;
} }
if ($parameters) { if ($parameters) {
$requestOptions['query'] = array_merge($defaultParameters, $parameters); $requestOptions['query'] = array_merge($defaultParameters, $parameters);
} }
if ($requestHeaders) { if ($requestHeaders) {
$requestOptions['headers'] = array_merge($defaultHeaders, $requestHeaders); $requestOptions['headers'] = array_merge($defaultHeaders, $requestHeaders);
} }
return $guzzleClient->request('GET', $path, $requestOptions); return $guzzleClient->request('GET', $path, $requestOptions);
} }
/** /**
@ -210,34 +257,34 @@ abstract class AbstractApiRequester implements ApiRequesterInterface
*/ */
public function post($path, $body, $requestHeaders = array(), $debugRequest = false) public function post($path, $body, $requestHeaders = array(), $debugRequest = false)
{ {
$client = $this->getClient(); $client = $this->getClient();
$guzzleClient = $client->getGuzzleClient(); $guzzleClient = $client->getGuzzleClient();
$defaultHeaders = $this->getDefaultHeadersForType("post"); $defaultHeaders = $this->getDefaultHeadersForType("post");
// Create request options array // Create request options array
// and populate it with defaults // and populate it with defaults
$requestOptions = []; $requestOptions = [];
$requestOptions['headers'] = $defaultHeaders; $requestOptions['headers'] = $defaultHeaders;
$requestOptions['body'] = "{}"; $requestOptions['body'] = "{}";
if ($debugRequest) { if ($debugRequest) {
$requestOptions['debug'] = true; $requestOptions['debug'] = true;
}
if ($body) {
if (is_object($body)) {
$requestOptions['body'] = json_encode($body);
} }
if (is_string($body)) { if ($body) {
$requestOptions['body'] = $body; if (is_object($body)) {
$requestOptions['body'] = json_encode($body);
}
if (is_string($body)) {
$requestOptions['body'] = $body;
}
} }
}
if ($requestHeaders) { if ($requestHeaders) {
$requestOptions['headers'] = array_merge($defaultHeaders, $requestHeaders); $requestOptions['headers'] = array_merge($defaultHeaders, $requestHeaders);
} }
return $guzzleClient->request('POST', $path, $requestOptions); return $guzzleClient->request('POST', $path, $requestOptions);
} }
/** /**
@ -248,34 +295,34 @@ abstract class AbstractApiRequester implements ApiRequesterInterface
*/ */
public function put($path, $body, $requestHeaders = array(), $debugRequest = false) public function put($path, $body, $requestHeaders = array(), $debugRequest = false)
{ {
$client = $this->getClient(); $client = $this->getClient();
$guzzleClient = $client->getGuzzleClient(); $guzzleClient = $client->getGuzzleClient();
$defaultHeaders = $this->getDefaultHeadersForType("put"); $defaultHeaders = $this->getDefaultHeadersForType("put");
// Create request options array // Create request options array
// and populate it with defaults // and populate it with defaults
$requestOptions = []; $requestOptions = [];
$requestOptions['headers'] = $defaultHeaders; $requestOptions['headers'] = $defaultHeaders;
$requestOptions['body'] = "{}"; $requestOptions['body'] = "{}";
if ($debugRequest) { if ($debugRequest) {
$requestOptions['debug'] = true; $requestOptions['debug'] = true;
}
if ($body) {
if (is_object($body)) {
$requestOptions['body'] = json_encode($body);
} }
if (is_string($body)) { if ($body) {
$requestOptions['body'] = $body; if (is_object($body)) {
$requestOptions['body'] = json_encode($body);
}
if (is_string($body)) {
$requestOptions['body'] = $body;
}
} }
}
if ($requestHeaders) { if ($requestHeaders) {
$requestOptions['headers'] = array_merge($defaultHeaders, $requestHeaders); $requestOptions['headers'] = array_merge($defaultHeaders, $requestHeaders);
} }
return $guzzleClient->request('PUT', $path, $requestOptions); return $guzzleClient->request('PUT', $path, $requestOptions);
} }
/** /**
@ -285,22 +332,22 @@ abstract class AbstractApiRequester implements ApiRequesterInterface
*/ */
public function delete($path, $requestHeaders = array(), $debugRequest = false) public function delete($path, $requestHeaders = array(), $debugRequest = false)
{ {
$client = $this->getClient(); $client = $this->getClient();
$guzzleClient = $client->getGuzzleClient(); $guzzleClient = $client->getGuzzleClient();
$defaultHeaders = $this->getDefaultHeadersForType("delete"); $defaultHeaders = $this->getDefaultHeadersForType("delete");
// Create request options array // Create request options array
// and populate it with defaults // and populate it with defaults
$requestOptions = []; $requestOptions = [];
$requestOptions['headers'] = $defaultHeaders; $requestOptions['headers'] = $defaultHeaders;
if ($debugRequest) { if ($debugRequest) {
$requestOptions['debug'] = true; $requestOptions['debug'] = true;
} }
if ($requestHeaders) { if ($requestHeaders) {
$requestOptions['headers'] = array_merge($defaultHeaders, $requestHeaders); $requestOptions['headers'] = array_merge($defaultHeaders, $requestHeaders);
} }
return $guzzleClient->request('DELETE', $path, $requestOptions); return $guzzleClient->request('DELETE', $path, $requestOptions);
} }
} }

View File

@ -19,10 +19,10 @@ class Branches extends AbstractApiRequester
* Example: * Example:
* ``` * ```
* // Get all tags from a repository owned by a user * // Get all tags from a repository owned by a user
* $giteaClient->tags()->fromRepository("username", "test-repository"); * $client->tags()->fromRepository("username", "test-repository");
* *
* // Get all tags from a repository owned by an organization * // Get all tags from a repository owned by an organization
* $giteaClient->tags()->fromRepository("organizationName", "test-repository"); * $client->tags()->fromRepository("organizationName", "test-repository");
* ``` * ```
* *
* @param string $owner The name of the user or organization that owns the repository * @param string $owner The name of the user or organization that owns the repository
@ -46,7 +46,7 @@ class Branches extends AbstractApiRequester
foreach ($jsonItemList as $jsonItem) { foreach ($jsonItemList as $jsonItem) {
$encodedItem = json_encode($jsonItem); $encodedItem = json_encode($jsonItem);
$itemObject = Branch::fromJson( $itemObject = Branch::fromJson(
$this->getClient(), $client,
$this, $this,
json_decode($encodedItem) json_decode($encodedItem)
); );

View File

@ -13,12 +13,46 @@ interface ApiRequesterInterface
{ {
/** /**
* @param Client $client * @param Client $client
* @param object|null $caller
*/ */
public function __construct(Client $client, $authToken); public function __construct(Client &$client , ?object $caller);
public function getClient(); /**
* Get the gitea client (by reference)
*
* @author Benjamin Blake (sitelease.ca)
*
* @return Client
*/
public function getClient(): Client;
public function getAuthToken(); /**
* Set the gitea client (by reference)
*
* @author Benjamin Blake (sitelease.ca)
* @param Client $client
* @return self
*/
public function setClient(Client &$client): self;
/**
* Get the authentication token
*
* @author Benjamin Blake (sitelease.ca)
*
* @return string
*/
public function getAuthToken(): string;
/**
* Set the authentication token
*
* @author Benjamin Blake (sitelease.ca)
* @param string $authToken
* @return self
*/
public function setAuthToken(string $authToken): self;
/** /**
* @return $this * @return $this

View File

@ -18,7 +18,7 @@ class Organizations extends AbstractApiRequester
* *
* Example: * Example:
* ``` * ```
* $giteaClient->organizations()->getByUsername($orgUsername); * $client->organizations()->getByUsername($orgUsername);
* ``` * ```
* *
* @param string $username * @param string $username
@ -34,7 +34,7 @@ class Organizations extends AbstractApiRequester
$statusCode = $response->getStatusCode(); $statusCode = $response->getStatusCode();
$body = $response->getBody(); $body = $response->getBody();
if ($statusCode == 200) { if ($statusCode == 200) {
return Organization::fromJson(json_decode($body)); return Organization::fromJson($client, $this, json_decode($body));
} else { } else {
return $response; return $response;
} }

View File

@ -32,7 +32,7 @@ class Repositories extends AbstractAllApiRequester
* *
* Example: * Example:
* ``` * ```
* $giteaClient->repositories()->search($keyword, $page); * $client->repositories()->search($keyword, $page);
* ``` * ```
* *
* @param string $keyword Keyword to search by * @param string $keyword Keyword to search by
@ -63,7 +63,7 @@ class Repositories extends AbstractAllApiRequester
foreach ($jsonItemList as $jsonItem) { foreach ($jsonItemList as $jsonItem) {
$encodedItem = json_encode($jsonItem); $encodedItem = json_encode($jsonItem);
$itemObject = Repository::fromJson( $itemObject = Repository::fromJson(
$this->getClient(), $client,
$this, $this,
json_decode($encodedItem) json_decode($encodedItem)
); );
@ -83,7 +83,7 @@ class Repositories extends AbstractAllApiRequester
* *
* Example: * Example:
* ``` * ```
* $giteaClient->repositories()->getByName($owner, $repoName); * $client->repositories()->getByName($owner, $repoName);
* ``` * ```
* *
* @param string $owner The owner of the repository * @param string $owner The owner of the repository
@ -99,7 +99,7 @@ class Repositories extends AbstractAllApiRequester
$body = (string) $response->getBody(); $body = (string) $response->getBody();
if ($statusCode == 200) { if ($statusCode == 200) {
return Repository::fromJson( return Repository::fromJson(
$this->getClient(), $client,
$this, $this,
json_decode($body) json_decode($body)
); );
@ -117,7 +117,7 @@ class Repositories extends AbstractAllApiRequester
* *
* Example: * Example:
* ``` * ```
* $giteaClient->repositories()->getRawFile($owner, $repoName, "README.md"); * $client->repositories()->getRawFile($owner, $repoName, "README.md");
* ``` * ```
* *
* @param string $owner The owner of the repository * @param string $owner The owner of the repository
@ -147,7 +147,7 @@ class Repositories extends AbstractAllApiRequester
* *
* Example: * Example:
* ``` * ```
* $giteaClient->repositories()->downloadArchive($owner, $repoName, "master", ".zip"); * $client->repositories()->downloadArchive($owner, $repoName, "master", ".zip");
* ``` * ```
* *
* @param string $owner The owner of the repository * @param string $owner The owner of the repository

View File

@ -19,10 +19,10 @@ class Tags extends AbstractApiRequester
* Example: * Example:
* ``` * ```
* // Get all tags from a repository owned by a user * // Get all tags from a repository owned by a user
* $giteaClient->tags()->fromRepository("username", "test-repository"); * $client->tags()->fromRepository("username", "test-repository");
* *
* // Get all tags from a repository owned by an organization * // Get all tags from a repository owned by an organization
* $giteaClient->tags()->fromRepository("organizationName", "test-repository"); * $client->tags()->fromRepository("organizationName", "test-repository");
* ``` * ```
* *
* @param string $owner The name of the user or organization that owns the repository * @param string $owner The name of the user or organization that owns the repository
@ -46,7 +46,7 @@ class Tags extends AbstractApiRequester
foreach ($jsonItemList as $jsonItem) { foreach ($jsonItemList as $jsonItem) {
$encodedItem = json_encode($jsonItem); $encodedItem = json_encode($jsonItem);
$itemObject = Tag::fromJson( $itemObject = Tag::fromJson(
$this->getClient(), $client,
$this, $this,
json_decode($encodedItem) json_decode($encodedItem)
); );

View File

@ -14,10 +14,15 @@ use Gitea\Api\Repositories;
use Gitea\Api\Branches; use Gitea\Api\Branches;
use Gitea\Api\Tags; use Gitea\Api\Tags;
use \JsonSerializable; // Traits
use Gitea\Core\Traits\RequestChainable;
use Gitea\Core\Interfaces\RequestChainableInterface;
/** Represents a Gitea push event. */ /** Represents a Gitea push event. */
class Client { class Client implements RequestChainableInterface
{
use RequestChainable;
/** /**
* Stores an instance of Guzzle's Client * Stores an instance of Guzzle's Client
@ -148,7 +153,7 @@ class Client {
*/ */
public function repositories() public function repositories()
{ {
return new Repositories($this, $this->getAuthToken()); return new Repositories($this, $this);
} }
/** /**
@ -160,7 +165,7 @@ class Client {
*/ */
public function branches() public function branches()
{ {
return new Branches($this, $this->getAuthToken()); return new Branches($this, $this);
} }
/** /**
@ -172,7 +177,7 @@ class Client {
*/ */
public function tags() public function tags()
{ {
return new Tags($this, $this->getAuthToken()); return new Tags($this, $this);
} }
/** /**
@ -184,7 +189,7 @@ class Client {
*/ */
public function organizations() public function organizations()
{ {
return new Organizations($this, $this->getAuthToken()); return new Organizations($this, $this);
} }
} }

View File

@ -0,0 +1,56 @@
<?php
namespace Gitea\Core\Interfaces;
/**
* Interface that allows the tracking of request heirarchies
*
* @author Benjamin Blake (sitelease.ca)
*/
interface RequestChainableInterface
{
/**
* Get the object that called this method
*
* @author Benjamin Blake (sitelease.ca)
*
* @return object|null
*/
public function getCaller(): ?object;
/**
* Set the object that called this method
*
* @author Benjamin Blake (sitelease.ca)
*
* @return self
*/
public function setCaller($object): self;
/**
* Return the request chain heirarchy
* as an array of objects
*
* This is useful if you need to know
* what objects where called in order to
* get the current object
*
* @author Benjamin Blake (sitelease.ca)
*
* @return array
*/
public function getRequestChain(): array;
/**
* Return the request chain heirarchy
* as a string of class names
*
* This is useful if you need to quickly print out
* a breadcrumb like heirarchy of callers
*
* @author Benjamin Blake (sitelease.ca)
* @return array
*/
public function debugRequestChain(): string;
}

View File

@ -0,0 +1,104 @@
<?php
namespace Gitea\Core\Traits;
use Gitea\Client;
use Gitea\Core\Interfaces\RequestChainableInterface;
/**
* Abstract class that implements the RequestChainable interface
*
* @author Benjamin Blake (sitelease.ca)
*/
trait RequestChainable
{
/**
* The object that called this object
*
* @var string
*/
private $caller;
/**
* Get the object that called this method
*
* @author Benjamin Blake (sitelease.ca)
*
* @return object|null
*/
public function getCaller(): ?object
{
return $this->caller;
}
/**
* Set the object that called this method
*
* @author Benjamin Blake (sitelease.ca)
*
* @return self
*/
public function setCaller($object): self
{
$this->caller = $object;
return $this;
}
/**
* Return the request chain heirarchy
* as an array of objects
*
* This is useful if you need to know
* what objects where called in order to
* get the current object
*
* @author Benjamin Blake (sitelease.ca)
* @return array
*/
public function getRequestChain(): array
{
$requestChain = array();
// While the caller implements the RequestChainableInterface
// loop over it and add it to the request chain array
$caller = $this->getCaller();
while ($caller instanceof RequestChainableInterface) {
$requestChain[] = $caller;
if ($caller && !$caller instanceof Client) {
$caller = $caller->getCaller();
} else {
break;
}
}
return $requestChain;
}
/**
* Return the request chain heirarchy
* as a string of class names
*
* This is useful if you need to quickly print out
* a breadcrumb like heirarchy of callers
*
* @author Benjamin Blake (sitelease.ca)
* @return array
*/
public function debugRequestChain(): string
{
$requestChainDebug = "";
$requestChain = $this->getRequestChain();
while (count($requestChain) > 0) {
$callerObj = array_shift($requestChain);
if (count($requestChain) != 0) {
$requestChainDebug .= get_class($callerObj)." -> ";
} else {
$requestChainDebug .= get_class($callerObj);
}
}
return $requestChainDebug;
}
}

View File

@ -6,27 +6,40 @@ use Gitea\Client;
use Gitea\Api\Interfaces\ApiRequesterInterface; use Gitea\Api\Interfaces\ApiRequesterInterface;
use \stdClass; use \stdClass;
use Gitea\Model\Interfaces\ApiModelInterface;
use \JsonSerializable; use \JsonSerializable;
abstract class AbstractApiModel implements ApiModelInterface, JsonSerializable // Traits
use Gitea\Core\Traits\RequestChainable;
use Gitea\Model\Interfaces\ApiModelInterface;
use Gitea\Core\Interfaces\RequestChainableInterface;
abstract class AbstractApiModel implements ApiModelInterface, JsonSerializable, RequestChainableInterface
{ {
use RequestChainable;
/**
* Creates a new API model object
*
* @author Benjamin Blake (sitelease.ca)
*
* @param object $client The Gitea client that originally made the request for this object's data
* @param object|null $caller The object that called this method
* @param mixed $args The organization visibility.
*/
public function __construct(Client &$client , ?object $caller, ...$args) {
$this->setClient($client);
$this->setCaller($caller);
}
/** /**
* The Gitea client that originally made the * The Gitea client that originally made the
* Api request for this object * Api request for this object
* *
* @var Client * @var Client
*/ */
protected $giteaClient; protected $client;
/**
* The Api request object that created
* this model object
*
* @var ApiRequesterInterface
*/
protected $apiRequester;
/** /**
* Create a new API model object from a JSON map object * Create a new API model object from a JSON map object
@ -34,19 +47,19 @@ abstract class AbstractApiModel implements ApiModelInterface, JsonSerializable
* Example: * Example:
* ``` * ```
* ClassName::fromJson( * ClassName::fromJson(
* $giteaClient, * $client,
* $apiRequester, * $this,
* json_decode($jsonString) * json_decode($jsonString)
* ); * );
* ``` * ```
* *
* @author Benjamin Blake (sitelease.ca) * @author Benjamin Blake (sitelease.ca)
* *
* @param object $giteaClient The Gitea client that originally made the request for this object's data * @param object $client The Gitea client that originally made the request for this object's data
* @param object $apiRequester The Api requester that created this object * @param object|null $caller The object that called this method
* @param object $map A JSON data object * @param object $map A JSON data object
*/ */
static function fromJson(object $giteaClient, object $apiRequester, object $map) { static function fromJson(object &$client , ?object $caller, object $map) {
trigger_error("The abstract 'fromJson()' method must be overwritten"); trigger_error("The abstract 'fromJson()' method must be overwritten");
return false; return false;
} }
@ -65,38 +78,25 @@ abstract class AbstractApiModel implements ApiModelInterface, JsonSerializable
} }
/** /**
* Get the Gitea client that originally made the * Get the gitea client (by reference)
* Api request for this object
* *
* @author Benjamin Blake (sitelease.ca) * @author Benjamin Blake (sitelease.ca)
* *
* @return Client * @return Client
*/ */
public function getGiteaClient() { public function getClient(): Client {
return $this->giteaClient; return $this->client;
}
public function setGiteaClient($object): self {
$this->giteaClient = $object;
return $this;
} }
/** /**
* Get the Api request object that created * Set the gitea client (by reference)
* this model object
* *
* @author Benjamin Blake (sitelease.ca) * @author Benjamin Blake (sitelease.ca)
* * @param Client $client
* @return ApiRequesterInterface * @return self
*/ */
public function getApiRequester() { public function setClient(Client &$client): self {
return $this->apiRequester; $this->client = $client;
}
public function setApiRequester($object): self {
$this->apiRequester = $object;
return $this; return $this;
} }
} }

View File

@ -5,6 +5,7 @@ namespace Gitea\Model;
use GuzzleHttp\Psr7\Uri; use GuzzleHttp\Psr7\Uri;
use Psr\Http\Message\UriInterface; use Psr\Http\Message\UriInterface;
use Gitea\Client;
use Gitea\Model\PayloadCommit; use Gitea\Model\PayloadCommit;
use \InvalidArgumentException; use \InvalidArgumentException;
@ -32,13 +33,12 @@ class Branch extends AbstractApiModel {
/** /**
* Creates a new branch * Creates a new branch
* @param object $giteaClient The Gitea client that originally made the request for this object's data * @param object $client The Gitea client that originally made the request for this object's data
* @param object $apiRequester The Api requester that created this object * @param object|null $caller The object that called this method
* @param string $name The branch name * @param string $name The branch name
*/ */
function __construct(object $giteaClient, object $apiRequester, ...$args) { public function __construct(Client &$client , ?object $caller, ...$args) {
$this->setGiteaClient($giteaClient); parent::__construct($client, $caller, $args);
$this->setApiRequester($apiRequester);
if (count($args) >= 1) { if (count($args) >= 1) {
$name = $args[0]; $name = $args[0];
if (!is_string($name)) { if (!is_string($name)) {
@ -54,20 +54,20 @@ class Branch extends AbstractApiModel {
/** /**
* Creates a new branch from the specified JSON map. * Creates a new branch from the specified JSON map.
* @param object $giteaClient The Gitea client that originally made the request for this object's data * @param object $client The Gitea client that originally made the request for this object's data
* @param object $apiRequester The Api requester that created this object * @param object|null $caller The object that called this method
* @param object $map A JSON map representing an branch. * @param object $map A JSON map representing an branch.
* @return static The instance corresponding to the specified JSON map. * @return static The instance corresponding to the specified JSON map.
*/ */
static function fromJson(object $giteaClient, object $apiRequester, object $map): self { static function fromJson(object &$client , ?object $caller, object $map): self {
return ( return (
new static( new static(
$giteaClient, $client,
$apiRequester, $caller,
isset($map->name) && is_string($map->name) ? $map->name : '' isset($map->name) && is_string($map->name) ? $map->name : ''
) )
) )
->setCommit(isset($map->commit) && is_object($map->commit) ? PayloadCommit::fromJson($giteaClient, $apiRequester, $map->commit) : null) ->setCommit(isset($map->commit) && is_object($map->commit) ? PayloadCommit::fromJson($client, null, $map->commit) : null)
->setProtected(isset($map->protected) && is_bool($map->protected) ? $map->protected : true) ->setProtected(isset($map->protected) && is_bool($map->protected) ? $map->protected : true)
->setCanUserPush(isset($map->user_can_push) && is_bool($map->user_can_push) ? $map->user_can_push : false) ->setCanUserPush(isset($map->user_can_push) && is_bool($map->user_can_push) ? $map->user_can_push : false)
->setUserCanMerge(isset($map->user_can_merge) && is_bool($map->user_can_merge) ? $map->user_can_merge : false); ->setUserCanMerge(isset($map->user_can_merge) && is_bool($map->user_can_merge) ? $map->user_can_merge : false);

View File

@ -15,11 +15,11 @@ interface ApiModelInterface
* *
* @author Benjamin Blake (sitelease.ca) * @author Benjamin Blake (sitelease.ca)
* *
* @param object $giteaClient The Gitea client that originally made the request for this object's data * @param object $client The Gitea client that originally made the request for this object's data
* @param object $apiRequester The Api requester that created this object * @param object|null $caller The object that called this method
* @param mixed $args The organization visibility. * @param mixed $args The organization visibility.
*/ */
public function __construct(object $giteaClient, object $apiRequester, ...$args); public function __construct(Client &$client , ?object $caller, ...$args);
/** /**
* Create a new API model object from a JSON map object * Create a new API model object from a JSON map object
@ -27,19 +27,19 @@ interface ApiModelInterface
* Example: * Example:
* ``` * ```
* ClassName::fromJson( * ClassName::fromJson(
* $giteaClient, * $client,
* $apiRequester, * $this,
* json_decode($jsonString) * json_decode($jsonString)
* ); * );
* ``` * ```
* *
* @author Benjamin Blake (sitelease.ca) * @author Benjamin Blake (sitelease.ca)
* *
* @param object $giteaClient The Gitea client that originally made the request for this object's data * @param object $client The Gitea client that originally made the request for this object's data
* @param object $apiRequester The Api requester that created this object * @param object|null $caller The object that called this method
* @param object $map A JSON data object * @param object $map A JSON data object
*/ */
static function fromJson(object $giteaClient, object $apiRequester, object $map); static function fromJson(object &$client , ?object $caller, object $map);
/** /**
* Convert this Api model object to a JSON map. * Convert this Api model object to a JSON map.
@ -54,24 +54,21 @@ interface ApiModelInterface
public function jsonSerialize(): \stdClass; public function jsonSerialize(): \stdClass;
/** /**
* Get the Gitea client that originally made the * Get the gitea client (by reference)
* Api request for this object
* *
* @author Benjamin Blake (sitelease.ca) * @author Benjamin Blake (sitelease.ca)
* *
* @return Client * @return Client
*/ */
public function getGiteaClient(); public function getClient(): Client;
/** /**
* Get the Api request object that created * Set the gitea client (by reference)
* this model object
* *
* @author Benjamin Blake (sitelease.ca) * @author Benjamin Blake (sitelease.ca)
* * @param Client $client
* @return ApiRequesterInterface * @return self
*/ */
public function getApiRequester(); public function setClient(Client &$client): self;
} }

View File

@ -5,6 +5,8 @@ namespace Gitea\Model;
use GuzzleHttp\Psr7\Uri; use GuzzleHttp\Psr7\Uri;
use Psr\Http\Message\UriInterface; use Psr\Http\Message\UriInterface;
use Gitea\Client;
use \InvalidArgumentException; use \InvalidArgumentException;
use Gitea\Model\Abstracts\AbstractApiModel; use Gitea\Model\Abstracts\AbstractApiModel;
@ -38,14 +40,13 @@ class Organization extends AbstractApiModel {
/** /**
* Creates a new organization. * Creates a new organization.
* @param object $giteaClient The Gitea client that originally made the request for this object's data * @param object $client The Gitea client that originally made the request for this object's data
* @param object $apiRequester The Api requester that created this object * @param object|null $caller The object that called this method
* @param string $username The organization name. * @param string $username The organization name.
* @param string $visibility The organization visibility. * @param string $visibility The organization visibility.
*/ */
function __construct(object $giteaClient, object $apiRequester, ...$args) { public function __construct(Client &$client , ?object $caller, ...$args) {
$this->setGiteaClient($giteaClient); parent::__construct($client, $caller, $args);
$this->setApiRequester($apiRequester);
if (count($args) >= 2) { if (count($args) >= 2) {
$username = $args[0]; $username = $args[0];
$visibility = $args[1]; $visibility = $args[1];
@ -67,14 +68,16 @@ class Organization extends AbstractApiModel {
/** /**
* Creates a new organization from the specified JSON map. * Creates a new organization from the specified JSON map.
* @param object $client The Gitea client that originally made the request for this object's data
* @param object|null $caller The object that called this method
* @param object $map A JSON map representing an organization. * @param object $map A JSON map representing an organization.
* @return static The instance corresponding to the specified JSON map. * @return static The instance corresponding to the specified JSON map.
*/ */
static function fromJson(object $giteaClient, object $apiRequester, object $map): self { static function fromJson(object &$client , ?object $caller, object $map): self {
return ( return (
new static( new static(
$giteaClient, $client,
$apiRequester, $caller,
isset($map->username) && is_string($map->username) ? $map->username : '', isset($map->username) && is_string($map->username) ? $map->username : '',
isset($map->visibility) && is_string($map->visibility) ? $map->visibility : 'private' isset($map->visibility) && is_string($map->visibility) ? $map->visibility : 'private'
) )

View File

@ -5,6 +5,8 @@ namespace Gitea\Model;
use GuzzleHttp\Psr7\Uri; use GuzzleHttp\Psr7\Uri;
use Psr\Http\Message\UriInterface; use Psr\Http\Message\UriInterface;
use Gitea\Client;
use Gitea\Model\Abstracts\AbstractApiModel; use Gitea\Model\Abstracts\AbstractApiModel;
/** Represents a Gitea owner. */ /** Represents a Gitea owner. */
@ -36,14 +38,13 @@ class Owner extends AbstractApiModel {
/** /**
* Creates a new owner. * Creates a new owner.
* @param object $giteaClient The Gitea client that originally made the request for this object's data * @param object $client The Gitea client that originally made the request for this object's data
* @param object $apiRequester The Api requester that created this object * @param object|null $caller The object that called this method
* @param int $id The owner identifier. * @param int $id The owner identifier.
* @param string $login The name of the Gitea account. * @param string $login The name of the Gitea account.
*/ */
function __construct(object $giteaClient, object $apiRequester, ...$args) { public function __construct(Client &$client , ?object $caller, ...$args) {
$this->setGiteaClient($giteaClient); parent::__construct($client, $caller, $args);
$this->setApiRequester($apiRequester);
if (count($args) >= 2) { if (count($args) >= 2) {
$id = $args[0]; $id = $args[0];
$login = $args[1]; $login = $args[1];
@ -65,16 +66,16 @@ class Owner extends AbstractApiModel {
/** /**
* Creates a new owner from the specified JSON map. * Creates a new owner from the specified JSON map.
* @param object $giteaClient The Gitea client that originally made the request for this object's data * @param object $client The Gitea client that originally made the request for this object's data
* @param object $apiRequester The Api requester that created this object * @param object|null $caller The object that called this method
* @param object $map A JSON map representing a owner. * @param object $map A JSON map representing a owner.
* @return static The instance corresponding to the specified JSON map. * @return static The instance corresponding to the specified JSON map.
*/ */
static function fromJson(object $giteaClient, object $apiRequester, object $map): self { static function fromJson(object &$client , ?object $caller, object $map): self {
return ( return (
new static( new static(
$giteaClient, $client,
$apiRequester, $caller,
isset($map->id) && is_int($map->id) ? $map->id : -1, isset($map->id) && is_int($map->id) ? $map->id : -1,
isset($map->login) && is_string($map->login) ? $map->login : '' isset($map->login) && is_string($map->login) ? $map->login : ''
) )

View File

@ -5,6 +5,7 @@ namespace Gitea\Model;
use GuzzleHttp\Psr7\Uri; use GuzzleHttp\Psr7\Uri;
use Psr\Http\Message\UriInterface; use Psr\Http\Message\UriInterface;
use Gitea\Client;
use Gitea\Model\PayloadUser; use Gitea\Model\PayloadUser;
use Gitea\Model\PayloadCommitVerification; use Gitea\Model\PayloadCommitVerification;
@ -38,14 +39,13 @@ class PayloadCommit extends AbstractApiModel {
/** /**
* Creates a new payload commit. * Creates a new payload commit.
* @param object $giteaClient The Gitea client that originally made the request for this object's data * @param object $client The Gitea client that originally made the request for this object's data
* @param object $apiRequester The Api requester that created this object * @param object|null $caller The object that called this method
* @param string $id The commit hash. * @param string $id The commit hash.
* @param string $message The commit message. * @param string $message The commit message.
*/ */
function __construct(object $giteaClient, object $apiRequester, ...$args) { public function __construct(Client &$client , ?object $caller, ...$args) {
$this->setGiteaClient($giteaClient); parent::__construct($client, $caller, $args);
$this->setApiRequester($apiRequester);
if (count($args) >= 2) { if (count($args) >= 2) {
$id = $args[0]; $id = $args[0];
$message = $args[1]; $message = $args[1];
@ -67,25 +67,25 @@ class PayloadCommit extends AbstractApiModel {
/** /**
* Creates a new commit from the specified JSON map. * Creates a new commit from the specified JSON map.
* @param object $giteaClient The Gitea client that originally made the request for this object's data * @param object $client The Gitea client that originally made the request for this object's data
* @param object $apiRequester The Api requester that created this object * @param object|null $caller The object that called this method
* @param object $map A JSON map representing a commit. * @param object $map A JSON map representing a commit.
* @return static The instance corresponding to the specified JSON map. * @return static The instance corresponding to the specified JSON map.
*/ */
static function fromJson(object $giteaClient, object $apiRequester, object $map): self { static function fromJson(object &$client , ?object $caller, object $map): self {
return ( return (
new static( new static(
$giteaClient, $client,
$apiRequester, $caller,
isset($map->id) && is_string($map->id) ? $map->id : '', isset($map->id) && is_string($map->id) ? $map->id : '',
isset($map->message) && is_string($map->message) ? $map->message : '' isset($map->message) && is_string($map->message) ? $map->message : ''
) )
) )
->setAuthor(isset($map->author) && is_object($map->author) ? PayloadUser::fromJson($giteaClient, $apiRequester, $map->author) : null) ->setAuthor(isset($map->author) && is_object($map->author) ? PayloadUser::fromJson($client, null, $map->author) : null)
->setCommitter(isset($map->committer) && is_object($map->committer) ? PayloadUser::fromJson($giteaClient, $apiRequester, $map->committer) : null) ->setCommitter(isset($map->committer) && is_object($map->committer) ? PayloadUser::fromJson($client, null, $map->committer) : null)
->setTimestamp(isset($map->timestamp) && is_string($map->timestamp) ? new \DateTime($map->timestamp) : null) ->setTimestamp(isset($map->timestamp) && is_string($map->timestamp) ? new \DateTime($map->timestamp) : null)
->setUrl(isset($map->url) && is_string($map->url) ? new Uri($map->url) : null) ->setUrl(isset($map->url) && is_string($map->url) ? new Uri($map->url) : null)
->setVerification(isset($map->verification) && is_object($map->verification) ? PayloadCommitVerification::fromJson($giteaClient, $apiRequester, $map->verification) : null); ->setVerification(isset($map->verification) && is_object($map->verification) ? PayloadCommitVerification::fromJson($client, null, $map->verification) : null);
} }
/** /**

View File

@ -5,6 +5,8 @@ namespace Gitea\Model;
use GuzzleHttp\Psr7\Uri; use GuzzleHttp\Psr7\Uri;
use Psr\Http\Message\UriInterface; use Psr\Http\Message\UriInterface;
use Gitea\Client;
use \InvalidArgumentException; use \InvalidArgumentException;
use Gitea\Model\Abstracts\AbstractApiModel; use Gitea\Model\Abstracts\AbstractApiModel;
@ -26,14 +28,12 @@ class PayloadCommitVerification extends AbstractApiModel {
/** /**
* Creates a new verification of a payload commit. * Creates a new verification of a payload commit.
* @param object $giteaClient The Gitea client that originally made the request for this object's data * @param object $client The Gitea client that originally made the request for this object's data
* @param object $apiRequester The Api requester that created this object * @param object|null $caller The object that called this method
* @param bool $isVerified Value indicating whether the verification has succeeded. * @param bool $isVerified Value indicating whether the verification has succeeded.
*/ */
function __construct(object $giteaClient, object $apiRequester, ...$args) { public function __construct(Client &$client , ?object $caller, ...$args) {
parent::__construct($client, $caller, $args);
$this->setGiteaClient($giteaClient);
$this->setApiRequester($apiRequester);
if (count($args) >= 1) { if (count($args) >= 1) {
$isVerified = $args[0]; $isVerified = $args[0];
if (!is_bool($isVerified)) { if (!is_bool($isVerified)) {
@ -49,16 +49,16 @@ class PayloadCommitVerification extends AbstractApiModel {
/** /**
* Creates a new commit from the specified JSON map. * Creates a new commit from the specified JSON map.
* @param object $giteaClient The Gitea client that originally made the request for this object's data * @param object $client The Gitea client that originally made the request for this object's data
* @param object $apiRequester The Api requester that created this object * @param object|null $caller The object that called this method
* @param object $map A JSON map representing a commit. * @param object $map A JSON map representing a commit.
* @return static The instance corresponding to the specified JSON map. * @return static The instance corresponding to the specified JSON map.
*/ */
static function fromJson(object $giteaClient, object $apiRequester, object $map): self { static function fromJson(object &$client , ?object $caller, object $map): self {
return ( return (
new static( new static(
$giteaClient, $client,
$apiRequester, $caller,
isset($map->verified) && is_bool($map->verified) ? $map->verified : false isset($map->verified) && is_bool($map->verified) ? $map->verified : false
) )
) )

View File

@ -5,6 +5,8 @@ namespace Gitea\Model;
use GuzzleHttp\Psr7\Uri; use GuzzleHttp\Psr7\Uri;
use Psr\Http\Message\UriInterface; use Psr\Http\Message\UriInterface;
use Gitea\Client;
use \stdClass; use \stdClass;
use \InvalidArgumentException; use \InvalidArgumentException;
@ -24,13 +26,12 @@ class PayloadUser extends AbstractApiModel {
/** /**
* Creates a new payload user. * Creates a new payload user.
* @param object $giteaClient The Gitea client that originally made the request for this object's data * @param object $client The Gitea client that originally made the request for this object's data
* @param object $apiRequester The Api requester that created this object * @param object|null $caller The object that called this method
* @param string $username The name of the Gitea account. * @param string $username The name of the Gitea account.
*/ */
function __construct(object $giteaClient, object $apiRequester, ...$args) { public function __construct(Client &$client , ?object $caller, ...$args) {
$this->setGiteaClient($giteaClient); parent::__construct($client, $caller, $args);
$this->setApiRequester($apiRequester);
if (count($args) >= 1) { if (count($args) >= 1) {
$username = $args[0]; $username = $args[0];
if (!is_string($username)) { if (!is_string($username)) {
@ -46,16 +47,16 @@ class PayloadUser extends AbstractApiModel {
/** /**
* Creates a new user from the specified JSON map. * Creates a new user from the specified JSON map.
* @param object $giteaClient The Gitea client that originally made the request for this object's data * @param object $client The Gitea client that originally made the request for this object's data
* @param object $apiRequester The Api requester that created this object * @param object|null $caller The object that called this method
* @param object $map A JSON map representing a user. * @param object $map A JSON map representing a user.
* @return static The instance corresponding to the specified JSON map. * @return static The instance corresponding to the specified JSON map.
*/ */
static function fromJson(object $giteaClient, object $apiRequester, object $map): self { static function fromJson(object &$client , ?object $caller, object $map): self {
return ( return (
new static( new static(
$giteaClient, $client,
$apiRequester, $caller,
isset($map->username) && is_string($map->username) ? $map->username : '' isset($map->username) && is_string($map->username) ? $map->username : ''
) )
) )

View File

@ -5,6 +5,8 @@ namespace Gitea\Model;
use GuzzleHttp\Psr7\Uri; use GuzzleHttp\Psr7\Uri;
use Psr\Http\Message\UriInterface; use Psr\Http\Message\UriInterface;
use Gitea\Client;
use \InvalidArgumentException; use \InvalidArgumentException;
use Gitea\Model\Abstracts\AbstractApiModel; use Gitea\Model\Abstracts\AbstractApiModel;
@ -23,15 +25,14 @@ class Permission extends AbstractApiModel {
/** /**
* Creates a new permission. * Creates a new permission.
* @param object $giteaClient The Gitea client that originally made the request for this object's data * @param object $client The Gitea client that originally made the request for this object's data
* @param object $apiRequester The Api requester that created this object * @param object|null $caller The object that called this method
* @param bool $admin Value indicating whether administrator access is allowed. * @param bool $admin Value indicating whether administrator access is allowed.
* @param bool $pull Value indicating whether pull is allowed. * @param bool $pull Value indicating whether pull is allowed.
* @param bool $push Value indicating whether push is allowed. * @param bool $push Value indicating whether push is allowed.
*/ */
function __construct(object $giteaClient, object $apiRequester, ...$args) { public function __construct(Client &$client , ?object $caller, ...$args) {
$this->setGiteaClient($giteaClient); parent::__construct($client, $caller, $args);
$this->setApiRequester($apiRequester);
if (count($args) >= 2) { if (count($args) >= 2) {
$admin = $args[0]; $admin = $args[0];
$pull = $args[1]; $pull = $args[1];
@ -57,15 +58,15 @@ class Permission extends AbstractApiModel {
/** /**
* Creates a new user from the specified JSON map. * Creates a new user from the specified JSON map.
* @param object $giteaClient The Gitea client that originally made the request for this object's data * @param object $client The Gitea client that originally made the request for this object's data
* @param object $apiRequester The Api requester that created this object * @param object|null $caller The object that called this method
* @param object $map A JSON map representing a user. * @param object $map A JSON map representing a user.
* @return static The instance corresponding to the specified JSON map. * @return static The instance corresponding to the specified JSON map.
*/ */
static function fromJson(object $giteaClient, object $apiRequester, object $map): self { static function fromJson(object &$client , ?object $caller, object $map): self {
return new static( return new static(
$giteaClient, $client,
$apiRequester, $caller,
isset($map->admin) && is_bool($map->admin) ? $map->admin : false, isset($map->admin) && is_bool($map->admin) ? $map->admin : false,
isset($map->pull) && is_bool($map->pull) ? $map->pull : false, isset($map->pull) && is_bool($map->pull) ? $map->pull : false,
isset($map->push) && is_bool($map->push) ? $map->push : false isset($map->push) && is_bool($map->push) ? $map->push : false

View File

@ -1,9 +1,11 @@
<?php declare(strict_types=1); <?php declare(strict_types=1);
namespace Gitea\Model; namespace Gitea\Model;
use GuzzleHttp\Psr7\{Uri}; use GuzzleHttp\Psr7\{Uri};
use Psr\Http\Message\{UriInterface}; use Psr\Http\Message\{UriInterface};
use Gitea\Client;
use Gitea\Collections\ApiItemCollection; use Gitea\Collections\ApiItemCollection;
use Gitea\Model\Owner; use Gitea\Model\Owner;
use Gitea\Api\Repositories; use Gitea\Api\Repositories;
@ -88,14 +90,13 @@ class Repository extends AbstractApiModel {
/** /**
* Creates a new repository. * Creates a new repository.
* @param object $giteaClient The Gitea client that originally made the request for this object's data * @param object $client The Gitea client that originally made the request for this object's data
* @param object $apiRequester The Api requester that created this object * @param object|null $caller The object that called this method
* @param int $id The repository identifier. * @param int $id The repository identifier.
* @param string $fullName The full name of the repository. * @param string $fullName The full name of the repository.
*/ */
function __construct(object $giteaClient, object $apiRequester, ...$args) { public function __construct(Client &$client , ?object $caller, ...$args) {
$this->setGiteaClient($giteaClient); parent::__construct($client, $caller, $args);
$this->setApiRequester($apiRequester);
if (count($args) >= 2) { if (count($args) >= 2) {
$id = $args[0]; $id = $args[0];
$fullName = $args[1]; $fullName = $args[1];
@ -117,14 +118,16 @@ class Repository extends AbstractApiModel {
/** /**
* Creates a new repository from the specified JSON map. * Creates a new repository from the specified JSON map.
* @param object $client The Gitea client that originally made the request for this object's data
* @param object|null $caller The object that called this method
* @param object $map A JSON map representing a repository. * @param object $map A JSON map representing a repository.
* @return static The instance corresponding to the specified JSON map. * @return static The instance corresponding to the specified JSON map.
*/ */
static function fromJson(object $giteaClient, object $apiRequester, object $map): self { static function fromJson(object &$client , ?object $caller, object $map): self {
return ( return (
new static( new static(
$giteaClient, $client,
$apiRequester, $caller,
isset($map->id) && is_int($map->id) ? $map->id : -1, isset($map->id) && is_int($map->id) ? $map->id : -1,
isset($map->full_name) && is_string($map->full_name) ? $map->full_name : '' isset($map->full_name) && is_string($map->full_name) ? $map->full_name : ''
) )
@ -140,9 +143,9 @@ class Repository extends AbstractApiModel {
->setMirror(isset($map->mirror) && is_bool($map->mirror) ? $map->mirror : false) ->setMirror(isset($map->mirror) && is_bool($map->mirror) ? $map->mirror : false)
->setName(isset($map->name) && is_string($map->name) ? $map->name : '') ->setName(isset($map->name) && is_string($map->name) ? $map->name : '')
->setOpenIssuesCount(isset($map->open_issues_count) && is_int($map->open_issues_count) ? $map->open_issues_count : 0) ->setOpenIssuesCount(isset($map->open_issues_count) && is_int($map->open_issues_count) ? $map->open_issues_count : 0)
->setOwner(isset($map->owner) && is_object($map->owner) ? Owner::fromJson($giteaClient, $apiRequester, $map->owner) : null) ->setOwner(isset($map->owner) && is_object($map->owner) ? Owner::fromJson($client, null, $map->owner) : null)
->setParent(isset($map->parent) && is_object($map->parent) ? Repository::fromJson($giteaClient, $apiRequester, $map->parent) : null) ->setParent(isset($map->parent) && is_object($map->parent) ? Repository::fromJson($client, null, $map->parent) : null)
->setPermissions(isset($map->permissions) && is_object($map->permissions) ? Permission::fromJson($giteaClient, $apiRequester, $map->permissions) : null) ->setPermissions(isset($map->permissions) && is_object($map->permissions) ? Permission::fromJson($client, null, $map->permissions) : null)
->setPrivate(isset($map->private) && is_bool($map->private) ? $map->private : false) ->setPrivate(isset($map->private) && is_bool($map->private) ? $map->private : false)
->setSize(isset($map->size) && is_int($map->size) ? $map->size : 0) ->setSize(isset($map->size) && is_int($map->size) ? $map->size : 0)
->setSshUrl(isset($map->ssh_url) && is_string($map->ssh_url) ? new Uri($map->ssh_url) : null) ->setSshUrl(isset($map->ssh_url) && is_string($map->ssh_url) ? new Uri($map->ssh_url) : null)
@ -602,8 +605,8 @@ class Repository extends AbstractApiModel {
{ {
$owner = $this->getOwner(); $owner = $this->getOwner();
if ($owner) { if ($owner) {
$client = $this->getGiteaClient(); $client = $this->getClient();
$repositoriesApi = new Repositories($client, $client->getAuthToken()); $repositoriesApi = new Repositories($client, $this);
return $repositoriesApi->downloadArchive($owner->getUsername(), $this->getName(), $gitRef, $format); return $repositoriesApi->downloadArchive($owner->getUsername(), $this->getName(), $gitRef, $format);
} }
} }
@ -619,8 +622,8 @@ class Repository extends AbstractApiModel {
{ {
$owner = $this->getOwner(); $owner = $this->getOwner();
if ($owner) { if ($owner) {
$client = $this->getGiteaClient(); $client = $this->getClient();
$branchesApi = new Branches($client, $client->getAuthToken()); $branchesApi = new Branches($client, $this);
return $branchesApi->fromRepository($owner->getUsername(), $this->getName()); return $branchesApi->fromRepository($owner->getUsername(), $this->getName());
} }
} }
@ -636,8 +639,8 @@ class Repository extends AbstractApiModel {
{ {
$owner = $this->getOwner(); $owner = $this->getOwner();
if ($owner) { if ($owner) {
$client = $this->getGiteaClient(); $client = $this->getClient();
$tagsApi = new Tags($client, $client->getAuthToken()); $tagsApi = new Tags($client, $this);
return $tagsApi->fromRepository($owner->getUsername(), $this->getName()); return $tagsApi->fromRepository($owner->getUsername(), $this->getName());
} }
} }

View File

@ -5,6 +5,8 @@ namespace Gitea\Model;
use GuzzleHttp\Psr7\Uri; use GuzzleHttp\Psr7\Uri;
use Psr\Http\Message\UriInterface; use Psr\Http\Message\UriInterface;
use Gitea\Client;
use \InvalidArgumentException; use \InvalidArgumentException;
use Gitea\Model\Abstracts\AbstractApiModel; use Gitea\Model\Abstracts\AbstractApiModel;
@ -32,14 +34,13 @@ class Tag extends AbstractApiModel {
/** /**
* Creates a new tag * Creates a new tag
* @param object $giteaClient The Gitea client that originally made the request for this object's data * @param object $client The Gitea client that originally made the request for this object's data
* @param object $apiRequester The Api requester that created this object * @param object|null $caller The object that called this method
* @param int $id The tag identifier * @param int $id The tag identifier
* @param string $name The tag name * @param string $name The tag name
*/ */
function __construct(object $giteaClient, object $apiRequester, ...$args) { public function __construct(Client &$client , ?object $caller, ...$args) {
$this->setGiteaClient($giteaClient); parent::__construct($client, $caller, $args);
$this->setApiRequester($apiRequester);
if (count($args) >= 2) { if (count($args) >= 2) {
$id = $args[0]; $id = $args[0];
$name = $args[1]; $name = $args[1];
@ -61,15 +62,15 @@ class Tag extends AbstractApiModel {
/** /**
* Creates a new tag from the specified JSON map. * Creates a new tag from the specified JSON map.
* @param object $giteaClient The Gitea client that originally made the request for this object's data * @param object $client The Gitea client that originally made the request for this object's data
* @param object $apiRequester The Api requester that created this object * @param object|null $caller The object that called this method
* @param object $map A JSON map representing an tag. * @param object $map A JSON map representing an tag.
* @return static The instance corresponding to the specified JSON map. * @return static The instance corresponding to the specified JSON map.
*/ */
static function fromJson(object $giteaClient, object $apiRequester, object $map): self { static function fromJson(object &$client , ?object $caller, object $map): self {
$newTag = new static( $newTag = new static(
$giteaClient, $client,
$apiRequester, $caller,
isset($map->id) && is_int($map->id) ? $map->id : -1, isset($map->id) && is_int($map->id) ? $map->id : -1,
isset($map->name) && is_string($map->name) ? $map->name : '' isset($map->name) && is_string($map->name) ? $map->name : ''
); );

View File

@ -43,7 +43,8 @@ $giteaClient->setAuthToken('32e609ad39539c1d0e8544800bd49b1025ac6b49');
// $tags = $repository->tags(); // $tags = $repository->tags();
// if ($tags && count($tags) > 0) { // if ($tags && count($tags) > 0) {
// foreach ($tags as $tag) { // foreach ($tags as $tag) {
// var_dump(json_encode($tag)); // print("Processing ".$tag->getName()."\n");
// // var_dump(json_encode($tag));
// print("\n\n"); // print("\n\n");
// } // }
// print("Total Tags: ".count($tags)."\n"); // print("Total Tags: ".count($tags)."\n");
@ -52,20 +53,25 @@ $giteaClient->setAuthToken('32e609ad39539c1d0e8544800bd49b1025ac6b49');
// print("The repository could not be retrieved"."\n"); // print("The repository could not be retrieved"."\n");
// } // }
// print("Getting all branches for the 'sl-product-catalog' repository \n\n"); print("Getting all branches for the 'sl-product-catalog' repository \n\n");
// $repository = $giteaClient->repositories()->getByName("Sitelease", "sl-theme-recipe"); $repository = $giteaClient->repositories()->getByName("Sitelease", "sl-theme-recipe");
// if ($repository) { if ($repository) {
// $branches = $repository->branches(); $branches = $repository->branches();
// if ($branches && count($branches) > 0) { if ($branches && count($branches) > 0) {
// //foreach ($branches as $branch) { foreach ($branches as $branch) {
// var_dump($branches->getItem("master")); // var_dump(json_encode($branch));
// print("\n\n"); print("Processing ".$branch->getName()."\n");
// //} $debugChain = $branch->debugRequestChain();
// print("Total Branches: ".count($branches)."\n"); if ($debugChain) {
// } print("Chain ".$debugChain."\n");
// } else { }
// print("The repository could not be retrieved"."\n"); print("\n\n");
// } }
print("Total Branches: ".count($branches)."\n");
}
} else {
print("The repository could not be retrieved"."\n");
}
// print("Getting contents of \"composer.json\" file \n\n"); // print("Getting contents of \"composer.json\" file \n\n");
// $rawFile = $giteaClient->repositories()->getRawFile("Sitelease", "sl-theme-recipe", "composer.json"); // $rawFile = $giteaClient->repositories()->getRawFile("Sitelease", "sl-theme-recipe", "composer.json");
@ -94,17 +100,17 @@ $giteaClient->setAuthToken('32e609ad39539c1d0e8544800bd49b1025ac6b49');
// print("The repository could not be retrieved"."\n"); // print("The repository could not be retrieved"."\n");
// } // }
print("Getting archive for the 'sl-product-catalog' repository \n\n"); // print("Getting archive for the 'sl-product-catalog' repository \n\n");
$repository = $giteaClient->repositories()->getByName("Sitelease", "sl-theme-recipe"); // $repository = $giteaClient->repositories()->getByName("Sitelease", "sl-theme-recipe");
if ($repository) { // if ($repository) {
$archive = $repository->archive("master"); // $archive = $repository->archive("master");
if ($archive) { // if ($archive) {
print("Downloading Archive...\n"); // print("Downloading Archive...\n");
file_put_contents("master.tar.gz", $archive); // file_put_contents("master.tar.gz", $archive);
print("Success!\n"); // print("Success!\n");
} // }
} else { // } else {
print("The archive could not be retrieved"."\n"); // print("The archive could not be retrieved"."\n");
} // }
print("Exiting script"."\n"); print("Exiting script"."\n");