Blame view

artweb/artbox-core/behaviors/BitMaskBehavior.php 2.59 KB
326af919   mzavalniuk   add artbox-core t...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
  <?php
      
      namespace artbox\core\behaviors;
      
      use yii\base\Behavior;
      use yii\web\Request;
      
      /**
       * Class BitMaskBehavior
       *
       * @package artbox\core\behaviors
       */
      class BitMaskBehavior extends Behavior
      {
          /**
           * Field for the mask
           *
           * @var array
           */
          public $fields;
      
          /**
           * Param for storing masks
           *
           * @var
           */
          public $masks;
      
          /**
           * Db field for storing mask integer value
           *
           * @var string
           */
          public $maskField = 'mask';
      
          /**
           * Generate the integer mask from request
           *
           * @param \yii\web\Request $request
           */
          public function loadMask(Request $request)
          {
              $owner = $this->owner;
              $maskField = $this->maskField;
              if (isset($request->post($this->getModelName())[ 'masks' ])) {
                  $masks = $request->post($this->getModelName())[ 'masks' ];
              } else {
                  if ($request->isPost) {
                      $owner->$maskField = 0;
                      return;
                  } else {
                      return;
                  }
              }
          
              $owner->$maskField = 0;
              foreach ($masks as $mask => $value) {
                  if ($value == '1') {
                      $owner->$maskField += 2 ** (int) $this->fields[ $mask ];
                  }
              }
          }
      
          /**
           * Get the value of the owners mask field
           *
           * @return mixed
           */
          public function getMaskInt()
          {
              $maskField = $this->maskField;
              return $this->owner->$maskField;
          }
      
          /**
           * Return the name of the owner's model
           * without namespaces
           *
           * @return string
           */
          public function getModelName()
          {
              $owner = $this->owner;
              $arr = explode('\\', get_class($owner));
              return array_pop($arr);
          }
      
          /**
           * Checks the value
           *
           * @param string $field
           *
           * @return bool
           */
          public function is(string $field)
          {
              if (array_key_exists($field, $this->fields)) {
                  $result = $this->getMaskInt() & 2 ** $this->fields[ $field ];
                  if ($result === 0) {
                      return false;
                  } else {
                      return true;
                  }
              } else {
                  return false;
              }
          }
      }