Skip to content

Commit

Permalink
Refactor populateRecord() to split ArrayAccess implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov committed May 25, 2024
1 parent 804ca3f commit 98295c7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
4 changes: 4 additions & 0 deletions src/AbstractActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,10 @@ public function optimisticLock(): string|null
*/
public function populateRecord(array|object $row): void
{
if ($row instanceof ActiveRecordInterface) {
$row = $row->getAttributes();

Check warning on line 537 in src/AbstractActiveRecord.php

View check run for this annotation

Codecov / codecov/patch

src/AbstractActiveRecord.php#L537

Added line #L537 was not covered by tests
}

foreach ($row as $name => $value) {
$this->populateAttribute($name, $value);
$this->oldAttributes[$name] = $value;
Expand Down
27 changes: 27 additions & 0 deletions src/ArArrayHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
namespace Yiisoft\ActiveRecord;

use Closure;
use Traversable;

use function array_combine;
use function array_key_exists;
use function array_map;
use function get_object_vars;
use function is_array;
use function iterator_to_array;
use function property_exists;
use function strrpos;
use function substr;
Expand Down Expand Up @@ -162,4 +165,28 @@ public static function index(array $rows, Closure|string|null $indexBy = null):

return $result;
}

/**
* Converts an object into an array.
*
* @param array|object $object The object to be converted into an array.
*
* @return array The array representation of the object.
*/
public static function toArray(array|object $object): array
{
if (is_array($object)) {
return $object;
}

if ($object instanceof ActiveRecordInterface) {
return $object->getAttributes();
}

if ($object instanceof Traversable) {
return iterator_to_array($object);

Check warning on line 187 in src/ArArrayHelper.php

View check run for this annotation

Codecov / codecov/patch

src/ArArrayHelper.php#L186-L187

Added lines #L186 - L187 were not covered by tests
}

return get_object_vars($object);

Check warning on line 190 in src/ArArrayHelper.php

View check run for this annotation

Codecov / codecov/patch

src/ArArrayHelper.php#L190

Added line #L190 was not covered by tests
}
}
11 changes: 5 additions & 6 deletions src/BaseActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,16 +162,15 @@ public function loadDefaultValues(bool $skipIfSet = true): self

public function populateRecord(array|object $row): void
{
$row = ArArrayHelper::toArray($row);
$columns = $this->getTableSchema()->getColumns();
$rowColumns = array_intersect_key($row, $columns);

/** @psalm-var array[][] $row */
foreach ($row as $name => $value) {
if (isset($columns[$name])) {
$row[$name] = $columns[$name]->phpTypecast($value);
}
foreach ($rowColumns as $name => &$value) {
$value = $columns[$name]->phpTypecast($value);
}

parent::populateRecord($row);
parent::populateRecord($rowColumns + $row);
}

public function primaryKey(): array
Expand Down

0 comments on commit 98295c7

Please sign in to comment.