Skip to content

Commit

Permalink
Use constructor property promotion (todo: update commit message to cl…
Browse files Browse the repository at this point in the history
…ose issue 1)
  • Loading branch information
theodorejb committed Oct 7, 2024
1 parent aed10ab commit 08d1173
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 92 deletions.
4 changes: 2 additions & 2 deletions src/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public static function mapRows(\Generator $rows, array $fieldProps): array

$nullParents[$parent] = $prop;
continue;
} elseif ($prop->noOutput) {
} elseif (!$prop->output) {
continue;
}

Expand Down Expand Up @@ -227,7 +227,7 @@ public static function getFieldPropMap(array $fields, array $propMap): array
foreach ($propMap as $prop => $data) {
if (!isset($fieldProps[$prop]) && isset($dependedOn[$prop])) {
$data = clone $data;
$data->noOutput = true;
$data->output = false;
$fieldProps[$prop] = $data;
}
}
Expand Down
78 changes: 18 additions & 60 deletions src/Prop.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,85 +4,54 @@

namespace theodorejb\Phaster;

use Closure;
use DateTimeZone;

class Prop
{
/** @readonly */
public string $name;
/** @readonly */
public string $col;
/** @readonly */
public string $alias;

/**
* @var callable|null
* @readonly
*/
public $getValue;
/** @readonly */
public ?string $type = null;

/**
* @var DateTimeZone|null|false
* @readonly
*/
public $timeZone;

/**
* @var string[]
* @readonly
*/
public array $dependsOn;
/** @readonly */
public bool $nullGroup;
/** @readonly */
public bool $isDefault;

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

/**
* @var string[]
* @readonly
*/
public array $map;
/** @readonly */
public int $depth;
public readonly array $map;
public readonly int $depth;

/**
* @var string[]
* @readonly
*/
public array $parents = [];
public readonly array $parents;

/**
* @param DateTimeZone|null|false $timeZone
* @param string[] $dependsOn
*/
public function __construct(
string $name,
string $col = '',
bool $nullGroup = false,
bool $isDefault = true,
string $alias = '',
?string $type = null,
$timeZone = false,
?callable $getValue = null,
array $dependsOn = [],
public readonly string $name,
public readonly string $col = '',
public readonly bool $nullGroup = false,
public readonly bool $isDefault = true,
public readonly string $alias = '',
public readonly ?string $type = null,
public readonly DateTimeZone|null|false $timeZone = false,
public readonly ?Closure $getValue = null,
public readonly array $dependsOn = [],
bool $output = true
) {
$this->output = $output;
$this->map = explode('.', $name);
$this->depth = count($this->map);
$parent = '';
$parents = [];

for ($i = 0; $i < $this->depth - 1; $i++) {
$parent .= $this->map[$i] . '.';
$this->parents[] = $parent;
$parents[] = $parent;
}
$this->parents = $parents;

if ($nullGroup && $this->depth < 2) {
throw new \Exception("nullGroup cannot be set on top-level {$name} property");
Expand Down Expand Up @@ -113,17 +82,6 @@ public function __construct(
throw new \Exception("timeZone cannot be set on {$name} property along with type");
}
}

$this->name = $name;
$this->col = $col;
$this->alias = $alias;
$this->getValue = $getValue;
$this->type = $type;
$this->timeZone = $timeZone;
$this->dependsOn = $dependsOn;
$this->nullGroup = $nullGroup;
$this->isDefault = $isDefault;
$this->noOutput = !$output;
}

public function getOutputCol(): string
Expand Down
26 changes: 6 additions & 20 deletions src/QueryOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,15 @@
*/
class QueryOptions
{
/** @readonly */
public array $filter;
/** @readonly */
public array $originalFilter;
/** @readonly */
public array $sort;

/**
* @var Prop[]
* @readonly
*/
public array $fieldProps;

/**
* @param Prop[] $fieldProps
*/
public function __construct(array $filter, array $originalFilter, array $sort, array $fieldProps)
{
$this->filter = $filter;
$this->originalFilter = $originalFilter;
$this->sort = $sort;
$this->fieldProps = $fieldProps;
}
public function __construct(
public readonly array $filter,
public readonly array $originalFilter,
public readonly array $sort,
public readonly array $fieldProps
) {}

public function getColumns(): string
{
Expand Down
20 changes: 10 additions & 10 deletions test/HelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ public function testGetFieldPropMap(): void
$expected = ['client.isDisabled', 'groupName', 'client.id'];
$actual = Helpers::getFieldPropMap(['client.isDisabled', 'groupName'], $propMap);
$this->assertSame($expected, array_keys($actual));
$this->assertFalse($actual['groupName']->noOutput);
$this->assertTrue($actual['client.id']->noOutput);
$this->assertTrue($actual['groupName']->output);
$this->assertFalse($actual['client.id']->output);

$expected = [
'client.id' => $propMap['client.id'],
Expand All @@ -189,8 +189,8 @@ public function testGetFieldPropMap(): void
$expected = ['groupName', 'client.name'];
$actual = Helpers::getFieldPropMap(['groupName'], $propMap);
$this->assertSame($expected, array_keys($actual));
$this->assertFalse($actual['groupName']->noOutput);
$this->assertTrue($actual['client.name']->noOutput);
$this->assertTrue($actual['groupName']->output);
$this->assertFalse($actual['client.name']->output);

try {
Helpers::getFieldPropMap(['group.test'], $propMap);
Expand Down Expand Up @@ -221,17 +221,17 @@ public function testGetFieldPropMap(): void
$expected = ['client.name', 'client.id'];
$actual = Helpers::getFieldPropMap(['client.name'], $propMap);
$this->assertSame($expected, array_keys($actual));
$this->assertFalse($actual['client.name']->noOutput);
$this->assertTrue($actual['client.id']->noOutput);
$this->assertTrue($actual['client.name']->output);
$this->assertFalse($actual['client.id']->output);

// username should be selected even though it isn't default since client.id is marked as dependent on it
$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);
$this->assertTrue($actual['client.id']->output);
$this->assertTrue($actual['client.name']->output);
$this->assertFalse($actual['client.secret']->output);
$this->assertFalse($actual['username']->output);
}

public function testPropListToPropMap(): void
Expand Down

0 comments on commit 08d1173

Please sign in to comment.