@@ -29,4 +29,106 @@ public function __construct($mask = 0, callable $onModify = null)
2929 $ this ->setOnModifyCallback ($ onModify );
3030 }
3131 }
32+
33+ /**
34+ * Return the current element
35+ *
36+ * @return string the description of the flag or the name of the constant
37+ * @since 1.2.0
38+ */
39+ public function current ()
40+ {
41+ return $ this ->getFlagNames ($ this ->currentPos );
42+ }
43+
44+ /**
45+ * Move forward to next element
46+ *
47+ * @return void
48+ * @since 1.2.0
49+ */
50+ public function next ()
51+ {
52+ $ this ->currentPos <<= 1 ; // shift to next bit
53+ while (($ this ->mask & $ this ->currentPos ) == 0 && $ this ->currentPos > 0 ) {
54+ $ this ->currentPos <<= 1 ;
55+ }
56+ }
57+
58+ /**
59+ * Return the key of the current element
60+ *
61+ * @return int the flag
62+ * @since 1.2.0
63+ */
64+ public function key ()
65+ {
66+ return $ this ->currentPos ;
67+ }
68+
69+ /**
70+ * Checks if current position is valid
71+ *
72+ * @return boolean Returns true on success or false on failure.
73+ * @since 1.2.0
74+ */
75+ public function valid ()
76+ {
77+ return $ this ->currentPos > 0 ;
78+ }
79+
80+ /**
81+ * Rewind the Iterator to the first element
82+ *
83+ * @return void
84+ * @since 1.2.0
85+ */
86+ public function rewind ()
87+ {
88+ // find the first element
89+ if ($ this ->mask === 0 ) {
90+ $ this ->currentPos = 0 ;
91+
92+ return ;
93+ }
94+
95+ $ this ->currentPos = 1 ;
96+ while (($ this ->mask & $ this ->currentPos ) == 0 ) {
97+ $ this ->currentPos <<= 1 ;
98+ }
99+ }
100+
101+ /**
102+ * Returns the number of flags that are set
103+ *
104+ * @return int
105+ *
106+ * The return value is cast to an integer.
107+ * @since 1.2.0
108+ */
109+ public function count ()
110+ {
111+ $ count = 0 ;
112+ $ mask = $ this ->mask ;
113+
114+ while ($ mask != 0 ) {
115+ if (($ mask & 1 ) == 1 ) {
116+ $ count ++;
117+ }
118+ $ mask >>= 1 ;
119+ }
120+
121+ return $ count ;
122+ }
123+
124+ /**
125+ * Specify data which should be serialized to JSON
126+ *
127+ * @return mixed data which can be serialized by <b>json_encode</b>,
128+ * @since 1.2.0
129+ */
130+ public function jsonSerialize ()
131+ {
132+ return ['mask ' => $ this ->mask ];
133+ }
32134}
0 commit comments