Skip to content

Commit

Permalink
Add optional output parameter to Prop constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
theodorejb committed Aug 1, 2023
1 parent 9106b0c commit ebfa815
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 8 deletions.
13 changes: 12 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [2.6.0] - 2023-08-01
### Added
- `output` bool parameter on `Prop` constructor.

### Changed
- `col` is now optional when constructing `Prop` with a `getValue` function.

### Fixed
- Error when a `Prop` depends on a property declared in one of the `get*Map` methods.

## [2.5.0] - 2023-03-05
### Changed
- Minor code cleanup and refactoring.
Expand Down Expand Up @@ -132,7 +142,8 @@ return early if passed an empty IDs array.
### Changed
- Initial stable release

[Unreleased]: https://github.com/theodorejb/phaster/compare/v2.5.0...HEAD
[Unreleased]: https://github.com/theodorejb/phaster/compare/v2.6.0...HEAD
[2.6.0]: https://github.com/theodorejb/phaster/compare/v2.5.0...v2.6.0
[2.5.0]: https://github.com/theodorejb/phaster/compare/v2.4.0...v2.5.0
[2.4.0]: https://github.com/theodorejb/phaster/compare/v2.3.0...v2.4.0
[2.3.0]: https://github.com/theodorejb/phaster/compare/v2.2.2...v2.3.0
Expand Down
2 changes: 1 addition & 1 deletion src/Entities.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/**
* @psalm-type PropArray = array{
* col?: string, nullGroup?: bool, notDefault?: bool, alias?: string, type?: string,
* timeZone?: \DateTimeZone|null, getValue?: callable|null, dependsOn?: list<string>
* timeZone?: \DateTimeZone|null, getValue?: callable|null, dependsOn?: list<string>, output?: bool
* }
*/
abstract class Entities
Expand Down
3 changes: 2 additions & 1 deletion src/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,8 @@ public static function rawPropMapToProps(array $map): array
$options['type'] ?? null,
$options['timeZone'] ?? false,
$options['getValue'] ?? null,
$options['dependsOn'] ?? []
$options['dependsOn'] ?? [],
$options['output'] ?? true
);
}

Expand Down
7 changes: 5 additions & 2 deletions src/Prop.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ class Prop

/**
* This is the only property which can be modified externally.
* @internal
*/
public bool $noOutput = false;
public bool $noOutput;

/**
* @var string[]
Expand Down Expand Up @@ -71,7 +72,8 @@ public function __construct(
?string $type = null,
$timeZone = false,
?callable $getValue = null,
array $dependsOn = []
array $dependsOn = [],
bool $output = true
) {
$this->map = explode('.', $name);
$this->depth = count($this->map);
Expand Down Expand Up @@ -121,6 +123,7 @@ public function __construct(
$this->dependsOn = $dependsOn;
$this->nullGroup = $nullGroup;
$this->isDefault = $isDefault;
$this->noOutput = !$output;
}

public function getOutputCol(): string
Expand Down
9 changes: 6 additions & 3 deletions test/HelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,13 @@ public function testGetFieldPropMap(): void
$this->assertSame("' username' is not a valid field", $e->getMessage());
}

// test dependent field marked as not default
// test dependent fields marked as not default or excluded from output
$dependencies = ['username', 'client.secret'];
$rawPropMap = [
'username' => ['col' => 'UserName', 'notDefault' => true],
'client.id' => ['col' => 'ClientID', 'nullGroup' => true, 'getValue' => $valueGetter, 'dependsOn' => ['username']],
'client.id' => ['col' => 'ClientID', 'nullGroup' => true, 'getValue' => $valueGetter, 'dependsOn' => $dependencies],
'client.name' => ['col' => 'Company', 'alias' => 'ClientName'],
'client.secret' => ['col' => 'Secret', 'output' => false],
];

$props = Helpers::rawPropMapToProps($rawPropMap);
Expand All @@ -224,11 +226,12 @@ public function testGetFieldPropMap(): void
$this->assertTrue($actual['client.id']->noOutput);

// username should be selected even though it isn't default since client.id is marked as dependent on it
$expected = ['client.id', 'client.name', 'username'];
$expected = ['client.id', 'client.name', 'client.secret', 'username'];
$actual = Helpers::getFieldPropMap([], $propMap);
$this->assertSame($expected, array_keys($actual));
$this->assertFalse($actual['client.id']->noOutput);
$this->assertFalse($actual['client.name']->noOutput);
$this->assertTrue($actual['client.secret']->noOutput);
$this->assertTrue($actual['username']->noOutput);
}

Expand Down

0 comments on commit ebfa815

Please sign in to comment.