Skip to content

Commit

Permalink
feat: add default fields and clear_elements helper
Browse files Browse the repository at this point in the history
  • Loading branch information
kauffinger committed Oct 14, 2024
1 parent 1dc3373 commit bb99390
Show file tree
Hide file tree
Showing 6 changed files with 245 additions and 1 deletion.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,37 @@ $result = BaseRepository::lastRecorded();
*/
```

### Default Fields
Sometimes, it can be useful to have default fields for your queries
to quickly glance at data. You can find a selection of default fields
in the OnOfficeService.

```php
use Innobrain\OnOfficeAdapter\Facades\EstateRepository;
use Innobrain\OnOfficeAdapter\Services\OnOfficeService;

$estates = EstateRepository::query()
->select(OnOfficeService::DEFAULT_ESTATE_INFO_FIELDS)
->get();
```

### Helpers
When using default fields, you might find it helpful to hide all
empty fields. To do so, use the `clean_elements` helper.
It will work with both `find` and `get` responses.

```php
use Innobrain\OnOfficeAdapter\Facades\EstateRepository;
use Innobrain\OnOfficeAdapter\Services\OnOfficeService;

$estates = EstateRepository::query()
->select(OnOfficeService::DEFAULT_ESTATE_INFO_FIELDS)
->get();

// will leave out fields with empty values like "", "0.00", [], or null.
$estates = clean_elements($estates);
```

### Usage in tests
```php
use Innobrain\OnOfficeAdapter\Facades\EstateRepository;
Expand Down
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@
"psr-4": {
"Innobrain\\OnOfficeAdapter\\": "src/",
"Innobrain\\OnOfficeAdapter\\Database\\Factories\\": "database/factories/"
}
},
"files": [
"src/helpers.php"
]
},
"autoload-dev": {
"psr-4": {
Expand Down
61 changes: 61 additions & 0 deletions src/Services/OnOfficeDefaultFieldConst.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);

namespace Innobrain\OnOfficeAdapter\Services;

trait OnOfficeDefaultFieldConst
{
// Contains default fields for resources in onOffice API.
// These fields are helpful to quickly take a look at data.
// Note that some clients may have deactivated some fields,
// which will lead to a failed request.
// You will need to try to remove those fields from
// the request to get a successful response.

const DEFAULT_ESTATE_INFO_FIELDS = [
'anzahl_zimmer',
'objektart',
'objekttyp',
'property_type',
'regionaler_zusatz',
'aussicht',
'etage',
'barrierefrei',
'rollstuhlgerecht',
'seniorengerecht',
'fahrstuhl',
'wohnflaeche',
'vermarktungsart',
'zustand',
'anzahl_schlafzimmer',
'anzahl_badezimmer',
'unterkellert',
'unterkellertText',
'baujahr',
'bauweise',
'jahrLetzteModernisierung',
'letzteModernisierung',
'energiestandard',
'ausstattungsqualitaet',
'boden',
'bad',
'gaesteWc',
'kueche',
'klimatisiert',
'kamin',
'gartennutzung',
'terrasse',
'ausricht_balkon_terrasse',
'balkon',
'swimmingpool',
'sauna',
'haustiere',
'hausgeld',
'vermietet',
'mieteinnahmen_pro_jahr_ist',
'nebenkosten',
'verfuegbar_ab',
'visit_remark',
];
}
1 change: 1 addition & 0 deletions src/Services/OnOfficeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

class OnOfficeService
{
use OnOfficeDefaultFieldConst;
use OnOfficeParameterConst;

public function __construct() {}
Expand Down
36 changes: 36 additions & 0 deletions src/helpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

use Illuminate\Support\Collection;

if (! function_exists('clear_elements')) {
/**
* Clear elements from an onOffice response
*
* @param array{id: string, type: string, elements: array} | array{array{id: string, type: string, elements: array}} | array{} $response
*/
function clear_elements(array $response): array
{
// Determine if it's a single record or multiple records
$isMultiple = ! isset($response['elements']);

$cleaned = Collection::make($isMultiple ? $response : [$response])
->map(function (array $record) {
$record['elements'] = Collection::make($record['elements'])
->reject(fn ($value) => match ($value) {
[], null, '', '0.00' => true,
default => false,
})
->all();

return $record;
});

if ($isMultiple) {
return $cleaned->all();
}

return $cleaned->first();
}
}
112 changes: 112 additions & 0 deletions tests/HelpersTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

declare(strict_types=1);

test('clear_elements removes empty elements from a single record', function () {
$input = [
'id' => '1',
'type' => 'estate',
'elements' => [
'name' => 'Test Estate',
'price' => '100000',
'description' => '',
'rooms' => '0.00',
'area' => null,
'features' => [],
],
];

$expected = [
'id' => '1',
'type' => 'estate',
'elements' => [
'name' => 'Test Estate',
'price' => '100000',
],
];

$result = clear_elements($input);

expect($result)->toEqual($expected);
});

test('clear_elements removes empty elements from multiple records', function () {
$input = [
[
'id' => '1',
'type' => 'estate',
'elements' => [
'name' => 'Estate 1',
'price' => '100000',
'description' => '',
],
],
[
'id' => '2',
'type' => 'estate',
'elements' => [
'name' => 'Estate 2',
'price' => '0.00',
'area' => null,
],
],
];

$expected = [
[
'id' => '1',
'type' => 'estate',
'elements' => [
'name' => 'Estate 1',
'price' => '100000',
],
],
[
'id' => '2',
'type' => 'estate',
'elements' => [
'name' => 'Estate 2',
],
],
];

$result = clear_elements($input);

expect($result)->toEqual($expected);
});

test('clear_elements handles an empty input array', function () {
$input = [];
$result = clear_elements($input);
expect($result)->toEqual([]);
});

test('clear_elements preserves non-empty values', function () {
$input = [
'id' => '1',
'type' => 'estate',
'elements' => [
'name' => 'Test Estate',
'price' => '100000',
'rooms' => '3',
'area' => '150.5',
'features' => ['garden', 'garage'],
],
];

$expected = [
'id' => '1',
'type' => 'estate',
'elements' => [
'name' => 'Test Estate',
'price' => '100000',
'rooms' => '3',
'area' => '150.5',
'features' => ['garden', 'garage'],
],
];

$result = clear_elements($input);

expect($result)->toEqual($expected);
});

0 comments on commit bb99390

Please sign in to comment.