Skip to content

Commit

Permalink
Drop entity parameter from {HasOneSql, HasMany}::refLink() method
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Mar 9, 2024
1 parent 190abc6 commit 3e835dd
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/Model/ReferencesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,14 @@ public function ref(string $link, array $defaults = []): Model
}

/**
* Traverse reference and create their model but keep condition not materialized (for subquery actions).
* Traverse reference and create their model but keep reference condition not materialized (for subquery actions).
*
* @param array<string, mixed> $defaults
*/
public function refLink(string $link, array $defaults = []): Model
{
$reference = $this->getModel(true)->getReference($link);

return $reference->refLink($this, $defaults);
return $reference->refLink($defaults);
}
}
14 changes: 7 additions & 7 deletions src/Model/Scope/Condition.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,12 +287,14 @@ public function negate(): self
#[\Override]
public function toWords(Model $model = null): string
{
if ($model === null) {
if ($model !== null) {
$model->assertIsModel();
} else {
$model = $this->getModel();
}

if ($model === null) {
throw new Exception('Condition must be associated with Model to convert to words');
if ($model === null) {
throw new Exception('Condition must be associated with model to convert to words');
}
}

$field = $this->fieldToWords($model);
Expand Down Expand Up @@ -405,9 +407,7 @@ protected function valueToWords(Model $model, $value): string
$title = null;
if ($field instanceof Field && $field->hasReference()) {
// make sure we set the value in the Model
$entity = $model->isEntity()
? clone $model
: $model->createEntity();
$entity = $model->createEntity();
$entity->set($field->shortName, $value);

// then take the title
Expand Down
16 changes: 6 additions & 10 deletions src/Reference/HasMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,8 @@ public function ref(Model $ourModelOrEntity, array $defaults = []): Model
*
* @param array<string, mixed> $defaults
*/
public function refLink(?Model $ourModelOrEntity, array $defaults = []): Model
public function refLink(array $defaults = []): Model
{
if ($ourModelOrEntity !== null) { // TODO drop this parameter, refLink for entity is useless
$this->assertOurModelOrEntity($ourModelOrEntity);
}

$theirModelLinked = $this->createTheirModel($defaults)->addCondition(
$this->getTheirFieldName(),
$this->referenceOurValue()
Expand Down Expand Up @@ -136,7 +132,7 @@ public function addField(string $fieldName, array $defaults = []): Field

if (isset($defaults['expr'])) {
$fx = function () use ($defaults, $alias) {
$theirModelLinked = $this->refLink(null);
$theirModelLinked = $this->refLink();

return $theirModelLinked->action('field', [$theirModelLinked->expr(
$defaults['expr'],
Expand All @@ -146,15 +142,15 @@ public function addField(string $fieldName, array $defaults = []): Field
unset($defaults['args']);
} elseif (is_object($defaults['aggregate'])) {
$fx = function () use ($defaults, $alias) {
return $this->refLink(null)->action('field', [$defaults['aggregate'], 'alias' => $alias]);
return $this->refLink()->action('field', [$defaults['aggregate'], 'alias' => $alias]);
};
} elseif ($defaults['aggregate'] === 'count' && !isset($defaults['field'])) {
$fx = function () use ($alias) {
return $this->refLink(null)->action('count', ['alias' => $alias]);
return $this->refLink()->action('count', ['alias' => $alias]);
};
} elseif (in_array($defaults['aggregate'], ['sum', 'avg', 'min', 'max', 'count'], true)) {
$fx = function () use ($defaults, $field) {
return $this->refLink(null)->action('fx0', [$defaults['aggregate'], $field]);
return $this->refLink()->action('fx0', [$defaults['aggregate'], $field]);
};
} else {
$fx = function () use ($defaults, $field) {
Expand All @@ -163,7 +159,7 @@ public function addField(string $fieldName, array $defaults = []): Field
$args['concatSeparator'] = $defaults['concatSeparator'];
}

return $this->refLink(null)->action('fx', $args);
return $this->refLink()->action('fx', $args);
};
}

Expand Down
2 changes: 1 addition & 1 deletion src/Reference/HasOneSql.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public function ref(Model $ourModelOrEntity, array $defaults = []): Model
*
* @param array<string, mixed> $defaults
*/
public function refLink(Model $ourModelOrEntity, array $defaults = []): Model
public function refLink(array $defaults = []): Model
{
$theirModel = $this->createTheirModel($defaults);

Expand Down
2 changes: 1 addition & 1 deletion tests/ScopeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ public function testConditionUnsupportedToWords(): void
$condition = new Condition('name', 'abc');

$this->expectException(Exception::class);
$this->expectExceptionMessage('Condition must be associated with Model to convert to words');
$this->expectExceptionMessage('Condition must be associated with model to convert to words');
$condition->toWords();
}

Expand Down

0 comments on commit 3e835dd

Please sign in to comment.