Skip to content

Commit

Permalink
Merge pull request #30 from BRFCS/feature/strapi-filters
Browse files Browse the repository at this point in the history
Added filter to the collection method.
  • Loading branch information
dbfx authored Feb 4, 2024
2 parents 25d32c9 + f60b23c commit 3a16b7c
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 61 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
.php_cs
.php_cs.cache
.phpunit.result.cache
.phpunit.cache/
build
composer.lock
coverage
Expand Down
8 changes: 2 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,8 @@
"illuminate/contracts": "^8.37|^9.0|^10"
},
"require-dev": {
"brianium/paratest": "^6.2",
"nunomaduro/collision": "^5.3|^6.0",
"orchestra/testbench": "^6.15",
"phpunit/phpunit": "^9.3",
"spatie/laravel-ray": "^1.9",
"vimeo/psalm": "^5.7"
"phpunit/phpunit": "^9.6",
"orchestra/testbench": "^6.0"
},
"autoload": {
"psr-4": {
Expand Down
56 changes: 19 additions & 37 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,39 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
executionOrder="random"
failOnWarning="true"
failOnRisky="true"
failOnEmptyTestSuite="true"
beStrictAboutOutputDuringTests="true"
verbose="true"
>
<testsuites>
<testsuite name="Dbfx Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<coverage>
<include>
<directory suffix=".php">./src</directory>
</include>
<report>
<html outputDirectory="build/coverage"/>
<text outputFile="build/coverage.txt"/>
<clover outputFile="build/logs/clover.xml"/>
</report>
</coverage>
<logging>
<junit outputFile="build/report.junit.xml"/>
</logging>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.4/phpunit.xsd" backupGlobals="false" bootstrap="vendor/autoload.php" colors="true" processIsolation="false" stopOnFailure="false" executionOrder="random" failOnWarning="true" failOnRisky="true" failOnEmptyTestSuite="true" beStrictAboutOutputDuringTests="true">
<testsuites>
<testsuite name="Dbfx Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<coverage>
<report>
<html outputDirectory="build/coverage"/>
<text outputFile="build/coverage.txt"/>
<clover outputFile="build/logs/clover.xml"/>
</report>
</coverage>
<logging>
<junit outputFile="build/report.junit.xml"/>
</logging>
<php>
<env name="STRAPI_URL" value="http://localhost:3000" />
</php>
</phpunit>
30 changes: 24 additions & 6 deletions src/LaravelStrapi.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Dbfx\LaravelStrapi\Exceptions\UnknownError;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;

class LaravelStrapi
{
Expand All @@ -28,19 +29,21 @@ public function __construct()
}
}

public function collection(string $type, $sortKey = 'id', $sortOrder = 'DESC', $limit = 20, $start = 0, $fullUrls = true, array $populate = array()): array
public function collection(string $type, $sortKey = 'id', $sortOrder = 'DESC', $limit = 20, $start = 0, $fullUrls = true, array $populate = array(), array $filters = array()): array
{
$url = $this->strapiUrl;
$cacheKey = self::CACHE_KEY . '.collection.' . $type . '.' . $sortKey . '.' . $sortOrder . '.' . $limit . '.' . $start;
$filterString = $this->createFilterString($filters);
$cacheKey = self::CACHE_KEY . '.collection.' . $type . '.' . $sortKey . '.' . $sortOrder . '.' . $limit . '.' . $start . '.' . $filterString;
$populateString = $this->createPopulateString($populate);

// Fetch and cache the collection type
$collection = Cache::remember($cacheKey, $this->cacheTime, function () use ($url, $type, $sortKey, $sortOrder, $limit, $start, $populateString) {
$response = Http::withHeaders($this->headers)->get($url . '/' . $type . '?sort[0]=' . $sortKey . ':' . $sortOrder . '&pagination[limit]=' . $limit . '&pagination[start]=' . $start . '&' . $populateString);
$collection = Cache::remember($cacheKey, $this->cacheTime, function () use ($url, $type, $sortKey, $sortOrder, $limit, $start, $populateString, $filterString) {
$response = Http::withHeaders($this->headers)->get($url . '/' . $type . '?sort[0]=' . $sortKey . ':' . $sortOrder . '&pagination[limit]=' . $limit . '&pagination[start]=' . $start . '&' . $populateString . '&' . $filterString);

return $response->json();
});


if (isset($collection['statusCode']) && $collection['statusCode'] >= 400) {
Cache::forget($cacheKey);

Expand Down Expand Up @@ -217,8 +220,8 @@ private function createPopulateString($array): string
{
$populateString = '';

foreach($array as $key => $value) {
if($key == 0) {
foreach ($array as $key => $value) {
if ($key == 0) {
$populateString = 'populate[' . $key . ']=' . $value;
} else {
$populateString = $populateString . '&populate[' . $key . ']=' . $value;
Expand All @@ -227,4 +230,19 @@ private function createPopulateString($array): string

return $populateString;
}

public function createFilterString($array): string
{
$filters = [];

foreach ($array as $key => $value) {
$col = array_keys($value)[0];
$op = array_keys($value[$col])[0];
$val = array_values($value[$col])[0];

$filters[] = "filters[$col][$op]=$val";
}

return implode("&", $filters);
}
}
12 changes: 0 additions & 12 deletions tests/ExampleTest.php

This file was deleted.

26 changes: 26 additions & 0 deletions tests/LaravelStrapiTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Dbfx\LaravelStrapi\Tests;

use Dbfx\LaravelStrapi\LaravelStrapi;

class LaravelStrapiTest extends TestCase
{
public function test_single_filter_string()
{
$strapi = new LaravelStrapi();

$filterString = $strapi->createFilterString([["column" => ['$eq' => 'value']]]);

$this->assertEquals('filters[column][$eq]=value', $filterString);
}

public function test_multi_filter_string()
{
$strapi = new LaravelStrapi();

$filterString = $strapi->createFilterString([["column" => ['$eq' => 'value']], ["column1" => ['$eq1' => 'value1']]]);

$this->assertEquals('filters[column][$eq]=value&filters[column1][$eq1]=value1', $filterString);
}
}

0 comments on commit 3a16b7c

Please sign in to comment.