Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov committed May 20, 2024
1 parent 107cd30 commit d63dcd9
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 29 deletions.
6 changes: 3 additions & 3 deletions src/ActiveQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public function prepare(QueryBuilderInterface $builder): QueryInterface
if ($viaCallableUsed) {
$viaModels = $viaQuery->all();
} elseif ($this->primaryModel->isRelationPopulated($viaName)) {
$viaModels = $this->primaryModel->$viaName;
$viaModels = $this->primaryModel->relation($viaName);
} else {
$viaModels = $viaQuery->all();
$this->primaryModel->populateRelation($viaName, $viaModels);
Expand All @@ -214,7 +214,7 @@ public function prepare(QueryBuilderInterface $builder): QueryInterface
if ($viaCallableUsed) {
$model = $viaQuery->onePopulate();
} elseif ($this->primaryModel->isRelationPopulated($viaName)) {
$model = $this->primaryModel->$viaName;
$model = $this->primaryModel->relation($viaName);
} else {
$model = $viaQuery->onePopulate();
$this->primaryModel->populateRelation($viaName, $model);
Expand Down Expand Up @@ -927,7 +927,7 @@ protected function findByCondition(mixed $condition): static
$condition = [$condition];
}

if (!DbArrayHelper::isAssociative($condition) && !$condition instanceof ExpressionInterface) {
if (!DbArrayHelper::isAssociative($condition)) {
/** query by primary key */
$primaryKey = $arInstance->primaryKey();

Expand Down
2 changes: 0 additions & 2 deletions src/ActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@
* To declare an ActiveRecord class you need to extend {@see ActiveRecord} and implement the `getTableName` method:
*
* ```php
* <?php
*
* class Customer extends ActiveRecord
* {
* public static function getTableName(): string
Expand Down
16 changes: 1 addition & 15 deletions src/ActiveRecordInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ interface ActiveRecordInterface
*
* The default implementation will return all column names of the table associated with this AR class.
*
* @throws InvalidConfigException
* @throws Exception
*
* @return array List of attribute names.
*
* @psalm-return string[]
Expand Down Expand Up @@ -77,7 +74,7 @@ public function deleteAll(array $condition = []): int;
* Returns a value indicating whether the given active record is the same as the current one.
*
* The comparison is made by comparing the table names and the primary key values of the two active records. If one
* of the records {@see isNewRecord|is new} they're also considered not equal.
* of the records {@see getIsNewRecord|is new} they're also considered not equal.
*
* @param self $record Record to compare to.
*
Expand Down Expand Up @@ -305,17 +302,6 @@ public function isRelationPopulated(string $name): bool;
*/
public function link(string $name, self $arClass, array $extraColumns = []): void;

/**
* Returns the element at the specified offset.
*
* This method is required by the SPL interface {@see ArrayAccess}.
*
* It's implicitly called when you use something like `$value = $model[$offset];`.
*
* @return mixed the element at the offset, null if no element is found at the offset
*/
public function offsetGet(mixed $offset): mixed;

/**
* Populates the named relation with the related records.
*
Expand Down
27 changes: 18 additions & 9 deletions src/ActiveRelationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public function via(string $relationName, callable $callable = null): static
$callableUsed = $callable !== null;
$this->via = [$relationName, $relation, $callableUsed];

if ($callable !== null) {
if ($callableUsed) {
$callable($relation);
}

Expand Down Expand Up @@ -164,13 +164,10 @@ public function inverseOf(string $relationName): static
}

/**
* Finds the related records for the specified primary record.
* Returns query records depends on {@see $multiple} .
*
* This method is invoked when a relation of an ActiveRecord is being accessed in a lazy fashion.
*
* @param string $name the relation name.
* @param ActiveRecordInterface $model the primary model.
*
* @throws Exception
* @throws InvalidArgumentException
* @throws InvalidConfigException
Expand Down Expand Up @@ -612,9 +609,19 @@ private function getModelKey(ActiveRecordInterface|array $activeRecord, array $a
{
$key = [];

foreach ($attributes as $attribute) {
if (isset($activeRecord[$attribute]) || (is_object($activeRecord) && property_exists($activeRecord, $attribute))) {
$key[] = $this->normalizeModelKey($activeRecord[$attribute]);
if (is_array($activeRecord)) {
foreach ($attributes as $attribute) {
if (isset($activeRecord[$attribute])) {
$key[] = $this->normalizeModelKey($activeRecord[$attribute]);
}
}
} else {
foreach ($attributes as $attribute) {
$value = $activeRecord->getAttribute($attribute);

if ($value !== null) {
$key[] = $this->normalizeModelKey($value);
}
}
}

Expand All @@ -628,9 +635,11 @@ private function getModelKey(ActiveRecordInterface|array $activeRecord, array $a
}

/**
* @param int|null|string|Stringable $value raw key value.
*
* @return int|string|null normalized key value.
*/
private function normalizeModelKey(mixed $value): int|string|null
private function normalizeModelKey(int|null|string|Stringable $value): int|string|null
{
if ($value instanceof Stringable) {
/**
Expand Down

0 comments on commit d63dcd9

Please sign in to comment.