Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F12507812
D3818.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
3 KB
Referenced Files
None
Subscribers
None
D3818.diff
View Options
diff --git a/omnitools/src/DataTypes/Option/Option.php b/omnitools/src/DataTypes/Option/Option.php
--- a/omnitools/src/DataTypes/Option/Option.php
+++ b/omnitools/src/DataTypes/Option/Option.php
@@ -11,4 +11,23 @@
public abstract function map(callable $callable) : self;
public abstract function orElse(mixed $default) : mixed;
+
+ ///
+ /// Helper to build options
+ ///
+
+ /**
+ * Converts a nullable value to an Option.
+ *
+ * @param mixed $value
+ *
+ * @return Option An instance of None if the value is null; otherwise, an instance of Some.
+ */
+ public static function from (mixed $value) : self {
+ return match ($value) {
+ null => new None,
+ default => new Some($value),
+ };
+ }
+
}
diff --git a/omnitools/src/OS/Environment.php b/omnitools/src/OS/Environment.php
--- a/omnitools/src/OS/Environment.php
+++ b/omnitools/src/OS/Environment.php
@@ -4,6 +4,8 @@
use InvalidArgumentException;
+use Keruald\OmniTools\DataTypes\Option\Option;
+
class Environment {
public static function has (string $key) : bool {
@@ -22,6 +24,19 @@
return $_ENV[$key] ?? $_SERVER[$key];
}
+ /**
+ * Try to get an environment value
+ *
+ * @param string $key The key of the environment value to get
+ *
+ * @return Option Some<string> if found, None if not found
+ */
+ public static function tryGet (string $key) : Option {
+ $value = $_ENV[$key] ?? $_SERVER[$key] ?? null;
+
+ return Option::from($value);
+ }
+
public static function getOr (string $key, string $default) : string {
return $_ENV[$key] ?? $_SERVER[$key] ?? $default;
}
diff --git a/omnitools/tests/DataTypes/Option/OptionTest.php b/omnitools/tests/DataTypes/Option/OptionTest.php
new file mode 100644
--- /dev/null
+++ b/omnitools/tests/DataTypes/Option/OptionTest.php
@@ -0,0 +1,23 @@
+<?php
+
+namespace Keruald\OmniTools\Tests\DataTypes\Option;
+
+use Keruald\OmniTools\DataTypes\Option\None;
+use Keruald\OmniTools\DataTypes\Option\Option;
+use Keruald\OmniTools\DataTypes\Option\Some;
+
+use PHPUnit\Framework\TestCase;
+
+class OptionTest extends TestCase {
+
+ public function testFrom () : void {
+ $this->assertEquals(new Some(42), Option::from(42));
+ }
+
+ public function testFromWhenValueIsNull () : void {
+ $actual = Option::from(null);
+
+ $this->assertTrue($actual->isNone());
+ }
+
+}
diff --git a/omnitools/tests/OS/EnvironmentTest.php b/omnitools/tests/OS/EnvironmentTest.php
--- a/omnitools/tests/OS/EnvironmentTest.php
+++ b/omnitools/tests/OS/EnvironmentTest.php
@@ -3,6 +3,7 @@
namespace Keruald\OmniTools\Tests\OS;
use InvalidArgumentException;
+use Keruald\OmniTools\DataTypes\Option\Some;
use Keruald\OmniTools\OS\Environment;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
@@ -43,6 +44,19 @@
self::assertSame($value, Environment::get($key));
}
+ #[DataProvider('provideEnvironment')]
+ public function testTryGet (string $key, string $value) : void {
+ $actual = Environment::tryGet($key);
+
+ $this->assertEquals(new Some($value), $actual);
+ }
+
+ public function testTryGetWhenKeyDoesNotExist () : void {
+ $actual = Environment::tryGet("quux");
+
+ $this->assertTrue($actual->isNone());
+ }
+
#[DataProvider('provideEnvironment')]
public function testGetOrWhenKeyExists (string $key, string $value) : void {
self::assertSame($value, Environment::getOr($key, "default"));
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sun, Nov 9, 22:58 (2 h, 47 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3152609
Default Alt Text
D3818.diff (3 KB)
Attached To
Mode
D3818: Add support for Option::from() and Environment::tryGet()
Attached
Detach File
Event Timeline
Log In to Comment