Skip to content

Commit db9728d

Browse files
committed
enum support
1 parent ca6d7b8 commit db9728d

File tree

6 files changed

+87
-10
lines changed

6 files changed

+87
-10
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# PHPFUI\ORM [![Tests](https://github.com/phpfui/ORM/actions/workflows/tests.yml/badge.svg)](https://github.com/phpfui/ORM/actions?query=workflow%3Atests) [![Latest Packagist release](https://img.shields.io/packagist/v/phpfui/ORM.svg)](https://packagist.org/packages/phpfui/ORM) ![](https://img.shields.io/badge/PHPStan-level%206-brightgreen.svg?style=flat)
22

33
### PHPFUI\ORM a minimal Object Relational Mapper (ORM) for MySQL, MariaDB and SQLite3
4-
Why another PHP ORM? In writing minimal and fast websites, it was determined that existing PHP ORM solutions were overly complex. **PHPFUI\ORM** is a little more than 6K lines of code in under 50 files. It is designed to have a minimal memory footprint and excellent execution times for most database needs.
4+
Why another PHP ORM? In writing minimal and fast websites, it was determined that existing PHP ORM solutions were overly complex. **PHPFUI\ORM** is a little more than 6.5K lines of code in under 50 files. It is designed to have a minimal memory footprint and excellent execution times for most database needs.
55

66
**PHPFUI\ORM** is not an attempt to write an abstraction around SQL as other ORMs do, rather it is a way to work with SQL that closely matches the semantics of SQL, with the power of PHP objects. It allows PHP to manipulate SQL queries without having to write SQL in plain text. This is very useful for queries generated via user interfaces where the user is given a lot of flexability in how a query is defined.
77

Tests/Unit/JoinTest.php

+4-5
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
class JoinTest extends \PHPUnit\Framework\TestCase
66
{
7-
public function testBadTableJoin() : void
7+
public function testBadJoin() : void
88
{
99
$this->expectException(\PHPFUI\ORM\Exception::class);
1010
$table = new \Tests\App\Table\InventoryTransaction();
11-
$table->addJoin('fred');
11+
$table->addJoin('employee');
1212
}
1313

1414
public function testBadJoinType() : void
@@ -19,11 +19,10 @@ public function testBadJoinType() : void
1919
$table->addJoin('fred', type:'fred');
2020
}
2121

22-
public function testBadJoin() : void
22+
public function testBadTableJoin() : void
2323
{
2424
$this->expectException(\PHPFUI\ORM\Exception::class);
2525
$table = new \Tests\App\Table\InventoryTransaction();
26-
$table->addJoin('employee');
26+
$table->addJoin('fred');
2727
}
2828
}
29-

docs/5. Virtual Fields.md

+32
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,35 @@ class Invoice extends \Tests\App\Record\Definition\Order
170170
$invoice = new Invoice(20);
171171
echo 'Lead Weeks: ' . $invoice->invoice_date->diffInWeeks($invoice->due_date);
172172
```
173+
174+
## Type Safe Enum Support
175+
In PHP 8.1 and above, you can add enum support easily. Assume this is your enum:
176+
```php
177+
namespace App\Enum;
178+
enum IncludeMembership : int
179+
{
180+
case NO = 0;
181+
case NEW_MEMBERS_ONLY = 1;
182+
case EXTEND_MEMBERSHIP = 2;
183+
case RENEW_MEMBERSHIP = 3;
184+
}
185+
```
186+
You can define the event.includeMembership field to use enums instead of integer values.
187+
```php
188+
class Event extends \App\Record\Definition\Event
189+
{
190+
protected static array $virtualFields = [
191+
'includeMembership' => [\PHPFUI\ORM\Enum::class, \App\Enum\IncludeMembership::class],
192+
];
193+
}
194+
```
195+
Your code would now look like this:
196+
```php
197+
if (\App\Enum\IncludeMembership::NEW_MEMBERS_ONLY == $event->includeMembership)
198+
```
199+
You can also set and save the enum directly:
200+
```php
201+
$event->includeMembership = \App\Enum\IncludeMembership:NO;
202+
$event->update();
203+
```
204+
Enum assignments are type safe. Attempting to set the enum with an incorrect type will throw an exception.

src/PHPFUI/ORM/Enum.php

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace PHPFUI\ORM;
4+
5+
class Enum extends \PHPFUI\ORM\VirtualField
6+
{
7+
/**
8+
* @param array<mixed> $parameters optional
9+
*/
10+
public function getValue(array $parameters) : mixed
11+
{
12+
$enum = $parameters[0];
13+
14+
return $enum::from($this->currentRecord->offsetGet($this->fieldName));
15+
}
16+
17+
/**
18+
* @param array<mixed> $parameters optional
19+
*/
20+
public function setValue(mixed $value, array $parameters) : void
21+
{
22+
$enum = $parameters[0];
23+
24+
if ($value instanceof $enum)
25+
{
26+
$this->currentRecord->offsetSet($this->fieldName, $value->value);
27+
28+
return;
29+
}
30+
31+
throw new \PHPFUI\ORM\Exception('You can not assign a variable of type ' . \get_debug_type($value) . ' to an enum type of ' . $enum);
32+
}
33+
}

src/PHPFUI/ORM/Record.php

+13-1
Original file line numberDiff line numberDiff line change
@@ -505,13 +505,25 @@ public function loadFromSQL(string $sql, array $input = []) : bool
505505
return true;
506506
}
507507

508-
public function offsetGet(mixed $offset) : mixed
508+
/**
509+
* Low level get access to underlying data to implement ArrayAccess
510+
*/
511+
public function offsetGet($offset) : mixed
509512
{
510513
$this->validateFieldExists($offset);
511514

512515
return $this->current[$offset] ?? null;
513516
}
514517

518+
/**
519+
* Low level set access to underlying data to implement ArrayAccess
520+
*/
521+
public function offsetSet($offset, $value) : void
522+
{
523+
$this->validateFieldExists($offset);
524+
$this->current[$offset] = $value;
525+
}
526+
515527
/**
516528
* Read a record from the db. If more than one match, only the first is loaded.
517529
*

src/PHPFUI/ORM/Table.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -176,12 +176,13 @@ public function addJoin(string $table, string | \PHPFUI\ORM\Condition $on = '',
176176
$thisFields = $this->getFields();
177177
$joinFields = $joinTable->getFields();
178178
$keys = $this->getPrimaryKeys();
179-
$thisPrimaryKey = array_shift($keys);
180-
if (array_key_exists($on, $thisFields) && array_key_exists($on, $joinFields))
179+
$thisPrimaryKey = \array_shift($keys);
180+
181+
if (\array_key_exists($on, $thisFields) && \array_key_exists($on, $joinFields))
181182
{
182183
// do nothing here, just exclude this case
183184
}
184-
else if (array_key_exists($thisPrimaryKey, $joinFields))
185+
elseif (\array_key_exists($thisPrimaryKey, $joinFields))
185186
{
186187
// join on master table primary key
187188
$on = $thisPrimaryKey;

0 commit comments

Comments
 (0)