Skip to content

[Config] Add ifFalse() #59325

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

Merged
merged 1 commit into from
Jan 2, 2025
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
5 changes: 5 additions & 0 deletions src/Symfony/Component/Config/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.3
---

* Add `ExprBuilder::ifFalse()`

7.2
---

Expand Down
15 changes: 15 additions & 0 deletions src/Symfony/Component/Config/Definition/Builder/ExprBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,21 @@ public function ifTrue(?\Closure $closure = null): static
return $this;
}

/**
* Sets a closure to use as tests.
*
* The default one tests if the value is false.
*
* @return $this
*/
public function ifFalse(?\Closure $closure = null): static
{
$this->ifPart = $closure ?? static fn ($v) => false === $v;
$this->allowedTypes = self::TYPE_ANY;

return $this;
}

/**
* Tests if the value is a string.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,27 @@ public function testIfTrueExpression()
$this->assertFinalizedValueIs('value', $test);
}

public function testIfFalseExpression()
{
$test = $this->getTestBuilder()
->ifFalse()
->then($this->returnClosure('new_value'))
->end();
$this->assertFinalizedValueIs('new_value', $test, ['key' => false]);

$test = $this->getTestBuilder()
->ifFalse(fn () => true)
->then($this->returnClosure('new_value'))
->end();
$this->assertFinalizedValueIs('new_value', $test);

$test = $this->getTestBuilder()
->ifFalse(fn () => false)
->then($this->returnClosure('new_value'))
->end();
$this->assertFinalizedValueIs('value', $test);
}

public function testIfStringExpression()
{
$test = $this->getTestBuilder()
Expand Down