diff --git a/composer.json b/composer.json index e175c99..cffe855 100644 --- a/composer.json +++ b/composer.json @@ -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" diff --git a/grumphp.yml.dist b/grumphp.yml.dist index a96bd21..266ca71 100644 --- a/grumphp.yml.dist +++ b/grumphp.yml.dist @@ -10,7 +10,7 @@ grumphp: #https://www.php.net/supported-versions.php phpversion: - project: '7.4' + project: '8.2' psalm: config: psalm.xml.dist diff --git a/src/LaravelStrapi.php b/src/LaravelStrapi.php index e55ba30..b338596 100644 --- a/src/LaravelStrapi.php +++ b/src/LaravelStrapi.php @@ -35,15 +35,15 @@ 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; @@ -51,9 +51,9 @@ public function collection(string $type, $sortKey = 'id', $sortOrder = 'DESC', $ $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) { @@ -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'); @@ -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); @@ -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); @@ -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'); @@ -142,7 +142,7 @@ 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; @@ -150,9 +150,9 @@ public function entriesByField(string $type, string $fieldName, $fieldValue, $fu $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); @@ -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'); @@ -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) { @@ -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'); @@ -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. @@ -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;