vendor/reinder83/binary-flags/src/BinaryFlags.php line 55

Open in your IDE?
  1. <?php
  2. namespace Reinder83\BinaryFlags;
  3. use Countable;
  4. use Iterator;
  5. use JsonSerializable;
  6. /**
  7.  * This class holds useful methods for checking, adding or removing binary flags
  8.  *
  9.  * @author Reinder
  10.  */
  11. abstract class BinaryFlags implements IteratorCountableJsonSerializable
  12. {
  13.     use Traits\BinaryFlags;
  14.     /**
  15.      * @var int
  16.      */
  17.     private $currentPos 0;
  18.     /**
  19.      * Initiate class
  20.      * @param int [$mask = 0]
  21.      * @param callable [$onModify = null]
  22.      */
  23.     public function __construct($mask 0, callable $onModify null)
  24.     {
  25.         $this->setMask($mask);
  26.         // set onModify callback if specified
  27.         if ($onModify !== null) {
  28.             $this->setOnModifyCallback($onModify);
  29.         }
  30.     }
  31.     /**
  32.      * Return the current element
  33.      *
  34.      * @return string the description of the flag or the name of the constant
  35.      * @since 1.2.0
  36.      */
  37.     public function current()
  38.     {
  39.         return $this->getFlagNames($this->currentPos);
  40.     }
  41.     /**
  42.      * Move forward to next element
  43.      *
  44.      * @return void
  45.      * @since 1.2.0
  46.      */
  47.     public function next()
  48.     {
  49.         $this->currentPos <<= 1// shift to next bit
  50.         while (($this->mask $this->currentPos) == && $this->currentPos 0) {
  51.             $this->currentPos <<= 1;
  52.         }
  53.     }
  54.     /**
  55.      * Return the key of the current element
  56.      *
  57.      * @return int the flag
  58.      * @since 1.2.0
  59.      */
  60.     public function key()
  61.     {
  62.         return $this->currentPos;
  63.     }
  64.     /**
  65.      * Checks if current position is valid
  66.      *
  67.      * @return boolean Returns true on success or false on failure.
  68.      * @since 1.2.0
  69.      */
  70.     public function valid()
  71.     {
  72.         return $this->currentPos 0;
  73.     }
  74.     /**
  75.      * Rewind the Iterator to the first element
  76.      *
  77.      * @return void
  78.      * @since 1.2.0
  79.      */
  80.     public function rewind()
  81.     {
  82.         // find the first element
  83.         if ($this->mask === 0) {
  84.             $this->currentPos 0;
  85.             return;
  86.         }
  87.         $this->currentPos 1;
  88.         while (($this->mask $this->currentPos) == 0) {
  89.             $this->currentPos <<= 1;
  90.         }
  91.     }
  92.     /**
  93.      * Returns the number of flags that are set
  94.      *
  95.      * @return int
  96.      *
  97.      * The return value is cast to an integer.
  98.      * @since 1.2.0
  99.      */
  100.     public function count()
  101.     {
  102.         $count 0;
  103.         $mask  $this->mask;
  104.         while ($mask != 0) {
  105.             if (($mask 1) == 1) {
  106.                 $count++;
  107.             }
  108.             $mask >>= 1;
  109.         }
  110.         return $count;
  111.     }
  112.     /**
  113.      * Specify data which should be serialized to JSON
  114.      *
  115.      * @return mixed data which can be serialized by <b>json_encode</b>,
  116.      * @since 1.2.0
  117.      */
  118.     public function jsonSerialize()
  119.     {
  120.         return ['mask' => $this->mask];
  121.     }
  122. }