Skip to content

Commit

Permalink
Move implementations of interfaces and update README.md (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
remenic authored and reinder83 committed Aug 1, 2019
1 parent 033cc31 commit f29be71
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 109 deletions.
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];
}
}

0 comments on commit f29be71

Please sign in to comment.