Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor populateRecord() to split ArrayAccess implementation #350

Merged
merged 1 commit into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 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 @@

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
Loading