8000 feature #59325 [Config] Add `ifFalse()` (OskarStark) · symfony/symfony@348781c · GitHub
[go: up one dir, main page]

Skip to content

Commit 348781c

Browse files
committed
feature #59325 [Config] Add ifFalse() (OskarStark)
This PR was squashed before being merged into the 7.3 branch. Discussion ---------- [Config] Add `ifFalse()` | Q | A | ------------- | --- | Branch? | 7.3 | Bug fix? | no | New feature? | yes | Deprecations? | no | Issues | -- | License | MIT Spottet while working on * php-llm/llm-chain-bundle#55 It is much easier to use like IMHO: ```php ->ifTrue(fn ($v) => false === $v) ``` Commits ------- b85beb6 [Config] Add `ifFalse()`
2 parents 5bc8c12 + b85beb6 commit 348781c

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

src/Symfony/Component/Config/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
7.3
5+
---
6+
7+
* Add `ExprBuilder::ifFalse()`
8+
49
7.2
510
---
611

src/Symfony/Component/Config/Definition/Builder/ExprBuilder.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,21 @@ public function ifTrue(?\Closure $closure = null): static
6767
return $this;
6868
}
6969

70+
/**
71+
* Sets a closure to use as tests.
72+
*
73+
* The default one tests if the value is false.
74+
*
75+
* @return $this
76+
*/
77+
public function ifFalse(?\Closure $closure = null): static
78+
{
79+
$this->ifPart = $closure ?? static fn ($v) => false === $v;
80+
$this->allowedTypes = self::TYPE_ANY;
81+
82+
return $this;
83+
}
84+
7085
/**
7186
* Tests if the value is a string.
7287
*

src/Symfony/Component/Config/Tests/Definition/Builder/ExprBuilderTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,27 @@ public function testIfTrueExpression()
4949
$this->assertFinalizedValueIs('value', $test);
5050
}
5151

52+
public function testIfFalseExpression()
53+
{
54+
$test = $this->getTestBuilder()
55+
->ifFalse()
56+
->then($this->returnClosure('new_value'))
57+
->end();
58+
$this->assertFinalizedValueIs('new_value', $test, ['key' => false]);
59+
60+
$test = $this->getTestBuilder()
61+
->ifFalse(fn () => true)
62+
->then($this->returnClosure('new_value'))
63+
->end();
64+
$this->assertFinalizedValueIs('new_value', $test);
65+
66+
$test = $this->getTestBuilder()
67+
->ifFalse(fn () => false)
68+
->then($this->returnClosure('new_value'))
69+
->end();
70+
$this->assertFinalizedValueIs('value', $test);
71+
}
72+
5273
public function testIfStringExpression()
5374
{
5475
$test = $this->getTestBuilder()

0 commit comments

Comments
 (0)
0