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

Move implementations of interfaces and update README.md #8

Merged
merged 1 commit into from
Aug 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,24 +205,24 @@ use Illuminate\Database\Eloquent\Model;

class Test extends Model
{
private $flagsObject;

/**
* Retrieve flags
* @return ExampleFlags
*/
public function getFlagsAttribute()
{
static $flags = null;
if ($flags === null) {
$model = $this;
$flags = new ExampleFlags(
if ($this->flagsObject === null) {
$this->flagsObject = new ExampleFlags(
$this->attributes['flags'], // set current flags mask
function (ExampleFlags $flags) use ($model) { // set callback function
function (ExampleFlags $flags) { // set callback function
// update the flags in this model
$model->flags = $flags->getMask();
$this->setAttribute('flags', $flags->getMask());
}
);
}
return $flags;
return $this->flagsObject;
}
}

Expand Down
102 changes: 102 additions & 0 deletions src/BinaryFlags.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,106 @@ public function __construct($mask = 0, callable $onModify = null)
$this->setOnModifyCallback($onModify);
}
}

/**
* Return the current element
*
* @return string the description of the flag or the name of the constant
* @since 1.2.0
*/
public function current()
{
return $this->getFlagNames($this->currentPos);
}

/**
* Move forward to next element
*
* @return void
* @since 1.2.0
*/
public function next()
{
$this->currentPos <<= 1; // shift to next bit
while (($this->mask & $this->currentPos) == 0 && $this->currentPos > 0) {
$this->currentPos <<= 1;
}
}

/**
* Return the key of the current element
*
* @return int the flag
* @since 1.2.0
*/
public function key()
{
return $this->currentPos;
}

/**
* Checks if current position is valid
*
* @return boolean Returns true on success or false on failure.
* @since 1.2.0
*/
public function valid()
{
return $this->currentPos > 0;
}

/**
* Rewind the Iterator to the first element
*
* @return void
* @since 1.2.0
*/
public function rewind()
{
// find the first element
if ($this->mask === 0) {
$this->currentPos = 0;

return;
}

$this->currentPos = 1;
while (($this->mask & $this->currentPos) == 0) {
$this->currentPos <<= 1;
}
}

/**
* Returns the number of flags that are set
*
* @return int
*
* The return value is cast to an integer.
* @since 1.2.0
*/
public function count()
{
$count = 0;
$mask = $this->mask;

while ($mask != 0) {
if (($mask & 1) == 1) {
$count++;
}
$mask >>= 1;
}

return $count;
}

/**
* Specify data which should be serialized to JSON
*
* @return mixed data which can be serialized by <b>json_encode</b>,
* @since 1.2.0
*/
public function jsonSerialize()
{
return ['mask' => $this->mask];
}
}
102 changes: 0 additions & 102 deletions src/Traits/BinaryFlags.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,106 +212,4 @@ public function checkAnyFlag($mask)
{
return $this->checkFlag($mask, false);
}

/**
* Return the current element
*
* @return string the description of the flag or the name of the constant
* @since 1.2.0
*/
public function current()
{
return $this->getFlagNames($this->currentPos);
}

/**
* Move forward to next element
*
* @return void
* @since 1.2.0
*/
public function next()
{
$this->currentPos <<= 1; // shift to next bit
while (($this->mask & $this->currentPos) == 0 && $this->currentPos > 0) {
$this->currentPos <<= 1;
}
}

/**
* Return the key of the current element
*
* @return int the flag
* @since 1.2.0
*/
public function key()
{
return $this->currentPos;
}

/**
* Checks if current position is valid
*
* @return boolean Returns true on success or false on failure.
* @since 1.2.0
*/
public function valid()
{
return $this->currentPos > 0;
}

/**
* Rewind the Iterator to the first element
*
* @return void
* @since 1.2.0
*/
public function rewind()
{
// find the first element
if ($this->mask === 0) {
$this->currentPos = 0;

return;
}

$this->currentPos = 1;
while (($this->mask & $this->currentPos) == 0) {
$this->currentPos <<= 1;
}
}

/**
* Returns the number of flags that are set
*
* @return int
*
* The return value is cast to an integer.
* @since 1.2.0
*/
public function count()
{
$count = 0;
$mask = $this->mask;

while ($mask != 0) {
if (($mask & 1) == 1) {
$count++;
}
$mask >>= 1;
}

return $count;
}

/**
* Specify data which should be serialized to JSON
*
* @return mixed data which can be serialized by <b>json_encode</b>,
* @since 1.2.0
*/
public function jsonSerialize()
{
return ['mask' => $this->mask];
}
}