diff --git a/.gitignore b/.gitignore index 57872d0..0dca145 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /vendor/ +.idea diff --git a/composer.json b/composer.json index 6278274..b6bcd51 100644 --- a/composer.json +++ b/composer.json @@ -10,14 +10,15 @@ } ], "autoload": { - "classmap": [ - "src/" - ] + "psr-4": { + "Gorse\\": "src/" + } }, "require-dev": { "phpunit/phpunit": "^9.3" }, "require": { + "php": "~8.2.0 || ~8.3.0", "guzzlehttp/guzzle": "^7.0", "ext-redis": "*" } diff --git a/src/Feedback.php b/src/Feedback.php new file mode 100644 index 0000000..cbd2de4 --- /dev/null +++ b/src/Feedback.php @@ -0,0 +1,31 @@ + $this->feedbackType, + 'UserId' => $this->userId, + 'ItemId' => $this->itemId, + 'Timestamp' => $this->timestamp, + ]; + + if ($this->comment) { + $result['Comment'] = $this->comment; + } + + return $result; + } +} \ No newline at end of file diff --git a/src/Gorse.php b/src/Gorse.php index cd16322..fef658c 100644 --- a/src/Gorse.php +++ b/src/Gorse.php @@ -1,79 +1,15 @@ -userId = $userId; - $this->labels = $labels; - } - - public function jsonSerialize(): array - { - return [ - 'UserId' => $this->userId, - 'Labels' => $this->labels, - ]; - } - - public static function fromJSON($json): User - { - return new User($json->UserId, $json->Labels); - } -} - -class Feedback implements JsonSerializable -{ - public string $feedback_type; - public string $user_id; - public string $item_id; - public string $timestamp; - - public function __construct(string $feedback_type, string $user_id, string $item_id, string $timestamp) - { - $this->feedback_type = $feedback_type; - $this->user_id = $user_id; - $this->item_id = $item_id; - $this->timestamp = $timestamp; - } - - public function jsonSerialize(): array - { - return [ - 'FeedbackType' => $this->feedback_type, - 'UserId' => $this->user_id, - 'ItemId' => $this->item_id, - 'Timestamp' => $this->timestamp, - ]; - } -} - -class RowAffected -{ - public int $rowAffected; +namespace Gorse; - public static function fromJSON($json): RowAffected - { - $rowAffected = new RowAffected(); - $rowAffected->rowAffected = $json->RowAffected; - return $rowAffected; - } -} +use GuzzleHttp\Exception\GuzzleException; -final class Gorse +final readonly class Gorse { - private string $endpoint; - private string $apiKey; - - function __construct(string $endpoint, string $apiKey) - { - $this->endpoint = $endpoint; - $this->apiKey = $apiKey; + function __construct( + private string $endpoint, + private string $apiKey + ) { } /** @@ -87,39 +23,44 @@ function insertUser(User $user): RowAffected /** * @throws GuzzleException */ - function getUser(string $user_id): User + function getUser(string $userId): User { - return User::fromJSON($this->request('GET', '/api/user/' . $user_id, null)); + return User::fromJSON($this->request('GET', '/api/user/' . $userId)); } /** * @throws GuzzleException */ - function deleteUser(string $user_id): RowAffected + function deleteUser(string $userId): RowAffected { - return RowAffected::fromJSON($this->request('DELETE', '/api/user/' . $user_id, null)); + return RowAffected::fromJSON($this->request('DELETE', '/api/user/' . $userId)); } /** + * @param Feedback|Feedback[] $feedback * @throws GuzzleException */ - function insertFeedback(array $feedback): RowAffected + function insertFeedback(mixed $feedback): RowAffected { + if ($feedback instanceof Feedback) { + $feedback = [$feedback]; + } + return RowAffected::fromJSON($this->request('POST', '/api/feedback/', $feedback)); } /** * @throws GuzzleException */ - function getRecommend(string $user_id): array + function getRecommend(string $userId): array { - return $this->request('GET', '/api/recommend/' . $user_id, null); + return $this->request('GET', '/api/recommend/' . $userId); } /** * @throws GuzzleException */ - private function request(string $method, string $uri, $body) + private function request(string $method, string $uri, \JsonSerializable|array|null $body = null) { $client = new GuzzleHttp\Client(['base_uri' => $this->endpoint]); $options = [GuzzleHttp\RequestOptions::HEADERS => ['X-API-Key' => $this->apiKey]]; @@ -127,6 +68,7 @@ private function request(string $method, string $uri, $body) $options[GuzzleHttp\RequestOptions::JSON] = $body; } $response = $client->request($method, $uri, $options); + return json_decode($response->getBody()); } } \ No newline at end of file diff --git a/src/RowAffected.php b/src/RowAffected.php new file mode 100644 index 0000000..6d3718d --- /dev/null +++ b/src/RowAffected.php @@ -0,0 +1,16 @@ +RowAffected); + } +} \ No newline at end of file diff --git a/src/User.php b/src/User.php new file mode 100644 index 0000000..152e0dd --- /dev/null +++ b/src/User.php @@ -0,0 +1,26 @@ + $this->userId, + 'Labels' => $this->labels, + ]; + } + + public static function fromJSON($json): User + { + return new User($json->UserId, $json->Labels); + } +} diff --git a/test/GorseTest.php b/test/GorseTest.php index 25f6469..a76e32b 100644 --- a/test/GorseTest.php +++ b/test/GorseTest.php @@ -1,5 +1,8 @@