Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F3787635
D3222.id8257.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
6 KB
Referenced Files
None
Subscribers
None
D3222.id8257.diff
View Options
diff --git a/omnitools/src/Booleans/Boolean.php b/omnitools/src/Booleans/Boolean.php
new file mode 100644
--- /dev/null
+++ b/omnitools/src/Booleans/Boolean.php
@@ -0,0 +1,109 @@
+<?php
+declare(strict_types=1);
+
+namespace Keruald\OmniTools\Booleans;
+
+class Boolean {
+
+ ///
+ /// Properties
+ ///
+
+ private bool $value;
+
+ ///
+ /// Constructors
+ ///
+
+ public function __construct (bool $value) {
+ $this->value = $value;
+ }
+
+ public static function true () : self {
+ return new self(true);
+ }
+
+ public static function false () : self {
+ return new self(false);
+ }
+
+ ///
+ /// Basic logic operators
+ ///
+
+ public function and (self|bool $other) : self {
+ if ($this->value === true) {
+ $this->value = self::toScalar($other);
+ }
+
+ return $this;
+ }
+
+ public function or (self|bool $other) : self {
+ if ($this->value === false) {
+ $this->value = self::toScalar($other);
+ }
+
+ return $this;
+ }
+
+ public function xor (self|bool $other) : self {
+ $this->value = ($this->value xor self::toScalar($other));
+
+ return $this;
+ }
+
+ public function not () : self {
+ $this->value = !$this->value;
+
+ return $this;
+ }
+
+ public function implication (self|bool $other) : self {
+ $this->value = $this->value === false || self::toScalar($other);
+
+ return $this;
+ }
+
+ public function equivalence (self|bool $other) : self {
+ $this->value = $this->isEqualsTo($other);
+
+ return $this;
+ }
+
+ ///
+ /// Comparison
+ ///
+
+ public function isEqualsTo (self|bool $other) : bool {
+ return $this->value === self::toScalar($other);
+ }
+
+ ///
+ /// Type convert
+ ///
+
+ public function asBool () : bool {
+ return $this->value;
+ }
+
+ public function asInteger () : int {
+ return (int)$this->value;
+ }
+
+ public function asString () : string {
+ return match ($this->value) {
+ true => "true",
+ false => "false",
+ };
+ }
+
+ public static function toScalar (self|bool $bool) : bool {
+ if ($bool instanceof self) {
+ return $bool->value;
+ }
+
+ return (bool)$bool;
+ }
+
+}
diff --git a/omnitools/tests/Booleans/BooleanTest.php b/omnitools/tests/Booleans/BooleanTest.php
new file mode 100644
--- /dev/null
+++ b/omnitools/tests/Booleans/BooleanTest.php
@@ -0,0 +1,139 @@
+<?php
+
+namespace Keruald\OmniTools\Tests\Booleans;
+
+use Keruald\OmniTools\Booleans\Boolean;
+
+use PHPUnit\Framework\Attributes\DataProvider;
+use PHPUnit\Framework\TestCase;
+
+class BooleanTest extends TestCase {
+
+ ///
+ /// Constructors
+ ///
+
+ public function testConstruct () {
+ $this->assertTrue((new Boolean(true))->asBool());
+ $this->assertFalse((new Boolean(false))->asBool());
+ }
+
+ public function testTrue () {
+ $this->assertTrue(Boolean::true()->asBool());
+ }
+
+ public function testFalse () {
+ $this->assertFalse(Boolean::false()->asBool());
+ }
+
+ ///
+ /// Operations
+ ///
+
+ public static function provideAnd () : iterable {
+ yield [true, true, true];
+ yield [true, false, false];
+ yield [false, true, false];
+ yield [false, false, false];
+ }
+
+ #[DataProvider("provideAnd")]
+ public function testAnd ($left, $right, $result) {
+ $bool = new Boolean($left);
+
+ $this->assertEquals($result, $bool->and($right)->asBool());
+ }
+
+ public static function provideOr () : iterable {
+ yield [true, true, true];
+ yield [true, false, true];
+ yield [false, true, true];
+ yield [false, false, false];
+ }
+
+ #[DataProvider("provideOr")]
+ public function testOr ($left, $right, $result) {
+ $bool = new Boolean($left);
+
+ $this->assertEquals($result, $bool->or($right)->asBool());
+ }
+
+ public function testNot () {
+ $this->assertFalse(Boolean::true()->not()->asBool());
+ $this->assertTrue(Boolean::false()->not()->asBool());
+ }
+
+ public static function provideXor () : iterable {
+ yield [true, true, false];
+ yield [true, false, true];
+ yield [false, true, true];
+ yield [false, false, false];
+ }
+
+ #[DataProvider("provideXor")]
+ public function testXor ($left, $right, $result) {
+ $bool = new Boolean($left);
+
+ $this->assertEquals($result, $bool->xor($right)->asBool());
+ }
+
+ public static function provideImplications () : iterable {
+ yield [true, true, true];
+ yield [true, false, false];
+ yield [false, true, true];
+ yield [false, false, true];
+ }
+
+ #[DataProvider("provideImplications")]
+ public function testImplication ($left, $right, $result) {
+ $bool = new Boolean($left);
+
+ $this->assertEquals($result, $bool->implication($right)->asBool());
+ }
+
+ public static function provideEquivalence () : iterable {
+ yield [true, true, true];
+ yield [true, false, false];
+ yield [false, true, false];
+ yield [false, false, true];
+ }
+
+ #[DataProvider("provideEquivalence")]
+ public function testEquivalence ($left, $right, $result) {
+ $bool = new Boolean($left);
+
+ $this->assertEquals($result, $bool->equivalence($right)->asBool());
+ }
+
+ ///
+ /// Type convert
+ ///
+
+ public function testAsBool () {
+ $this->assertTrue((new Boolean(true))->asBool());
+ $this->assertFalse((new Boolean(false))->asBool());
+ }
+
+ public function testAsInteger () {
+ $this->assertEquals(1, (new Boolean(true))->asInteger());
+ $this->assertEquals(0, (new Boolean(false))->asInteger());
+ }
+
+ public function testAsString () {
+ $this->assertEquals("true", (new Boolean(true))->asString());
+ $this->assertEquals("false", (new Boolean(false))->asString());
+ }
+
+ public static function provideScalars () : iterable {
+ yield [true, true];
+ yield [Boolean::true(), true];
+ yield [false, false];
+ yield [Boolean::false(), false];
+ }
+
+ #[DataProvider("provideScalars")]
+ public function testToScalar ($scalar, $expected) {
+ $this->assertEquals($expected, Boolean::toScalar($scalar));
+ }
+
+}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Wed, Nov 27, 18:55 (20 h, 36 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2267590
Default Alt Text
D3222.id8257.diff (6 KB)
Attached To
Mode
D3222: Provide boolean object to compute
Attached
Detach File
Event Timeline
Log In to Comment