Skip to content

Commit

Permalink
Merge 94a6e03 into 473d351
Browse files Browse the repository at this point in the history
  • Loading branch information
ssitdikov committed Jun 19, 2018
2 parents 473d351 + 94a6e03 commit 4d22355
Show file tree
Hide file tree
Showing 34 changed files with 267 additions and 178 deletions.
21 changes: 10 additions & 11 deletions examples/example.php
@@ -1,27 +1,26 @@
<?php

use SSitdikov\TelegraphAPI\Client\TelegraphClient;
use SSitdikov\TelegraphAPI\Request\{
CreateAccountRequest, EditAccountInfoRequest
};
use SSitdikov\TelegraphAPI\Request\CreateAccountRequest;
use SSitdikov\TelegraphAPI\Request\EditAccountInfoRequest;
use SSitdikov\TelegraphAPI\Request\{
CreatePageRequest
};
use SSitdikov\TelegraphAPI\Type\{
Account, Page
};
use SSitdikov\TelegraphAPI\Type\ContentType\{
ImageType, LinkType, ParagraphType
};
use SSitdikov\TelegraphAPI\Type\Account;
use SSitdikov\TelegraphAPI\Type\ContentType\ImageType;
use SSitdikov\TelegraphAPI\Type\ContentType\LinkType;
use SSitdikov\TelegraphAPI\Type\ContentType\ParagraphType;
use SSitdikov\TelegraphAPI\Type\Page;

require __DIR__ . '/../vendor/autoload.php';
require __DIR__.'/../vendor/autoload.php';

$client = new \GuzzleHttp\Client(['base_uri' => 'https://api.telegra.ph/']);

$telegraph = new TelegraphClient($client);

$account = new Account();
$account->setShortName('Test.Account');

try {
$account = $telegraph->createAccount(
new CreateAccountRequest($account)
Expand Down Expand Up @@ -56,4 +55,4 @@
);
} catch (\Exception $e) {
// logger
}
}
4 changes: 3 additions & 1 deletion src/Client/AbstractClient.php
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace SSitdikov\TelegraphAPI\Client;

Expand Down
4 changes: 3 additions & 1 deletion src/Client/ClientInterface.php
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace SSitdikov\TelegraphAPI\Client;

Expand Down
5 changes: 4 additions & 1 deletion src/Client/TelegraphClient.php
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace SSitdikov\TelegraphAPI\Client;

Expand Down Expand Up @@ -42,6 +44,7 @@ private function doRequest(RequestInterface $request)
$request->getUrlRequest(),
$request->getParams()
);

return $request->handleResponse($response);
}

Expand Down
6 changes: 4 additions & 2 deletions src/Exception/ContentTextRequired.php
@@ -1,12 +1,14 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace SSitdikov\TelegraphAPI\Exception;

use Throwable;

class ContentTextRequired extends \Exception
{
public function __construct(string $message = "Content text is required", int $code = 0, Throwable $previous = null)
public function __construct(string $message = 'Content text is required', int $code = 0, Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}
Expand Down
6 changes: 4 additions & 2 deletions src/Exception/ShortNameRequiredException.php
@@ -1,12 +1,14 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace SSitdikov\TelegraphAPI\Exception;

use Throwable;

class ShortNameRequiredException extends \Exception
{
public function __construct(string $message = "Short name is required", int $code = 0, Throwable $previous = null)
public function __construct(string $message = 'Short name is required', int $code = 0, Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}
Expand Down
4 changes: 3 additions & 1 deletion src/Request/AbstractAccountRequest.php
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace SSitdikov\TelegraphAPI\Request;

Expand Down
4 changes: 3 additions & 1 deletion src/Request/AbstractPageRequest.php
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace SSitdikov\TelegraphAPI\Request;

Expand Down
12 changes: 9 additions & 3 deletions src/Request/CreateAccountRequest.php
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace SSitdikov\TelegraphAPI\Request;

Expand Down Expand Up @@ -29,26 +31,30 @@ public function getParams(): array
}

/**
* @param ResponseInterface $response
* @param ResponseInterface $response
*
* @throws ShortNameRequiredException
* @throws \Exception
*
* @return Account
*/
public function handleResponse(ResponseInterface $response): Account
{
$json = json_decode($response->getBody()->getContents());
if ($json->ok === false && isset($json->error)) {
switch ($json->error) {
case ('SHORT_NAME_REQUIRED'):
case 'SHORT_NAME_REQUIRED':
throw new ShortNameRequiredException();
}

throw new \Exception($json->error);
}
$this->account->setShortName($json->result->short_name);
$this->account->setAuthorName($json->result->author_name);
$this->account->setAuthorUrl($json->result->author_url);
$this->account->setAuthUrl($json->result->auth_url);
$this->account->setAccessToken($json->result->access_token);

return $this->account;
}
}
16 changes: 11 additions & 5 deletions src/Request/CreatePageRequest.php
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace SSitdikov\TelegraphAPI\Request;

