Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added set operations (fixes #1). #2

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions src/FlagSetTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ public function __get($name)

/**
* Get a string representation of the flag set.
*
* @return string The string representation.
*/
public function __toString()
{
Expand Down Expand Up @@ -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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These descriptions might be better the @return annotation

*
* @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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use self::__$flags instead of calling get_object_vars() ... you know, silly PHP5 and its function call overhead.

$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) {
Expand Down
138 changes: 138 additions & 0 deletions test/suite/FlagSetTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}