From 534917c0b7364a95689560a3a34cac2617f537d3 Mon Sep 17 00:00:00 2001 From: Kevin Millar Date: Wed, 25 Nov 2015 16:22:53 +1000 Subject: [PATCH 1/2] Added set operations (fixes #1). --- src/FlagSetTrait.php | 99 +++++++++++++++++++++++ test/suite/FlagSetTraitTest.php | 138 ++++++++++++++++++++++++++++++++ 2 files changed, 237 insertions(+) diff --git a/src/FlagSetTrait.php b/src/FlagSetTrait.php index 2865008..03d82d4 100644 --- a/src/FlagSetTrait.php +++ b/src/FlagSetTrait.php @@ -143,6 +143,8 @@ public function __get($name) /** * Get a string representation of the flag set. + * + * @return string The string representation. */ public function __toString() { @@ -172,6 +174,103 @@ public function __set($name, $value) throw new LogicException('Flag-sets are immutable.'); } + /** + * Compute the difference of flag-sets. + * + * A flag-set of the flags in $this and not in $other. + * + * @param self $other The other flag set to compare to. + * + * @return self + */ + public function diff(self $other) + { + $result = new self(); + foreach (get_object_vars($this) as $flag => $value) { + $otherValue = $other->{$flag}; + $result->{$flag} = ($value && !$otherValue); + } + + return $result; + } + + /** + * Compute the symmetric difference of flag-sets. + * + * A flag-set of the flags in $this and not in $other, and vice versa. + * + * @param self $other The other flag set to compare to. + * + * @return self + */ + public function symmetricDiff(self $other) + { + $result = new self(); + foreach (get_object_vars($this) as $flag => $value) { + $otherValue = $other->{$flag}; + $result->{$flag} = ($value !== $otherValue); + } + + return $result; + } + + /** + * Compute the intersection of flag-sets. + * + * A flag-set of the flags in $this and $other. + * + * @param self $other The other flag set to intersect with. + * + * @return self + */ + public function intersect(self $other) + { + $result = new self(); + foreach (get_object_vars($this) as $flag => $value) { + $otherValue = $other->{$flag}; + $result->{$flag} = ($value && $otherValue); + } + + return $result; + } + + /** + * Compute the union of flag-sets. + * + * A flag-set of the true flags in $this or $other. + * + * @param self $other The other flag set to union with. + * + * @return self + */ + public function union(self $other) + { + $result = new self(); + foreach (get_object_vars($this) as $flag => $value) { + $otherValue = $other->{$flag}; + $result->{$flag} = ($value || $otherValue); + } + + return $result; + } + + /** + * Compute the inverse of this flag set. + * + * The inverse of the flags in this flag-set. + * + * @return self + */ + public function inverse() + { + $result = new self(); + foreach (get_object_vars($this) as $flag => $value) { + $result->{$flag} = !$value; + } + + return $result; + } + private function __construct() { if (null === self::$__flags) { diff --git a/test/suite/FlagSetTraitTest.php b/test/suite/FlagSetTraitTest.php index 0bb728c..4438f19 100644 --- a/test/suite/FlagSetTraitTest.php +++ b/test/suite/FlagSetTraitTest.php @@ -140,4 +140,142 @@ public function testToString() strval(TestFlags::defaults()) ); } + + public function testDiff() + { + $defaults = TestFlags::defaults(); + + // diff to same + $flags = $defaults->diff( + TestFlags::defaults() + ); + $this->assertFalse($flags->foo); + $this->assertFalse($flags->bar); + $this->assertFalse($flags->baz); + $this->assertFalse($flags->qux); + + // diff to all + $flags = $defaults->diff( + TestFlags::all() + ); + $this->assertFalse($flags->foo); + $this->assertFalse($flags->bar); + $this->assertFalse($flags->baz); + $this->assertFalse($flags->qux); + + // diff to none + $flags = $defaults->diff( + TestFlags::none() + ); + $this->assertTrue($flags->foo); + $this->assertFalse($flags->bar); + $this->assertTrue($flags->baz); + $this->assertFalse($flags->qux); + } + + public function testSymmetricDiff() + { + $defaults = TestFlags::defaults(); + + // symmetric diff to same + $flags = $defaults->symmetricDiff( + TestFlags::defaults() + ); + $this->assertFalse($flags->foo); + $this->assertFalse($flags->bar); + $this->assertFalse($flags->baz); + $this->assertFalse($flags->qux); + + // symmetric diff to all + $flags = $defaults->symmetricDiff( + TestFlags::all() + ); + $this->assertFalse($flags->foo); + $this->assertTrue($flags->bar); + $this->assertFalse($flags->baz); + $this->assertTrue($flags->qux); + + // symmetric diff to none + $flags = $defaults->symmetricDiff( + TestFlags::none() + ); + $this->assertTrue($flags->foo); + $this->assertFalse($flags->bar); + $this->assertTrue($flags->baz); + $this->assertFalse($flags->qux); + } + + public function testIntersect() + { + $defaults = TestFlags::defaults(); + + // intersect to same + $flags = $defaults->intersect( + TestFlags::defaults() + ); + $this->assertTrue($flags->foo); + $this->assertFalse($flags->bar); + $this->assertTrue($flags->baz); + $this->assertFalse($flags->qux); + + // intersect to all + $flags = $defaults->intersect( + TestFlags::all() + ); + $this->assertTrue($flags->foo); + $this->assertFalse($flags->bar); + $this->assertTrue($flags->baz); + $this->assertFalse($flags->qux); + + // intersect to none + $flags = $defaults->intersect( + TestFlags::none() + ); + $this->assertFalse($flags->foo); + $this->assertFalse($flags->bar); + $this->assertFalse($flags->baz); + $this->assertFalse($flags->qux); + } + + public function testUnion() + { + $defaults = TestFlags::defaults(); + + // union to same + $flags = $defaults->union( + TestFlags::defaults() + ); + $this->assertTrue($flags->foo); + $this->assertFalse($flags->bar); + $this->assertTrue($flags->baz); + $this->assertFalse($flags->qux); + + // union to all + $flags = $defaults->union( + TestFlags::all() + ); + $this->assertTrue($flags->foo); + $this->assertTrue($flags->bar); + $this->assertTrue($flags->baz); + $this->assertTrue($flags->qux); + + // union to none + $flags = $defaults->union( + TestFlags::none() + ); + $this->assertTrue($flags->foo); + $this->assertFalse($flags->bar); + $this->assertTrue($flags->baz); + $this->assertFalse($flags->qux); + } + + public function testInverse() + { + $flags = TestFlags::defaults()->inverse(); + + $this->assertFalse($flags->foo); + $this->assertTrue($flags->bar); + $this->assertFalse($flags->baz); + $this->assertTrue($flags->qux); + } } From 0f3f8386e68b88cf3bf932ed42cb45864614da36 Mon Sep 17 00:00:00 2001 From: Kevin Millar Date: Thu, 26 Nov 2015 12:09:04 +1000 Subject: [PATCH 2/2] Changed to __flags instead of function call and updated return docs. --- src/FlagSetTrait.php | 44 +++++++++++---------------------- test/suite/FlagSetTraitTest.php | 8 +++--- 2 files changed, 19 insertions(+), 33 deletions(-) diff --git a/src/FlagSetTrait.php b/src/FlagSetTrait.php index 03d82d4..097bd34 100644 --- a/src/FlagSetTrait.php +++ b/src/FlagSetTrait.php @@ -177,18 +177,15 @@ public function __set($name, $value) /** * Compute the difference of flag-sets. * - * A flag-set of the flags in $this and not in $other. - * * @param self $other The other flag set to compare to. * - * @return self + * @return self A flag-set of the flags in $this and not in $other. */ public function diff(self $other) { $result = new self(); - foreach (get_object_vars($this) as $flag => $value) { - $otherValue = $other->{$flag}; - $result->{$flag} = ($value && !$otherValue); + foreach (self::$__flags as $flag) { + $result->{$flag} = $this->{$flag} && !$other->{$flag}; } return $result; @@ -197,18 +194,15 @@ public function diff(self $other) /** * Compute the symmetric difference of flag-sets. * - * A flag-set of the flags in $this and not in $other, and vice versa. - * * @param self $other The other flag set to compare to. * - * @return self + * @return self A flag-set of the flags in $this and not in $other, and vice versa. */ public function symmetricDiff(self $other) { $result = new self(); - foreach (get_object_vars($this) as $flag => $value) { - $otherValue = $other->{$flag}; - $result->{$flag} = ($value !== $otherValue); + foreach (self::$__flags as $flag) { + $result->{$flag} = $this->{$flag} !== $other->{$flag}; } return $result; @@ -217,18 +211,15 @@ public function symmetricDiff(self $other) /** * Compute the intersection of flag-sets. * - * A flag-set of the flags in $this and $other. - * * @param self $other The other flag set to intersect with. * - * @return self + * @return self A flag-set of the flags in $this and $other. */ public function intersect(self $other) { $result = new self(); - foreach (get_object_vars($this) as $flag => $value) { - $otherValue = $other->{$flag}; - $result->{$flag} = ($value && $otherValue); + foreach (self::$__flags as $flag) { + $result->{$flag} = $this->{$flag} && $other->{$flag}; } return $result; @@ -237,18 +228,15 @@ public function intersect(self $other) /** * Compute the union of flag-sets. * - * A flag-set of the true flags in $this or $other. - * * @param self $other The other flag set to union with. * - * @return self + * @return self A flag-set of the flags in $this or $other. */ public function union(self $other) { $result = new self(); - foreach (get_object_vars($this) as $flag => $value) { - $otherValue = $other->{$flag}; - $result->{$flag} = ($value || $otherValue); + foreach (self::$__flags as $flag) { + $result->{$flag} = $this->{$flag} || $other->{$flag}; } return $result; @@ -257,15 +245,13 @@ public function union(self $other) /** * Compute the inverse of this flag set. * - * The inverse of the flags in this flag-set. - * - * @return self + * @return self The inverse of the flags in this flag-set. */ public function inverse() { $result = new self(); - foreach (get_object_vars($this) as $flag => $value) { - $result->{$flag} = !$value; + foreach (self::$__flags as $flag) { + $result->{$flag} = !$this->{$flag}; } return $result; diff --git a/test/suite/FlagSetTraitTest.php b/test/suite/FlagSetTraitTest.php index 4438f19..c7d1e3a 100644 --- a/test/suite/FlagSetTraitTest.php +++ b/test/suite/FlagSetTraitTest.php @@ -147,7 +147,7 @@ public function testDiff() // diff to same $flags = $defaults->diff( - TestFlags::defaults() + $defaults ); $this->assertFalse($flags->foo); $this->assertFalse($flags->bar); @@ -179,7 +179,7 @@ public function testSymmetricDiff() // symmetric diff to same $flags = $defaults->symmetricDiff( - TestFlags::defaults() + $defaults ); $this->assertFalse($flags->foo); $this->assertFalse($flags->bar); @@ -211,7 +211,7 @@ public function testIntersect() // intersect to same $flags = $defaults->intersect( - TestFlags::defaults() + $defaults ); $this->assertTrue($flags->foo); $this->assertFalse($flags->bar); @@ -243,7 +243,7 @@ public function testUnion() // union to same $flags = $defaults->union( - TestFlags::defaults() + $defaults ); $this->assertTrue($flags->foo); $this->assertFalse($flags->bar);