Skip to content

Commit

Permalink
Make writable map optional, and remove special rules for ID column
Browse files Browse the repository at this point in the history
  • Loading branch information
theodorejb committed Jan 3, 2025
1 parent 267d4c0 commit c2dec9a
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 34 deletions.
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,24 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [4.0.0] - 2025-01-03
### Changed
- `getMap()` is no longer abstract and returns an empty array by default,
so it is no longer necessary to implement for read-only APIs.
- If the ID column is returned from `getMap()`, it is now automatically writable
without having to set a `writableId` flag.
- `getSelectMap()` now returns an empty array by default (rather than the `getMap()` value),
so it is no longer necessary to implement this method just to prevent selecting write-only
properties when using `getSelectProps()`.

> [!Important]
> When upgrading, remove readonly ID properties from `getMap()`, and implement
> `getSelectMap()` if any writable property is not returned by `getSelectProps()`.
### Removed
- Unnecessary `getDefaultValues()` method. Defaults can be set via `processValues()` instead.
- `writableId` bool property.

## [3.0.0] - 2024-10-29
### Added
- Official support for PostgreSQL.
Expand Down Expand Up @@ -187,6 +205,7 @@ return early if passed an empty IDs array.
- Initial stable release


[4.0.0]: https://github.com/devtheorem/phaster/compare/v3.0.0...v4.0.0
[3.0.0]: https://github.com/devtheorem/phaster/compare/v2.9.0...v3.0.0
[2.9.0]: https://github.com/devtheorem/phaster/compare/v2.8.0...v2.9.0
[2.8.0]: https://github.com/devtheorem/phaster/compare/v2.7.0...v2.8.0
Expand Down
40 changes: 15 additions & 25 deletions src/Entities.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,8 @@

abstract class Entities
{
/**
* Returns a map of properties to columns for the current table
* @return array<string, mixed>
*/
abstract protected function getMap(): array;

protected PeachySql $db;
public string $idField = 'id';
protected bool $writableId = false;
private string $idColumn;
/** @var array<string, Prop> */
private array $fullPropMap;
Expand All @@ -37,25 +30,13 @@ public function __construct(PeachySql $db)
$propMap = Helpers::propListToPropMap([...array_values($bcProps), ...$selectProps]);

if (!isset($propMap[$this->idField])) {
throw new \Exception('Missing required id property in map');
}

$map = $this->getMap();

if (isset($map[$this->idField])) {
/** @psalm-suppress MixedAssignment */
$this->idColumn = $map[$this->idField];

if (!$this->writableId) {
unset($map[$this->idField]); // prevent modifying identity column
}
} else {
$idParts = explode('.', $propMap[$this->idField]->col);
$this->idColumn = array_pop($idParts);
throw new \Exception("Missing required {$this->idField} property in select map");
}

$idParts = explode('.', $propMap[$this->idField]->col);
$this->idColumn = array_pop($idParts);
$this->fullPropMap = $propMap;
$this->map = $map;
$this->map = $this->getMap();
}

/**
Expand All @@ -66,6 +47,15 @@ protected function getTableName(): string
return (new \ReflectionClass($this))->getShortName();
}

/**
* Returns a map of properties to writable columns for the current table.
* @return array<string, mixed>
*/
protected function getMap(): array
{
return [];
}

/**
* Returns the identity increment value of the table
*/
Expand Down Expand Up @@ -99,12 +89,12 @@ protected function getDefaultSort(): array
}

/**
* Can be used to return a separate property map for filtering/sorting (but not inserting/updating)
* Returns a map of properties to columns for selecting/filtering/sorting.
* @return array<string, mixed>
*/
protected function getSelectMap(): array
{
return $this->getMap();
return [];
}

/**
Expand Down
4 changes: 1 addition & 3 deletions test/DbTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,15 +322,14 @@ public function testModernUsers(): void

$db->insertRow('UserThings', ['user_id' => $ids[3]]);

$actual = $entities->getEntitiesByIds([$ids[2], $ids[3]], ['id', 'name', 'isDisabled', 'computed', 'weight', 'thing.uid']);
$actual = $entities->getEntitiesByIds([$ids[2], $ids[3]], ['id', 'name', 'isDisabled', 'computed', 'thing.uid']);

$expected = [
[
'id' => $ids[3],
'name' => 'Modern user 4',
'isDisabled' => false,
'computed' => 41.0,
'weight' => $db->options->floatSelectedAsString ? '40' : 40.0,
'thing' => [
'uid' => $ids[3],
],
Expand All @@ -340,7 +339,6 @@ public function testModernUsers(): void
'name' => 'Modern user 3 modified',
'isDisabled' => false,
'computed' => 31.0,
'weight' => $db->options->floatSelectedAsString ? '30' : 30.0,
'thing' => null,
],
];
Expand Down
10 changes: 5 additions & 5 deletions test/src/ModernUsers.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ protected function getTableName(): string
protected function getMap(): array
{
return [
'id' => 'user_id',
'name' => 'name',
'birthday' => 'dob',
'weight' => 'weight',
Expand All @@ -31,11 +30,12 @@ protected function getSelectProps(): array

return [
new Prop('id', 'u.user_id'),
new Prop('name', 'name', false, true, 'username'),
new Prop('isDisabled', 'is_disabled', false, true, '', 'bool'),
new Prop('computed', '', false, true, '', null, false, $getValue, ['weight']),
new Prop('name', 'name', alias: 'username'),
new Prop('weight', 'weight'),
new Prop('isDisabled', 'is_disabled', type: 'bool'),
new Prop('computed', getValue: $getValue, dependsOn: ['weight']),
new Prop('thing.id', 'ut.thing_id', true),
new Prop('thing.uid', 'ut.user_id', false, true, 'thing_user'),
new Prop('thing.uid', 'ut.user_id', alias: 'thing_user'),
];
}

Expand Down
6 changes: 5 additions & 1 deletion test/src/Users.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,18 @@ class Users extends Entities
protected function getMap(): array
{
return [
'id' => 'user_id',
'name' => 'name',
'birthday' => 'dob',
'weight' => 'weight',
'isDisabled' => 'is_disabled',
];
}

protected function getSelectMap(): array
{
return ['id' => 'user_id', ...$this->getMap()];
}

protected function getSelectProps(): array
{
return [
Expand Down

0 comments on commit c2dec9a

Please sign in to comment.