Skip to content

Commit 7115995

Browse files
committed
Table::validateFromTable
1 parent c926404 commit 7115995

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

Tests/Unit/MiscellaneousTest.php

+34
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,42 @@
22

33
namespace Tests\Unit;
44

5+
enum TestEnum : int
6+
{
7+
case NO = 0;
8+
case YES = 1;
9+
}
10+
511
class MiscellaneousTest extends \PHPUnit\Framework\TestCase
612
{
13+
public function testEnumCondition() : void
14+
{
15+
$condition = new \PHPFUI\ORM\Condition('field', TestEnum::YES);
16+
$input = $condition->getInput();
17+
$this->assertIsArray($input);
18+
$this->assertCount(1, $input);
19+
$this->assertContains(1, $input);
20+
}
21+
22+
public function testInOperator() : void
23+
{
24+
$in = new \PHPFUI\ORM\Operator\In();
25+
$this->assertTrue($in->correctlyTyped([1, 2]));
26+
$this->assertFalse($in->correctlyTyped([]));
27+
$this->assertFalse($in->correctlyTyped(1));
28+
29+
$notIn = new \PHPFUI\ORM\Operator\NotIn();
30+
$this->assertTrue($notIn->correctlyTyped([1, 2]));
31+
$this->assertFalse($notIn->correctlyTyped([]));
32+
$this->assertFalse($notIn->correctlyTyped(1));
33+
}
34+
35+
public function testLiteralCondition() : void
36+
{
37+
$condition = new \PHPFUI\ORM\Condition('field', new \PHPFUI\ORM\Literal('invoiceItem.storeItemId'));
38+
$this->assertEquals('field = invoiceItem.storeItemId', (string)$condition);
39+
}
40+
741
public function testRow() : void
842
{
943
$row = \PHPFUI\ORM::getRow('select * from customer');

src/PHPFUI/ORM/Table.php

+45
Original file line numberDiff line numberDiff line change
@@ -1069,6 +1069,51 @@ public function updateFromTable(array $request) : bool
10691069
return $transation->commit();
10701070
}
10711071

1072+
/**
1073+
* @param array<string,mixed> $request
1074+
*
1075+
* @return array<string,string> errors
1076+
*/
1077+
public function validateFromTable(array $request) : array
1078+
{
1079+
$fields = $this->instance->getFields();
1080+
1081+
$primaryKeys = $this->getPrimaryKeys();
1082+
1083+
$errors = [];
1084+
1085+
if (\count($primaryKeys))
1086+
{
1087+
$mainKey = $primaryKeys[0];
1088+
1089+
foreach ($request[$mainKey] ?? [] as $existingKey => $index)
1090+
{
1091+
$data = [];
1092+
1093+
$record = new static::$className($existingKey);
1094+
1095+
foreach ($fields as $field => $typeInfo)
1096+
{
1097+
if (isset($request[$field]))
1098+
{
1099+
if (\is_array($request[$field]))
1100+
{
1101+
$data[$field] = $request[$field][$index];
1102+
}
1103+
else
1104+
{
1105+
$data[$field] = $request[$field];
1106+
}
1107+
}
1108+
}
1109+
$record->setFrom($data);
1110+
$errors = \array_merge($errors, $record->validate());
1111+
}
1112+
}
1113+
1114+
return $errors;
1115+
}
1116+
10721117
private function doTranslation(string $text) : string
10731118
{
10741119
$translationCallback = null;

0 commit comments

Comments
 (0)