BitmaskedBehavior.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. App::uses('ModelBehavior', 'Model');
  3. /**
  4. * BitmaskedBehavior
  5. *
  6. * An implementation of bitwise masks for row-level operations.
  7. * You can submit/register flags in different ways. The easiest way is using a static model function.
  8. * It should contain the bits like so (starting with 1):
  9. * 1 => w, 2 => x, 4 => y, 8 => z, ... (bits as keys - names as values)
  10. * The order doest't matter, as long as no bit is used twice.
  11. *
  12. * The theoretical limit for a 64-bit integer would be 64 bits (2^64).
  13. * But if you actually seem to need more than a hand full you
  14. * obviously do something wrong and should better use a joined table etc.
  15. *
  16. * @version 1.1
  17. * @author Mark Scherer
  18. * @cake 2.x
  19. * @license MIT
  20. * @uses ModelBehavior
  21. * 2012-02-24 ms
  22. */
  23. class BitmaskedBehavior extends ModelBehavior {
  24. /**
  25. * settings defaults
  26. */
  27. protected $_defaults = array(
  28. 'field' => 'status',
  29. 'mappedField' => null, # NULL = same as above
  30. //'mask' => null,
  31. 'bits' => null,
  32. 'before' => 'validate', // on: save or validate
  33. );
  34. /**
  35. * Behavior configuration
  36. *
  37. * @param Model $Model
  38. * @param array $config
  39. * @return void
  40. */
  41. public function setup(Model $Model, $config = array()) {
  42. $config = array_merge($this->_defaults, $config);
  43. if (empty($config['bits'])) {
  44. $config['bits'] = Inflector::pluralize($config['field']);
  45. }
  46. if (is_callable($config['bits'])) {
  47. $config['bits'] = call_user_func($config['bits']);
  48. } elseif (is_string($config['bits']) && method_exists($Model, $config['bits'])) {
  49. $config['bits'] = $Model->{$config['bits']}();
  50. } elseif (!is_array($config['bits'])) {
  51. $config['bits'] = false;
  52. }
  53. if (empty($config['bits'])) {
  54. throw new InternalErrorException('Bits not found');
  55. }
  56. ksort($config['bits'], SORT_NUMERIC);
  57. $this->settings[$Model->alias] = $config;
  58. }
  59. public function beforeFind(Model $Model, $query) {
  60. $field = $this->settings[$Model->alias]['field'];
  61. if (isset($query['conditions']) && is_array($query['conditions'])) {
  62. $query['conditions'] = $this->encodeBitmaskConditions($Model, $query['conditions']);
  63. }
  64. return $query;
  65. }
  66. public function afterFind(Model $Model, $results, $primary) {
  67. $field = $this->settings[$Model->alias]['field'];
  68. if (!($mappedField = $this->settings[$Model->alias]['mappedField'])) {
  69. $mappedField = $field;
  70. }
  71. foreach ($results as $key => $result) {
  72. if (isset($result[$Model->alias][$field])) {
  73. $results[$key][$Model->alias][$mappedField] = $this->decodeBitmask($Model, $result[$Model->alias][$field]);
  74. }
  75. }
  76. return $results;
  77. }
  78. public function beforeValidate(Model $Model) {
  79. if ($this->settings[$Model->alias]['before'] != 'validate') {
  80. return true;
  81. }
  82. $this->encodeBitmaskData($Model);
  83. return true;
  84. }
  85. public function beforeSave(Model $Model) {
  86. if ($this->settings[$Model->alias]['before'] != 'save') {
  87. return true;
  88. }
  89. $this->encodeBitmaskData($Model);
  90. return true;
  91. }
  92. /**
  93. * @param int $bitmask
  94. * @return array $bitmaskArray
  95. * from DB to APP
  96. */
  97. public function decodeBitmask(Model $Model, $value) {
  98. $res = array();
  99. $i = 0;
  100. $value = (int) $value;
  101. foreach ($this->settings[$Model->alias]['bits'] as $key => $val) {
  102. $val = (($value & pow(2, $i)) != 0) ? true : false;
  103. if ($val) {
  104. $res[] = $key;
  105. }
  106. $i++;
  107. }
  108. return $res;
  109. }
  110. /**
  111. * @param array $bitmaskArray
  112. * @return int $bitmask
  113. * from APP to DB
  114. */
  115. public function encodeBitmask(Model $Model, $value) {
  116. $res = 0;
  117. if (empty($value)) {
  118. return null;
  119. }
  120. foreach ((array)$value as $key => $val) {
  121. $res |= (int) $val;
  122. }
  123. if ($res === 0) {
  124. return null; # make sure notEmpty validation rule triggers
  125. }
  126. return $res;
  127. }
  128. public function encodeBitmaskConditions(Model $Model, $conditions) {
  129. $field = $this->settings[$Model->alias]['field'];
  130. if (!($mappedField = $this->settings[$Model->alias]['mappedField'])) {
  131. $mappedField = $field;
  132. }
  133. foreach ($conditions as $key => $val) {
  134. if ($key === $mappedField) {
  135. $conditions[$field] = $this->encodeBitmask($Model, $val);
  136. if ($field != $mappedField) {
  137. unset($conditions[$mappedField]);
  138. }
  139. continue;
  140. } elseif ($key === $Model->alias . '.' . $mappedField) {
  141. $conditions[$Model->alias . '.' .$field] = $this->encodeBitmask($Model, $val);
  142. if ($field != $mappedField) {
  143. unset($conditions[$Model->alias . '.' .$mappedField]);
  144. }
  145. continue;
  146. }
  147. if (!is_array($val)) {
  148. continue;
  149. }
  150. $conditions[$key] = $this->encodeBitmaskConditions($Model, $val);
  151. }
  152. return $conditions;
  153. }
  154. public function encodeBitmaskData(Model $Model) {
  155. $field = $this->settings[$Model->alias]['field'];
  156. if (!($mappedField = $this->settings[$Model->alias]['mappedField'])) {
  157. $mappedField = $field;
  158. }
  159. if (isset($Model->data[$Model->alias][$mappedField])) {
  160. $Model->data[$Model->alias][$field] = $this->encodeBitmask($Model, $Model->data[$Model->alias][$mappedField]);
  161. }
  162. if ($field != $mappedField) {
  163. unset($Model->data[$Model->alias][$mappedField]);
  164. }
  165. }
  166. /**
  167. * @param mixed bits (int, array)
  168. * @return array $sqlSnippet
  169. */
  170. public function isBit(Model $Model, $bits) {
  171. $bits = (array)$bits;
  172. $bitmask = $this->encodeBitmask($Model, $bits);
  173. $field = $this->settings[$Model->alias]['field'];
  174. return array($Model->alias.'.'.$field => $bitmask);
  175. }
  176. /**
  177. * @param mixed bits (int, array)
  178. * @return array $sqlSnippet
  179. */
  180. public function isNotBit(Model $Model, $bits) {
  181. $bits = (array)$bits;
  182. $bitmask = $this->encodeBitmask($Model, $bits);
  183. $field = $this->settings[$Model->alias]['field'];
  184. return array('NOT' => array($Model->alias.'.'.$field => $bitmask));
  185. }
  186. /**
  187. * @param mixed bits (int, array)
  188. * @return array $sqlSnippet
  189. */
  190. public function containsBit(Model $Model, $bits) {
  191. $bits = (array)$bits;
  192. $bitmask = $this->encodeBitmask($Model, $bits);
  193. $field = $this->settings[$Model->alias]['field'];
  194. return array('('.$Model->alias.'.'.$field.' & ? = ?)' => array($bitmask, $bitmask));
  195. }
  196. /**
  197. * @param mixed bits (int, array)
  198. * @return array $sqlSnippet
  199. */
  200. public function containsNotBit(Model $Model, $bits) {
  201. $bits = (array)$bits;
  202. $bitmask = $this->encodeBitmask($Model, $bits);
  203. $field = $this->settings[$Model->alias]['field'];
  204. return array('('.$Model->alias.'.'.$field.' & ? != ?)' => array($bitmask, $bitmask));
  205. }
  206. }