Expand All @@ -17,8 +19,8 @@ public function getParams(): array
{
$params = [
'access_token' => $this->account->getAccessToken(),
'title' => $this->page->getTitle(),
'content' => $this->page->getContent(),
'title' => $this->page->getTitle(),
'content' => $this->page->getContent(),
];
if ($this->page->getAuthorName()) {
$params['author_name'] = $this->page->getAuthorName();
Expand All @@ -29,21 +31,24 @@ public function getParams(): array
if ($this->returnContent) {
$params['return_content'] = true;
}

return ['json' => $params];
}

/**
* @param ResponseInterface $response
* @param ResponseInterface $response
*
* @throws ContentTextRequired
* @throws \Exception
*
* @return \SSitdikov\TelegraphAPI\Type\Page
*/
public function handleResponse(ResponseInterface $response): Page
{
$json = json_decode($response->getBody()->getContents());
if ($json->ok === false && isset($json->error)) {
switch ($json->error) {
case ('CONTENT_TEXT_REQUIRED'):
case 'CONTENT_TEXT_REQUIRED':
throw new ContentTextRequired();
default:
throw new \Exception($json->error);
Expand All @@ -60,6 +65,7 @@ public function handleResponse(ResponseInterface $response): Page
if ($this->getReturnContent()) {
$this->page->setContent($json->result->content);
}

return $this->page;
}
}
9 changes: 7 additions & 2 deletions src/Request/EditAccountInfoRequest.php
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace SSitdikov\TelegraphAPI\Request;

Expand Down Expand Up @@ -34,8 +36,10 @@ public function getParams(): array
}

/**
* @param ResponseInterface $response
* @param ResponseInterface $response
*
* @throws \Exception
*
* @return Account
*/
public function handleResponse(ResponseInterface $response): Account
Expand All @@ -47,6 +51,7 @@ public function handleResponse(ResponseInterface $response): Account
$this->account->setAuthorName($json->result->author_name);
$this->account->setAuthorUrl($json->result->author_url);
$this->account->setShortName($json->result->short_name);

return $this->account;
}
}
10 changes: 7 additions & 3 deletions src/Request/EditPageRequest.php
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace SSitdikov\TelegraphAPI\Request;

Expand All @@ -16,8 +18,8 @@ public function getParams(): array
{
$params = [
'access_token' => $this->account->getAccessToken(),
'title' => $this->page->getTitle(),
'content' => $this->page->getContent(),
'title' => $this->page->getTitle(),
'content' => $this->page->getContent(),
];
if ($this->page->getAuthorName()) {
$params['author_name'] = $this->page->getAuthorName();
Expand All @@ -28,6 +30,7 @@ public function getParams(): array
if ($this->returnContent) {
$params['return_content'] = true;
}

return ['json' => $params];
}

Expand All @@ -51,6 +54,7 @@ public function handleResponse(ResponseInterface $response): Page
if ($this->getReturnContent()) {
$this->page->setContent($json->result->content);
}

return $this->page;
}
}
11 changes: 8 additions & 3 deletions src/Request/GetAccountInfoRequest.php
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace SSitdikov\TelegraphAPI\Request;

Expand All @@ -23,15 +25,17 @@ public function getParams(): array
{
$params = [
'access_token' => $this->account->getAccessToken(),
'fields' => $this->fields,
'fields' => $this->fields,
];

return ['json' => $params];
}

/**
* @param ResponseInterface $response
* @param ResponseInterface $response
*
* @throws \Exception
*
* @return Account
*/
public function handleResponse(ResponseInterface $response): Account
Expand All @@ -57,6 +61,7 @@ public function handleResponse(ResponseInterface $response): Account
$account->setPageCount($json->result->page_count);
}
$account->setAccessToken($this->account->getAccessToken());

return $account;
}
}
16 changes: 12 additions & 4 deletions src/Request/GetPageListRequest.php
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace SSitdikov\TelegraphAPI\Request;

Expand All @@ -15,9 +17,11 @@ class GetPageListRequest implements RequestInterface

/**
* GetPageListRequest constructor.
*
* @param Account $account
* @param int $offset
* @param int $limit
*
* @todo add additional checks for limits
*/
public function __construct(Account $account, $offset = 0, $limit = 50)
Expand All @@ -44,15 +48,18 @@ public function getParams(): array
{
$params = [
'access_token' => $this->account->getAccessToken(),
'offset' => $this->offset,
'limit' => $this->limit,
'offset' => $this->offset,
'limit' => $this->limit,
];

return ['json' => $params];
}

/**
* @param ResponseInterface $response
* @param ResponseInterface $response
*
* @throws \Exception
*
* @return PageList
*/
public function handleResponse(ResponseInterface $response): PageList
Expand All @@ -64,6 +71,7 @@ public function handleResponse(ResponseInterface $response): PageList
$pageList = new PageList();
$pageList->setTotalCount($json->result->total_count);
$pageList->setPages($json->result->pages);

return $pageList;
}
}
6 changes: 5 additions & 1 deletion src/Request/GetPageRequest.php
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace SSitdikov\TelegraphAPI\Request;

Expand All @@ -17,6 +19,7 @@ public function getParams(): array
$params = [
'return_content' => $this->returnContent,
];

return ['json' => $params];
}

Expand All @@ -40,6 +43,7 @@ public function handleResponse(ResponseInterface $response): Page
if ($this->getReturnContent()) {
$this->page->setContent($json->result->content);
}

return $this->page;
}
}

0 comments on commit 4d22355

Please sign in to comment.