Skip to content

Commit 75ea9c9

Browse files
committed
Refactoring
1 parent 70d8f0d commit 75ea9c9

File tree

4 files changed

+86
-45
lines changed

4 files changed

+86
-45
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Performance [comparison of PHPFUI\ORM to Eloquent](https://github.com/phpfui/php
2626
### Setup
2727
```php
2828
$pdo = new \PHPFUI\ORM\PDOInstance($yourConnectionString);
29-
// permform any custom configuration settings needed on $pdo
29+
// perform any custom configuration settings needed on $pdo
3030
\PHPFUI\ORM::addConnection($pdo);
3131
```
3232

Tests/Unit/RecordTest.php

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Tests\Unit;
4+
5+
class RecordTest extends \PHPUnit\Framework\TestCase
6+
{
7+
public function testBadField() : void
8+
{
9+
$order = new \Tests\Fixtures\Record\Order();
10+
$this->assertFalse($order->loaded());
11+
$this->assertFalse(empty($order->ship_address));
12+
$order->ship_address = '';
13+
$this->assertTrue(empty($order->ship_address));
14+
$this->assertTrue(empty($order->fred));
15+
$this->assertFalse(isset($order->fred));
16+
$this->expectException(\PHPFUI\ORM\Exception::class);
17+
$order->fred = 'Fred';
18+
}
19+
20+
public function testEmpty() : void
21+
{
22+
$order = new \Tests\Fixtures\Record\Order(30);
23+
$this->assertTrue($order->loaded());
24+
$this->assertFalse(empty($order->employee));
25+
$order->employee_id = 0;
26+
$this->assertTrue(empty($order->employee));
27+
$this->assertTrue(empty($order->employee_id));
28+
$order->employee = new \Tests\Fixtures\Record\Employee(1);
29+
$this->assertFalse(empty($order->employee));
30+
$this->assertFalse(empty($order->employee_id));
31+
$order->employee = new \Tests\Fixtures\Record\Employee();
32+
$this->assertTrue(empty($order->employee));
33+
$this->assertTrue(empty($order->employee_id));
34+
}
35+
}

src/PHPFUI/ORM/DataObject.php

+30-11
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ public function __get(string $field) : mixed
3333
throw new \PHPFUI\ORM\Exception(self::class . " {$field} is not a valid field");
3434
}
3535

36+
/**
37+
* Allows for empty($object->field) to work correctly
38+
*/
39+
public function __isset(string $field) : bool
40+
{
41+
return \array_key_exists($field, $this->current) || ! empty($this->current[$field . \PHPFUI\ORM::$idSuffix]);
42+
}
43+
3644
public function __set(string $field, mixed $value) : void
3745
{
3846
if (! \array_key_exists($field, $this->current))
@@ -50,30 +58,30 @@ public function empty() : bool
5058

5159
public function isset(string $field) : bool
5260
{
53-
return \array_key_exists($field, $this->current);
61+
return $this->__isset($field);
5462
}
5563

5664
public function offsetExists($offset) : bool
5765
{
5866
return \array_key_exists($offset, $this->current);
5967
}
6068

69+
/**
70+
* Low level get access to underlying data to implement ArrayAccess
71+
*/
6172
public function offsetGet($offset) : mixed
6273
{
63-
if (\array_key_exists($offset, $this->current))
64-
{
65-
return $this->current[$offset];
66-
}
74+
$this->validateFieldExists($offset);
6775

68-
throw new \PHPFUI\ORM\Exception(self::class . " {$offset} is not defined");
76+
return $this->current[$offset] ?? null;
6977
}
7078

71-
public function offsetSet($offset, $value) : void
79+
/**
80+
* Low level set access to underlying data to implement ArrayAccess
81+
*/
82+
public function offsetSet($offset, $value) : void
7283
{
73-
if (! \array_key_exists($offset, $this->current))
74-
{
75-
throw new \PHPFUI\ORM\Exception(self::class . " {$offset} is not defined");
76-
}
84+
$this->validateFieldExists($offset);
7785
$this->current[$offset] = $value;
7886
}
7987

@@ -87,4 +95,15 @@ public function toArray() : array
8795
{
8896
return $this->current;
8997
}
98+
99+
protected function validateFieldExists(string $field) : void
100+
{
101+
if (! $this->__isset($field))
102+
{
103+
$message = static::class . "::{$field} is not a valid field";
104+
\PHPFUI\ORM::log(\Psr\Log\LogLevel::ERROR, $message);
105+
106+
throw new \PHPFUI\ORM\Exception($message);
107+
}
108+
}
90109
}

