-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
433 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# SimonDevelop\Strapi\CollectionType references | ||
|
||
## __constructor(SimonDevelop\Strapi\Setup $setup) | ||
```php | ||
$collection = new CollectionType($setup); | ||
``` | ||
|
||
## getSetup(): SimonDevelop\Strapi\Setup | ||
### Get Setup object | ||
```php | ||
$setup = $collection->getSetup(); | ||
``` | ||
|
||
## setSetup(SimonDevelop\Strapi\Setup $setup): self | ||
### Define Setup object | ||
```php | ||
$collection->setSetup($setup); | ||
``` | ||
|
||
## get(string $collection): array|string | ||
### CollectionType data (response strapi) if the request is passed, else information in string | ||
```php | ||
$response = $collection->get('users'); | ||
``` | ||
|
||
## getById(string $collection, int $id): array|string | ||
### CollectionType data item by id (response strapi) if the request is passed, else information in string | ||
```php | ||
$response = $collection->get('users', 1); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# SimonDevelop\Strapi\SingleType references | ||
|
||
## __constructor(SimonDevelop\Strapi\Setup $setup) | ||
```php | ||
$single = new SingleType($setup); | ||
``` | ||
|
||
## getSetup(): SimonDevelop\Strapi\Setup | ||
### Get Setup object | ||
```php | ||
$setup = $single->getSetup(); | ||
``` | ||
|
||
## setSetup(SimonDevelop\Strapi\Setup $setup): self | ||
### Define Setup object | ||
```php | ||
$single->setSetup($setup); | ||
``` | ||
|
||
## get(string $single): array|string | ||
### SingleType data (response strapi) if the request is passed, else information in string | ||
```php | ||
$response = $single->get('homepage'); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
<?php | ||
|
||
/* | ||
* This file is the strapi-api package. | ||
* | ||
* (c) Simon Micheneau <contact@simon-micheneau.fr> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace SimonDevelop\Strapi; | ||
|
||
use SimonDevelop\Strapi\Setup; | ||
use GuzzleHttp\Client; | ||
use GuzzleHttp\Exception\ClientException; | ||
use GuzzleHttp\Exception\ConnectException; | ||
|
||
class CollectionType | ||
{ | ||
/** | ||
* @var Setup | ||
*/ | ||
private Setup $setup; | ||
|
||
/** | ||
* @var Client | ||
*/ | ||
private Client $client; | ||
|
||
/** | ||
* @param Setup $setup | ||
*/ | ||
public function __construct(Setup $setup) | ||
{ | ||
$this->setup = $setup; | ||
$this->client = new Client(); | ||
} | ||
|
||
/** | ||
* @param string $collection | ||
* @return array|string | ||
*/ | ||
public function get(string $collection) | ||
{ | ||
// Function tested with mock | ||
try { | ||
// @codeCoverageIgnoreStart | ||
$response = $this->client->request('GET', $this->setup->getUrl() . "/$collection", [ | ||
'headers' => [ | ||
'authorization' => 'Bearer '.$this->setup->getToken() | ||
] | ||
]); | ||
|
||
return json_decode($response->getBody()->getContents(), true); | ||
// @codeCoverageIgnoreEnd | ||
} catch (ClientException $e) { | ||
// @codeCoverageIgnoreStart | ||
$json = json_decode($e->getResponse()->getBody()->getContents(), true); | ||
|
||
return $json['error']['message']; | ||
// @codeCoverageIgnoreEnd | ||
} catch (ConnectException $e) { | ||
return $e->getMessage(); | ||
} | ||
} | ||
|
||
/** | ||
* @param string $collection | ||
* @param int $id | ||
* @return array|string | ||
*/ | ||
public function getById(string $collection, int $id) | ||
{ | ||
// Function tested with mock | ||
try { | ||
// @codeCoverageIgnoreStart | ||
$response = $this->client->request('GET', $this->setup->getUrl() . "/$collection/$id", [ | ||
'headers' => [ | ||
'authorization' => 'Bearer '.$this->setup->getToken() | ||
] | ||
]); | ||
|
||
return json_decode($response->getBody()->getContents(), true); | ||
// @codeCoverageIgnoreEnd | ||
} catch (ClientException $e) { | ||
// @codeCoverageIgnoreStart | ||
$json = json_decode($e->getResponse()->getBody()->getContents(), true); | ||
|
||
return $json['error']['message']; | ||
// @codeCoverageIgnoreEnd | ||
} catch (ConnectException $e) { | ||
return $e->getMessage(); | ||
} | ||
} | ||
|
||
/** | ||
* @return Setup Current setup | ||
*/ | ||
public function getSetup(): Setup | ||
{ | ||
return $this->setup; | ||
} | ||
|
||
/** | ||
* @param Setup $setup | ||
* @return CollectionType | ||
*/ | ||
public function setSetup(Setup $setup): self | ||
{ | ||
$this->setup = $setup; | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
|
||
/* | ||
* This file is the strapi-api package. | ||
* | ||
* (c) Simon Micheneau <contact@simon-micheneau.fr> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace SimonDevelop\Strapi; | ||
|
||
use SimonDevelop\Strapi\Setup; | ||
use GuzzleHttp\Client; | ||
use GuzzleHttp\Exception\ClientException; | ||
use GuzzleHttp\Exception\ConnectException; | ||
|
||
class SingleType | ||
{ | ||
/** | ||
* @var Setup | ||
*/ | ||
private Setup $setup; | ||
|
||
/** | ||
* @var Client | ||
*/ | ||
private Client $client; | ||
|
||
/** | ||
* @param Setup $setup | ||
*/ | ||
public function __construct(Setup $setup) | ||
{ | ||
$this->setup = $setup; | ||
$this->client = new Client(); | ||
} | ||
|
||
/** | ||
* @param string $single | ||
* @return array|string | ||
*/ | ||
public function get(string $single) | ||
{ | ||
// Function tested with mock | ||
try { | ||
// @codeCoverageIgnoreStart | ||
$response = $this->client->request('GET', $this->setup->getUrl() . "/$single", [ | ||
'headers' => [ | ||
'authorization' => 'Bearer '.$this->setup->getToken() | ||
] | ||
]); | ||
|
||
return json_decode($response->getBody()->getContents(), true); | ||
// @codeCoverageIgnoreEnd | ||
} catch (ClientException $e) { | ||
// @codeCoverageIgnoreStart | ||
$json = json_decode($e->getResponse()->getBody()->getContents(), true); | ||
|
||
return $json['error']['message']; | ||
// @codeCoverageIgnoreEnd | ||
} catch (ConnectException $e) { | ||
return $e->getMessage(); | ||
} | ||
} | ||
|
||
/** | ||
* @return Setup Current setup | ||
*/ | ||
public function getSetup(): Setup | ||
{ | ||
return $this->setup; | ||
} | ||
|
||
/** | ||
* @param Setup $setup | ||
* @return SingleType | ||
*/ | ||
public function setSetup(Setup $setup): self | ||
{ | ||
$this->setup = $setup; | ||
|
||
return $this; | ||
} | ||
} |
Oops, something went wrong.