Skip to content

Commit af89fad

Browse files
authored
Merge pull request #271 from thekid/feature/null-expansion
Pass NULL to Properties::expanding() to ignore expansion
2 parents 454d7a0 + 5f97de4 commit af89fad

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

src/main/php/util/Properties.class.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,20 @@ public function __construct(string $filename= null) {
4040
}
4141

4242
/**
43-
* Add expansion `${kind.X}` with a given expansion function `f(X)`
43+
* Add expansion `${kind.X}` with a given expansion function `f(X)`. Pass NULL
44+
* as expansion to ignore any expansions of this type.
4445
*
4546
* @param string $kind
46-
* @param [:var]|function(string): string $expansion
47+
* @param ?[:var]|function(string): string $expansion
4748
* @return self
4849
*/
4950
public function expanding($kind, $expansion) {
5051
$this->expansion= $this->expansion ?: clone self::$env;
5152

52-
if ($expansion instanceof \ArrayAccess || (is_array($expansion) && 0 !== key($expansion))) {
53-
$func= function($name) use($expansion) { return isset($expansion[$name]) ? $expansion[$name] : null; };
53+
if (null === $expansion) {
54+
$func= function($name) { return ''; };
55+
} else if ($expansion instanceof \ArrayAccess || (is_array($expansion) && 0 !== key($expansion))) {
56+
$func= function($name) use($expansion) { return $expansion[$name] ?? null; };
5457
} else {
5558
$func= cast($expansion, 'function(string): string');
5659
}

src/test/php/net/xp_framework/unittest/util/PropertyExpansionTest.class.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ function offsetUnset($key) { /* Not implemented */ }
4949
$this->assertEquals('test', $prop->readString('section', 'test'));
5050
}
5151

52+
#[Test]
53+
public function null_lookup_ignores_missing_expansion() {
54+
$prop= $this->newFixture(null);
55+
$this->assertEquals('', $prop->readString('section', 'test'));
56+
}
57+
5258
#[Test, Expect(ElementNotFoundException::class), Values([null, false])]
5359
public function non_existant_lookup($return) {
5460
$this->newFixture(function($name) use($return) { return $return; })->readString('section', 'test');

0 commit comments

Comments
 (0)