diff --git a/README.md b/README.md
index bbb3a8a..db08b06 100644
--- a/README.md
+++ b/README.md
@@ -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;
}
}
diff --git a/src/BinaryFlags.php b/src/BinaryFlags.php
index 9d627f9..d92271d 100644
--- a/src/BinaryFlags.php
+++ b/src/BinaryFlags.php
@@ -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 json_encode,
+ * @since 1.2.0
+ */
+ public function jsonSerialize()
+ {
+ return ['mask' => $this->mask];
+ }
}
diff --git a/src/Traits/BinaryFlags.php b/src/Traits/BinaryFlags.php
index 74aa740..af59bc1 100644
--- a/src/Traits/BinaryFlags.php
+++ b/src/Traits/BinaryFlags.php
@@ -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 json_encode,
- * @since 1.2.0
- */
- public function jsonSerialize()
- {
- return ['mask' => $this->mask];
- }
}