generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add default fields and clear_elements helper
- Loading branch information
1 parent
1dc3373
commit bb99390
Showing
6 changed files
with
245 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |