Skip to content

Commit

Permalink
Feature/aggregate facility use modifier (#109)
Browse files Browse the repository at this point in the history
* Add an Order feed modifier that filters out Order elements that should not be present on Order objects when they are in the Orders RPDE feed. Also add a test case to check it works.
  • Loading branch information
drinkynet authored Jun 30, 2023
1 parent 18ace54 commit 45ceb5e
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ Example & helper modifiers can be found in the `src/Modifiers` directory.
**Please note:** [Composer](https://getcomposer.org/doc/00-intro.md#installation-linux-unix-macos) is required for dependency management.

```bash
git clone https://github.com/openactive/models-php.git
git clone git@github.com:openactive/models-php.git
cd models-php
composer install
```
Expand Down
29 changes: 29 additions & 0 deletions src/Modifiers/AggregateFacilityUseProviderModifier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/**
* AggregateFacilityUseProviderModifier
*
* Removes the provider attribute from the aggregateFacilityUse
* property of an OrderedItem's FacilityUse.
*
* Used to modify the C1/C2/B order output if the provider has
* been added in during construction of Slot OrderedItems
*
* This typically happens because the Orders feed requires the
* provider to be present and the C1/C2/B steps require it to
* be omitted. It depends how your renderer for FacilityUse has
* been setup
*/

namespace OpenActive\Modifiers;

class AggregateFacilityUseProviderModifier
{
public function __invoke($class, $key, $value)
{
if ($key == 'aggregateFacilityUse' && isset($value['provider'])) {
unset($value['provider']);
}

return $value;
}
}
60 changes: 60 additions & 0 deletions tests/Unit/Modifiers/AggregateFacilityUseProviderModifierTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace OpenActive\Models\Tests\Unit\Modifiers;

use OpenActive\Modifiers\AggregateFacilityUseProviderModifier;
use PHPUnit\Framework\TestCase;

class AggregateFacilityUseProviderModifierTest extends TestCase
{
protected function getTestData()
{
return [
'id' => 1,
'provider' => 'https://example.com/openactive/sellers/1'
];
}

public function testModifierRemovesProvider()
{
// Data for this test
$testData = $this->getTestData();

// Check the test data has both keys
$this->assertArrayHasKey('id', $testData);
$this->assertArrayHasKey('provider', $testData);

// Create a new modifier
$modifier = new AggregateFacilityUseProviderModifier();

// Run the modifier against the test data
$resultData = $modifier('', 'aggregateFacilityUse', $testData);

// Check that the result still has the ID key
$this->assertArrayHasKey('id', $resultData);

// Check that the provider key has been removed
$this->assertArrayNotHasKey('provider', $resultData);
}

public function testModifierDoesNotRemoveProvider()
{
// Data for this test
$testData = $this->getTestData();

// Check the test data has both keys
$this->assertArrayHasKey('id', $testData);
$this->assertArrayHasKey('provider', $testData);

// Create a new modifier
$modifier = new AggregateFacilityUseProviderModifier();

// Run the modifier against the test data for a key it should
// not modify
$resultData = $modifier('', 'someOtherIgnoredType', $testData);

// Check the result data has both keys
$this->assertArrayHasKey('id', $resultData);
$this->assertArrayHasKey('provider', $resultData);
}
}

0 comments on commit 45ceb5e

Please sign in to comment.