src/PHPFUI/ORM/Record.php

+20-33
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,11 @@ public function __get(string $field) : mixed
127127
}
128128

129129
/**
130-
* Allows for empty($object->field) to work correctly
130+
* @inherit
131131
*/
132132
public function __isset(string $field) : bool
133133
{
134-
return (bool)(\array_key_exists($field, $this->current) || \array_key_exists($field, static::$virtualFields) || \array_key_exists($field . \PHPFUI\ORM::$idSuffix, $this->current));
134+
return (bool)(parent::__isset($field) || \array_key_exists($field, static::$virtualFields));
135135
}
136136

137137
public function __set(string $field, mixed $value) : void
@@ -153,8 +153,14 @@ public function __set(string $field, mixed $value) : void
153153
{
154154
$haveType = $value->getTableName();
155155

156-
if ($value instanceof \PHPFUI\ORM\Record && $field == $haveType)
156+
if ($field == $haveType)
157157
{
158+
if ($value->empty())
159+
{
160+
$this->current[$id] = 0;
161+
162+
return;
163+
}
158164
$this->empty = false;
159165

160166
if (empty($value->{$id}))
@@ -443,25 +449,6 @@ public function loadFromSQL(string $sql, array $input = []) : bool
443449
return true;
444450
}
445451

446-
/**
447-
* Low level get access to underlying data to implement ArrayAccess
448-
*/
449-
public function offsetGet($offset) : mixed
450-
{
451-
$this->validateFieldExists($offset);
452-
453-
return $this->current[$offset] ?? null;
454-
}
455-
456-
/**
457-
* Low level set access to underlying data to implement ArrayAccess
458-
*/
459-
public function offsetSet($offset, $value) : void
460-
{
461-
$this->validateFieldExists($offset);
462-
$this->current[$offset] = $value;
463-
}
464-
465452
/**
466453
* Read a record from the db. If more than one match, only the first is loaded.
467454
*
@@ -734,6 +721,17 @@ protected function timeStamp(?int $timeStamp) : string
734721
return \date('Y-m-d g:i a', $timeStamp);
735722
}
736723

724+
protected function validateFieldExists(string $field) : void
725+
{
726+
if (! isset(static::$fields[$field]))
727+
{
728+
$message = static::class . "::{$field} is not a valid field";
729+
\PHPFUI\ORM::log(\Psr\Log\LogLevel::ERROR, $message);
730+
731+
throw new \PHPFUI\ORM\Exception($message);
732+
}
733+
}
734+
737735
/**
738736
* Build a where clause
739737
*
@@ -878,15 +876,4 @@ private function privateInsert(bool $updateOnDuplicate, string $ignore = '') : i
878876

879877
return $returnValue;
880878
}
881-
882-
private function validateFieldExists(string $field) : void
883-
{
884-
if (! isset(static::$fields[$field]))
885-
{
886-
$message = static::class . "::{$field} is not a valid field";
887-
\PHPFUI\ORM::log(\Psr\Log\LogLevel::ERROR, $message);
888-
889-
throw new \PHPFUI\ORM\Exception($message);
890-
}
891-
}
892879
}

0 commit comments

Comments
 (0)