Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'create' and 'updated' methods. #43

Merged
merged 6 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
],
"homepage": "https://github.com/dbfx/laravel-strapi",
"require": {
"php": "^7.4 || ^8.0",
"php": "^8.0",
"illuminate/contracts": "^8.37 || ^9.0 || ^10",
"laravel/framework": "^8 || ^9 || ^10",
"spatie/laravel-package-tools": "^1.4.3"
Expand Down
2 changes: 1 addition & 1 deletion grumphp.yml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ grumphp:

#https://www.php.net/supported-versions.php
phpversion:
project: '7.4'
project: '8.2'

psalm:
config: psalm.xml.dist
Expand Down
72 changes: 47 additions & 25 deletions src/LaravelStrapi.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,25 @@ public function __construct()
$this->token = config('strapi.token');

if (!empty($this->token)) {
$this->headers['Authorization'] = 'Bearer '.$this->token;
$this->headers['Authorization'] = 'Bearer ' . $this->token;
}
}

public function collection(string $type, $sortKey = 'id', $sortOrder = 'DESC', $limit = 20, $start = 0, $fullUrls = true, array|string $populate = [], array $queryData = []): array
{
$endpoint = $this->strapiUrl.'/'.$type;
$endpoint = $this->strapiUrl . '/' . $type;

$queryData['sort'][0] = $sortKey.':'.$sortOrder;
$queryData['sort'][0] = $sortKey . ':' . $sortOrder;
$queryData['pagination']['limit'] = $limit;
$queryData['pagination']['start'] = $start;

if (!empty($populate)) {
$queryData['populate'] = $populate;
}

$endpoint .= '?'.http_build_query($queryData);
$endpoint .= '?' . http_build_query($queryData);

$cacheKey = self::CACHE_KEY.'.'.__FUNCTION__.'.'.encrypt($endpoint);
$cacheKey = self::CACHE_KEY . '.' . __FUNCTION__ . '.' . encrypt($endpoint);

// Fetch and cache the collection type
$return = Cache::remember($cacheKey, $this->cacheTime, function () use ($endpoint) {
Expand All @@ -65,14 +65,14 @@ public function collection(string $type, $sortKey = 'id', $sortOrder = 'DESC', $
if (isset($return['statusCode']) && $return['statusCode'] >= 400) {
Cache::forget($cacheKey);

throw new PermissionDenied('Strapi returned a '.$return['statusCode']);
throw new PermissionDenied('Strapi returned a ' . $return['statusCode']);
}

if (!is_array($return)) {
Cache::forget($cacheKey);

if (null === $return) {
throw new NotFound('The requested single entry ('.$type.') was null');
throw new NotFound('The requested single entry (' . $type . ') was null');
}

throw new UnknownError('An unknown Strapi error was returned');
Expand All @@ -88,9 +88,9 @@ public function collection(string $type, $sortKey = 'id', $sortOrder = 'DESC', $

public function collectionCount(string $type): int
{
$endpoint = $this->strapiUrl.'/'.$type.'/count';
$endpoint = $this->strapiUrl . '/' . $type . '/count';

$cacheKey = self::CACHE_KEY.'.'.__FUNCTION__.'.'.encrypt($endpoint);
$cacheKey = self::CACHE_KEY . '.' . __FUNCTION__ . '.' . encrypt($endpoint);

return Cache::remember($cacheKey, $this->cacheTime, function () use ($endpoint) {
$response = Http::withHeaders($this->headers)->get($endpoint);
Expand All @@ -101,15 +101,15 @@ public function collectionCount(string $type): int

public function entry(string $type, int $id, $fullUrls = true, array|string $populate = [], array $queryData = []): array
{
$endpoint = $this->strapiUrl.'/'.$type.'/'.$id;
$endpoint = $this->strapiUrl . '/' . $type . '/' . $id;

if (!empty($populate)) {
$queryData['populate'] = $populate;
}

$endpoint .= '?'.http_build_query($queryData);
$endpoint .= '?' . http_build_query($queryData);

$cacheKey = self::CACHE_KEY.'.'.__FUNCTION__.'.'.encrypt($endpoint);
$cacheKey = self::CACHE_KEY . '.' . __FUNCTION__ . '.' . encrypt($endpoint);

$return = Cache::remember($cacheKey, $this->cacheTime, function () use ($endpoint) {
$response = Http::withHeaders($this->headers)->get($endpoint);
Expand All @@ -120,14 +120,14 @@ public function entry(string $type, int $id, $fullUrls = true, array|string $pop
if (isset($return['statusCode']) && $return['statusCode'] >= 400) {
Cache::forget($cacheKey);

throw new PermissionDenied('Strapi returned a '.$return['statusCode']);
throw new PermissionDenied('Strapi returned a ' . $return['statusCode']);
}

if (!is_array($return)) {
Cache::forget($cacheKey);

if (null === $return) {
throw new NotFound('The requested single entry ('.$type.') was null');
throw new NotFound('The requested single entry (' . $type . ') was null');
}

throw new UnknownError('An unknown Strapi error was returned');
Expand All @@ -142,17 +142,17 @@ public function entry(string $type, int $id, $fullUrls = true, array|string $pop

public function entriesByField(string $type, string $fieldName, $fieldValue, $fullUrls = true, array|string $populate = [], array $queryData = []): array
{
$endpoint = $this->strapiUrl.'/'.$type;
$endpoint = $this->strapiUrl . '/' . $type;

$queryData['filters'][$fieldName]['$eq'] = $fieldValue;

if (!empty($populate)) {
$queryData['populate'] = $populate;
}

$endpoint .= '?'.http_build_query($queryData);
$endpoint .= '?' . http_build_query($queryData);

$cacheKey = self::CACHE_KEY.'.'.__FUNCTION__.'.'.encrypt($endpoint);
$cacheKey = self::CACHE_KEY . '.' . __FUNCTION__ . '.' . encrypt($endpoint);

$entries = Cache::remember($cacheKey, $this->cacheTime, function () use ($endpoint) {
$response = Http::withHeaders($this->headers)->get($endpoint);
Expand All @@ -163,14 +163,14 @@ public function entriesByField(string $type, string $fieldName, $fieldValue, $fu
if (isset($entries['statusCode']) && $entries['statusCode'] >= 400) {
Cache::forget($cacheKey);

throw new PermissionDenied('Strapi returned a '.$entries['statusCode']);
throw new PermissionDenied('Strapi returned a ' . $entries['statusCode']);
}

if (!is_array($entries)) {
Cache::forget($cacheKey);

if (null === $entries) {
throw new NotFound('The requested entries by field ('.$type.') were not found');
throw new NotFound('The requested entries by field (' . $type . ') were not found');
}

throw new UnknownError('An unknown Strapi error was returned');
Expand All @@ -185,15 +185,15 @@ public function entriesByField(string $type, string $fieldName, $fieldValue, $fu

public function single(string $type, string $pluck = null, $fullUrls = true, array|string $populate = [], array $queryData = []): array
{
$endpoint = $this->strapiUrl.'/'.$type;
$endpoint = $this->strapiUrl . '/' . $type;

if (!empty($populate)) {
$queryData['populate'] = $populate;
}

$endpoint .= '?'.http_build_query($queryData);
$endpoint .= '?' . http_build_query($queryData);

$cacheKey = self::CACHE_KEY.'.'.__FUNCTION__.'.'.encrypt($endpoint);
$cacheKey = self::CACHE_KEY . '.' . __FUNCTION__ . '.' . encrypt($endpoint);

// Fetch and cache the collection type
$return = Cache::remember($cacheKey, $this->cacheTime, function () use ($endpoint) {
Expand All @@ -205,14 +205,14 @@ public function single(string $type, string $pluck = null, $fullUrls = true, arr
if (isset($return['statusCode']) && $return['statusCode'] >= 400) {
Cache::forget($cacheKey);

throw new PermissionDenied('Strapi returned a '.$return['statusCode']);
throw new PermissionDenied('Strapi returned a ' . $return['statusCode']);
}

if (!is_array($return)) {
Cache::forget($cacheKey);

if (null === $return) {
throw new NotFound('The requested single entry ('.$type.') was null');
throw new NotFound('The requested single entry (' . $type . ') was null');
}

throw new UnknownError('An unknown Strapi error was returned');
Expand All @@ -230,6 +230,28 @@ public function single(string $type, string $pluck = null, $fullUrls = true, arr
return $return;
}

/**
* Function to create new entries in the Strapi DB.
*/
public function create(string $type, array $data): array
{
$endpoint = "$this->strapiUrl/$type";
$response = Http::withHeaders($this->headers)->post($endpoint, ['data' => $data]);

return $response->json();
}

/**
* Function to create new entries in the Strapi DB.
*/
public function update(string $type, int|string $id, array $data): array
{
$endpoint = "$this->strapiUrl/$type/$id";
$response = Http::withHeaders($this->headers)->put($endpoint, ['data' => $data]);

return $response->json();
}

/**
* This function adds the Strapi URL to the front of content in entries, collections, etc.
* This is primarily used to change image URLs to actually point to Strapi.
Expand All @@ -247,7 +269,7 @@ private function convertToFullUrls($array): array
continue;
}

$array[$key] = preg_replace('/!\[(.*)\]\((.*)\)/', '![$1]('.config('strapi.url').'$2)', $item);
$array[$key] = preg_replace('/!\[(.*)\]\((.*)\)/', '![$1](' . config('strapi.url') . '$2)', $item);
}

return $array;
Expand Down
Loading