BitMaskTrait::setFlagsIndex()   A
last analyzed

Complexity

Conditions 3
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 14
c 0
b 0
f 0
rs 9.9666
cc 3
nc 1
nop 1
1
<?php
2
namespace SoliDry\Extension;
3
4
use Illuminate\Database\Eloquent\Collection;
5
6
/**
7
 * Class BitMaskTrait
8
 * @package SoliDry\Extension
9
 * @property BitMask bitMask
10
 */
11
trait BitMaskTrait
12
{
13
    /**
14
     * @param Collection $data
15
     * @return Collection
16
     * @throws \SoliDry\Exceptions\AttributesException
17
     */
18
    protected function setFlagsIndex(Collection $data)
19
    {
20
        $data->map(function($v) {
21
            $field = $this->bitMask->getField();
22
            if(isset($v[$field])) {
23
                $flags = $this->bitMask->getFlags();
24
                $mask  = $v[$field];
25
                foreach($flags as $flag => $fVal) {
26
                    $v[$flag] = (bool)($fVal & $mask);
27
                }
28
            }
29
            return $v;
30
        });
31
        return $data;
32
    }
33
34
    /**
35
     * @param BaseModel $data
36
     * @return BaseModel
37
     * @throws \SoliDry\Exceptions\AttributesException
38
     */
39
    protected function setFlagsView(BaseModel $data)
40
    {
41
        $field = $this->bitMask->getField();
42
        if(isset($data[$field])) {
43
            $flags = $this->bitMask->getFlags();
44
            $mask  = $data[$field];
45
            foreach($flags as $flag => $fVal) {
46
                $data[$flag] = ($fVal & $mask) ? true : false;
47
            }
48
        }
49
        return $data;
50
    }
51
52
    /**
53
     * Creates bit mask based on bit flags and unset those flags to save via model
54
     * @param array $jsonProps
55
     * @throws \SoliDry\Exceptions\AttributesException
56
     */
57
    protected function setMaskCreate(array $jsonProps)
58
    {
59
        $field = $this->bitMask->getField();
60
        $flags = $this->bitMask->getFlags();
61
        foreach($flags as $flag => $fVal) {
62
            if (isset($jsonProps[$flag])) {
63
                if ((bool) $jsonProps[$flag] === true) {
64
                    $this->model->$field |= $fVal;
65
                } else if ((bool) $jsonProps[$flag] === false) {
66
                    $this->model->$field &= ~$fVal;
67
                }
68
            }
69
            unset($this->model->$flag);
70
        }
71
    }
72
73
    /**
74
     * Sets flags on model to pass them through json api processing
75
     * @throws \SoliDry\Exceptions\AttributesException
76
     */
77
    protected function setFlagsCreate()
78
    {
79
        $field = $this->bitMask->getField();
80
        if(isset($this->model->$field)) {
81
            $flags = $this->bitMask->getFlags();
82
            $mask  = $this->model->$field;
83
            foreach($flags as $flag => $fVal) {
84
                $this->model->$flag = (bool)($fVal & $mask);
85
            }
86
        }
87
        return $this->model;
88
    }
89
90
    /**
91
     * Updates bit mask based on bit flags and unset those flags to save via model
92
     * @param $model
93
     * @param array $jsonProps
94
     * @return mixed
95
     * @throws \SoliDry\Exceptions\AttributesException
96
     */
97
    protected function setMaskUpdate(&$model, array $jsonProps)
98
    {
99
        $field = $this->bitMask->getField();
100
        $flags = $this->bitMask->getFlags();
101
        foreach($flags as $flag => $fVal) {
102
            if (isset($jsonProps[$flag])) {
103
                if ((bool) $jsonProps[$flag] === true) {
104
                    $model->$field |= $fVal;
105
                } else if ((bool) $jsonProps[$flag] === false) {
106
                    $model->$field &= ~$fVal;
107
                }
108
            }
109
        }
110
        return $model;
111
    }
112
113
    /**
114
     * Sets flags on model to pass them through json api processing
115
     * @param $model
116
     * @throws \SoliDry\Exceptions\AttributesException
117
     */
118
    protected function setFlagsUpdate(&$model) : void
119
    {
120
        $field = $this->bitMask->getField();
121
        if(isset($model[$field])) {
122
            $flags = $this->bitMask->getFlags();
123
            $mask  = $model[$field];
124
            foreach($flags as $flag => $fVal) {
125
                $model[$flag] = (bool)($fVal & $mask);
126
            }
127
        }
128
    }
129
}