Skip to content

Commit

Permalink
Rename properties() to propertyNames(), values to `propertyValu…
Browse files Browse the repository at this point in the history
…es()`
  • Loading branch information
Tigrov committed Sep 12, 2024
1 parent 513b45d commit f0566eb
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 47 deletions.
6 changes: 3 additions & 3 deletions docs/create-model.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ Notes:

### Private properties

To use `private` properties inside the model class, you need to copy `valuesInternal()` and `populateProperty()`
To use `private` properties inside the model class, you need to copy `propertyValuesInternal()` and `populateProperty()`
methods from the `ActiveRecord` class and adjust them to work with the `private` properties.

```php
Expand All @@ -176,8 +176,8 @@ final class User extends ActiveRecord
// Getters and setters as for protected properties
// ...

// Copied `valuesInternal()` and `populateProperty()` methods from `ActiveRecord` class
protected function valuesInternal(): array
// Copied `propertyValuesInternal()` and `populateProperty()` methods from `ActiveRecord` class
protected function propertyValuesInternal(): array
{
return get_object_vars($this);
}
Expand Down
30 changes: 15 additions & 15 deletions src/AbstractActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ abstract class AbstractActiveRecord implements ActiveRecordInterface
*
* @psalm-return array<string, mixed>
*/
abstract protected function valuesInternal(): array;
abstract protected function propertyValuesInternal(): array;

/**
* Inserts Active Record values into DB without considering transaction.
Expand Down Expand Up @@ -104,18 +104,18 @@ public function equals(ActiveRecordInterface $record): bool

public function get(string $name): mixed
{
return $this->valuesInternal()[$name] ?? null;
return $this->propertyValuesInternal()[$name] ?? null;
}

public function values(array|null $names = null, array $except = []): array
public function propertyValues(array|null $names = null, array $except = []): array
{
$names ??= $this->properties();
$names ??= $this->propertyNames();

if (!empty($except)) {
$names = array_diff($names, $except);
}

return array_intersect_key($this->valuesInternal(), array_flip($names));
return array_intersect_key($this->propertyValuesInternal(), array_flip($names));
}

public function getIsNewRecord(): bool
Expand Down Expand Up @@ -145,13 +145,13 @@ public function oldValue(string $name): mixed
* The comparison of new and old values uses `===`.
*
* @param array|null $names The names of the properties whose values may be returned if they're changed recently.
* If null, {@see properties()} will be used.
* If null, {@see propertyNames()} will be used.
*
* @return array The changed property values (name-value pairs).
*/
public function dirtyValues(array|null $names = null): array
{
$values = $this->values($names);
$values = $this->propertyValues($names);

if ($this->oldValues === null) {
return $values;
Expand Down Expand Up @@ -232,7 +232,7 @@ public function getRelatedRecords(): array

public function hasProperty(string $name): bool
{
return in_array($name, $this->properties(), true);
return in_array($name, $this->propertyNames(), true);
}

/**
Expand Down Expand Up @@ -340,7 +340,7 @@ public function instantiateQuery(string|ActiveRecordInterface|Closure $arClass):
*/
public function isPropertyChanged(string $name, bool $identical = true): bool
{
$values = $this->valuesInternal();
$values = $this->propertyValuesInternal();

if (empty($this->oldValues) || !array_key_exists($name, $this->oldValues)) {
return array_key_exists($name, $values);
Expand Down Expand Up @@ -469,7 +469,7 @@ public function link(string $name, ActiveRecordInterface $arClass, array $extraC
$indexBy = $relation->getIndexBy();
if ($indexBy !== null) {
if ($indexBy instanceof Closure) {
$index = $indexBy($arClass->values());
$index = $indexBy($arClass->propertyValues());
} else {
$index = $arClass->get($indexBy);
}
Expand Down Expand Up @@ -535,7 +535,7 @@ public function optimisticLock(): string|null
public function populateRecord(array|object $row): void
{
if ($row instanceof ActiveRecordInterface) {
$row = $row->values();
$row = $row->propertyValues();
}

foreach ($row as $name => $value) {
Expand Down Expand Up @@ -630,11 +630,11 @@ public function set(string $name, mixed $value): void
*
* @param array $values Property values (name => value) to be assigned to the model.
*
* {@see properties()}
* {@see propertyNames()}
*/
public function populateProperties(array $values): void
{
$values = array_intersect_key($values, array_flip($this->properties()));
$values = array_intersect_key($values, array_flip($this->propertyNames()));

/** @psalm-var mixed $value */
foreach ($values as $name => $value) {
Expand All @@ -651,7 +651,7 @@ public function populateProperties(array $values): void
*/
public function setIsNewRecord(bool $value): void
{
$this->oldValues = $value ? null : $this->valuesInternal();
$this->oldValues = $value ? null : $this->propertyValuesInternal();
}

/**
Expand Down Expand Up @@ -1115,7 +1115,7 @@ protected function refreshInternal(array|ActiveRecordInterface|null $record = nu
return false;
}

foreach ($this->properties() as $name) {
foreach ($this->propertyNames() as $name) {
$this->populateProperty($name, $record->get($name));
}

Expand Down
4 changes: 2 additions & 2 deletions src/ActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
*/
class ActiveRecord extends AbstractActiveRecord
{
public function properties(): array
public function propertyNames(): array
{
return $this->getTableSchema()->getColumnNames();
}
Expand Down Expand Up @@ -235,7 +235,7 @@ protected function filterValidColumnNames(array $aliases): array
return $columnNames;
}

protected function valuesInternal(): array
protected function propertyValuesInternal(): array
{
return get_object_vars($this);
}
Expand Down
6 changes: 3 additions & 3 deletions src/ActiveRecordInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ interface ActiveRecordInterface
*
* @psalm-return string[]
*/
public function properties(): array;
public function propertyNames(): array;

/**
* Returns the abstract type of the property.
Expand Down Expand Up @@ -132,15 +132,15 @@ public function get(string $name): mixed;
* Returns property values.
*
* @param array|null $names List of property names whose value needs to be returned. Defaults to `null`, meaning all
* properties listed in {@see properties()} will be returned.
* properties listed in {@see propertyNames()} will be returned.
* @param array $except List of property names whose value shouldn't be returned.
*
* @throws Exception
* @throws InvalidConfigException
*
* @return array Property values (name => value).
*/
public function values(array|null $names = null, array $except = []): array;
public function propertyValues(array|null $names = null, array $except = []): array;

/**
* Returns a value indicating whether the current record is new (not saved in the database).
Expand Down
2 changes: 1 addition & 1 deletion src/ActiveRelationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ protected function filterByModels(array $models): void

if ($model instanceof ActiveRecordInterface) {
foreach ($models as $model) {
$value = $model->values($this->link);
$value = $model->propertyValues($this->link);

if (!empty($value)) {
$values[] = array_combine($columnNames, array_merge($nulls, $value));
Expand Down
2 changes: 1 addition & 1 deletion src/ArArrayHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public static function toArray(array|object $object): array
}

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

if ($object instanceof Traversable) {
Expand Down
6 changes: 3 additions & 3 deletions src/Trait/ArrayIteratorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
/**
* Trait to implement {@see IteratorAggregate} interface for ActiveRecord.
*
* @method array values(array|null $names = null, array $except = [])
* @see ActiveRecordInterface::values()
* @method array propertyValues(array|null $names = null, array $except = [])
* @see ActiveRecordInterface::propertyValues()
*/
trait ArrayIteratorTrait
{
Expand All @@ -24,7 +24,7 @@ trait ArrayIteratorTrait
*/
public function getIterator(): ArrayIterator
{
$values = $this->values();
$values = $this->propertyValues();

return new ArrayIterator($values);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Trait/ArrayableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
/**
* Trait to implement {@see \Yiisoft\Arrays\ArrayableInterface} interface for ActiveRecord.
*
* @method string[] properties()
* @see ActiveRecordInterface::properties()
* @method string[] propertyNames()
* @see ActiveRecordInterface::propertyNames()
*
* @method array getRelatedRecords()
* @see AbstractActiveRecord::getRelatedRecords()
Expand All @@ -40,7 +40,7 @@ public function extraFields(): array
*/
public function fields(): array
{
$fields = $this->properties();
$fields = $this->propertyNames();

return array_combine($fields, $fields);
}
Expand Down
16 changes: 8 additions & 8 deletions src/Trait/MagicPropertiesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@
*/
trait MagicPropertiesTrait
{
/** @psalm-var array<string, mixed> $properties */
private array $properties = [];
/** @psalm-var array<string, mixed> $propertyValues */
private array $propertyValues = [];

/**
* PHP getter magic method.
Expand Down Expand Up @@ -111,7 +111,7 @@ public function __isset(string $name): bool
public function __unset(string $name): void
{
if ($this->hasProperty($name)) {
unset($this->properties[$name]);
unset($this->propertyValues[$name]);

if ($this->hasDependentRelations($name)) {
$this->resetDependentRelations($name);
Expand Down Expand Up @@ -153,7 +153,7 @@ public function __set(string $name, mixed $value): void

public function hasProperty(string $name): bool
{
return isset($this->properties[$name]) || in_array($name, $this->properties(), true);
return isset($this->propertyValues[$name]) || in_array($name, $this->propertyNames(), true);
}

public function set(string $name, mixed $value): void
Expand Down Expand Up @@ -207,17 +207,17 @@ public function canSetProperty(string $name, bool $checkVars = true): bool
}

/** @psalm-return array<string, mixed> */
protected function valuesInternal(): array
protected function propertyValuesInternal(): array
{
return array_merge($this->properties, parent::valuesInternal());
return array_merge($this->propertyValues, parent::propertyValuesInternal());
}

protected function populateProperty(string $name, mixed $value): void
{
if ($name !== 'properties' && property_exists($this, $name)) {
if ($name !== 'propertyValues' && property_exists($this, $name)) {
$this->$name = $value;
} else {
$this->properties[$name] = $value;
$this->propertyValues[$name] = $value;
}
}
}
16 changes: 8 additions & 8 deletions tests/ActiveQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2128,7 +2128,7 @@ public function testPropertyValues(): void

$customer = new ActiveQuery(Customer::class);

$values = $customer->findOne(1)->values();
$values = $customer->findOne(1)->propertyValues();

$this->assertEquals($expectedValues, $values);
}
Expand All @@ -2139,7 +2139,7 @@ public function testPropertyValuesOnly(): void

$customer = new ActiveQuery(Customer::class);

$values = $customer->findOne(1)->values(['id', 'email', 'name']);
$values = $customer->findOne(1)->propertyValues(['id', 'email', 'name']);

$this->assertEquals(['id' => 1, 'email' => '[email protected]', 'name' => 'user1'], $values);
}
Expand All @@ -2150,7 +2150,7 @@ public function testPropertyValuesExcept(): void

$customer = new ActiveQuery(Customer::class);

$values = $customer->findOne(1)->values(null, ['status', 'bool_status', 'profile_id']);
$values = $customer->findOne(1)->propertyValues(null, ['status', 'bool_status', 'profile_id']);

$this->assertEquals(
['id' => 1, 'email' => '[email protected]', 'name' => 'user1', 'address' => 'address1'],
Expand All @@ -2166,7 +2166,7 @@ public function testGetOldValue(): void

$query = $customer->findOne(1);
$this->assertEquals('user1', $query->oldValue('name'));
$this->assertEquals($query->values(), $query->oldValues());
$this->assertEquals($query->propertyValues(), $query->oldValues());

$query->set('name', 'samdark');
$this->assertEquals('samdark', $query->get('name'));
Expand All @@ -2191,17 +2191,17 @@ public function testGetOldValues(): void
$customer = new ActiveQuery(Customer::class);

$query = $customer->findOne(1);
$this->assertEquals($expectedValues, $query->values());
$this->assertEquals($query->values(), $query->oldValues());
$this->assertEquals($expectedValues, $query->propertyValues());
$this->assertEquals($query->propertyValues(), $query->oldValues());

$query->set('name', 'samdark');

$expectedNewValues = $expectedValues;
$expectedNewValues['name'] = 'samdark';

$this->assertEquals($expectedNewValues, $query->values());
$this->assertEquals($expectedNewValues, $query->propertyValues());
$this->assertEquals($expectedValues, $query->oldValues());
$this->assertNotEquals($query->values(), $query->oldValues());
$this->assertNotEquals($query->propertyValues(), $query->oldValues());
}

public function testIsPropertyChanged(): void
Expand Down

0 comments on commit f0566eb

Please sign in to comment.