Skip to content

Commit f29be71

Browse files
remenicreinder83
authored andcommitted
Move implementations of interfaces and update README.md (#8)
1 parent 033cc31 commit f29be71

File tree

3 files changed

+109
-109
lines changed

3 files changed

+109
-109
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -205,24 +205,24 @@ use Illuminate\Database\Eloquent\Model;
205205

206206
class Test extends Model
207207
{
208+
private $flagsObject;
209+
208210
/**
209211
* Retrieve flags
210212
* @return ExampleFlags
211213
*/
212214
public function getFlagsAttribute()
213215
{
214-
static $flags = null;
215-
if ($flags === null) {
216-
$model = $this;
217-
$flags = new ExampleFlags(
216+
if ($this->flagsObject === null) {
217+
$this->flagsObject = new ExampleFlags(
218218
$this->attributes['flags'], // set current flags mask
219-
function (ExampleFlags $flags) use ($model) { // set callback function
219+
function (ExampleFlags $flags) { // set callback function
220220
// update the flags in this model
221-
$model->flags = $flags->getMask();
221+
$this->setAttribute('flags', $flags->getMask());
222222
}
223223
);
224224
}
225-
return $flags;
225+
return $this->flagsObject;
226226
}
227227
}
228228

src/BinaryFlags.php

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/Traits/BinaryFlags.php

Lines changed: 0 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -212,106 +212,4 @@ public function checkAnyFlag($mask)
212212
{
213213
return $this->checkFlag($mask, false);
214214
}
215-
216-
/**
217-
* Return the current element
218-
*
219-
* @return string the description of the flag or the name of the constant
220-
* @since 1.2.0
221-
*/
222-
public function current()
223-
{
224-
return $this->getFlagNames($this->currentPos);
225-
}
226-
227-
/**
228-
* Move forward to next element
229-
*
230-
* @return void
231-
* @since 1.2.0
232-
*/
233-
public function next()
234-
{
235-
$this->currentPos <<= 1; // shift to next bit
236-
while (($this->mask & $this->currentPos) == 0 && $this->currentPos > 0) {
237-
$this->currentPos <<= 1;
238-
}
239-
}
240-
241-
/**
242-
* Return the key of the current element
243-
*
244-
* @return int the flag
245-
* @since 1.2.0
246-
*/
247-
public function key()
248-
{
249-
return $this->currentPos;
250-
}
251-
252-
/**
253-
* Checks if current position is valid
254-
*
255-
* @return boolean Returns true on success or false on failure.
256-
* @since 1.2.0
257-
*/
258-
public function valid()
259-
{
260-
return $this->currentPos > 0;
261-
}
262-
263-
/**
264-
* Rewind the Iterator to the first element
265-
*
266-
* @return void
267-
* @since 1.2.0
268-
*/
269-
public function rewind()
270-
{
271-
// find the first element
272-
if ($this->mask === 0) {
273-
$this->currentPos = 0;
274-
275-
return;
276-
}
277-
278-
$this->currentPos = 1;
279-
while (($this->mask & $this->currentPos) == 0) {
280-
$this->currentPos <<= 1;
281-
}
282-
}
283-
284-
/**
285-
* Returns the number of flags that are set
286-
*
287-
* @return int
288-
*
289-
* The return value is cast to an integer.
290-
* @since 1.2.0
291-
*/
292-
public function count()
293-
{
294-
$count = 0;
295-
$mask = $this->mask;
296-
297-
while ($mask != 0) {
298-
if (($mask & 1) == 1) {
299-
$count++;
300-
}
301-
$mask >>= 1;
302-
}
303-
304-
return $count;
305-
}
306-
307-
/**
308-
* Specify data which should be serialized to JSON
309-
*
310-
* @return mixed data which can be serialized by <b>json_encode</b>,
311-
* @since 1.2.0
312-
*/
313-
public function jsonSerialize()
314-
{
315-
return ['mask' => $this->mask];
316-
}
317215
}

0 commit comments

Comments
 (0)