Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonDevelop committed Sep 3, 2022
2 parents b2e3a43 + e25a31b commit 516a743
Show file tree
Hide file tree
Showing 10 changed files with 433 additions and 18 deletions.
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,17 @@ PHP library allowing you to use the [Strapi](https://strapi.io/) API via various

## TODO
- [x] User system (without external provider)
- [ ] Collection types
- [ ] Single types
- Collection types
- [x] Get
- [x] Get Id
- [ ] Post
- [ ] Put
- [ ] Delete
- Single types
- [x] Get
- [ ] Put
- [ ] Delete
- [ ] Post
- [ ] Upload

Check this [docs](https://github.com/SimonDevelop/strapi-api/blob/master/docs) for more.
Expand Down
6 changes: 3 additions & 3 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

- [SimonDevelop\Strapi\Setup](https://github.com/SimonDevelop/strapi-api/blob/master/docs/setup.md)
- [SimonDevelop\Strapi\Auth](https://github.com/SimonDevelop/strapi-api/blob/master/docs/auth.md)
- SimonDevelop\Strapi\CollectionTypes
- SimonDevelop\Strapi\SingleTypes
- SimonDevelop\Strapi\Upload
- [SimonDevelop\Strapi\CollectionType](https://github.com/SimonDevelop/strapi-api/blob/master/docs/collection.md)
- [SimonDevelop\Strapi\SingleType](https://github.com/SimonDevelop/strapi-api/blob/master/docs/single.md)
- SimonDevelop\Strapi\Upload
30 changes: 30 additions & 0 deletions docs/collection.md
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);
```
24 changes: 24 additions & 0 deletions docs/single.md
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');
```
14 changes: 5 additions & 9 deletions src/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ConnectException;

/**
* Class Auth
* Authentication, register and more function.
*/
class Auth
{
/**
Expand Down Expand Up @@ -50,19 +46,19 @@ class Auth
/**
* @var Setup
*/
private $setup;
private Setup $setup;

/**
* @var Client
*/
private $client;
private Client $client;

/**
* @param Setup $setup
*/
public function __construct(Setup $setup)
{
$this->setup = $setup;
$this->setup = $setup;
$this->client = new Client();
}

Expand Down Expand Up @@ -175,8 +171,8 @@ public function resetPassword(string $code, string $newPassword, string $newPass
// @codeCoverageIgnoreStart
$response = $this->client->request('POST', $this->setup->getUrl() . self::RESET_PASSWORD_URL, [
'json' => [
'code' => $code,
'password' => $newPassword,
'code' => $code,
'password' => $newPassword,
'passwordConfirmation' => $newPasswordConfirm
]
]);
Expand Down
115 changes: 115 additions & 0 deletions src/CollectionType.php
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;
}
}
4 changes: 0 additions & 4 deletions src/Setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@

namespace SimonDevelop\Strapi;

/**
* Class Setup
* Configure your setup strapi api.
*/
class Setup
{
/**
Expand Down
86 changes: 86 additions & 0 deletions src/SingleType.php
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;
}
}
Loading

0 comments on commit 516a743

Please sign in to comment.