169 if ($this->convertBeforeSave) {
180 $states = $this->getOwner()->{$this->attribute};
181 if (!is_array($states)) {
184 $this->getOwner()->{$this->attribute} = $this->convertStates($states);
194 private function convertStates($states)
196 if (is_int($states)) {
199 if (!is_array($states)) {
200 throw new CException(
'Wrong states format given. Must be an array.');
203 return self::createBitMask($states, $this->data);
224 public static function createBitMask($states,
$data)
226 $keys = array_keys(
$data);
228 foreach ($states as $state) {
229 if (isset(
$data[$state])) {
230 $pos = array_search($state, $keys);
231 $new_states = 1 << $pos | $new_states;
243 public function afterSave($event)
245 if ($this->convertAfterSave) {
246 $this->toOutputArray();
256 private function toOutputArray()
258 $states = $this->getOwner()->{$this->attribute};
259 if (!is_int($states)) {
262 $keys = array_keys($this->data);
263 $new_states = array();
264 foreach ($keys as $pos => $k) {
265 if ($states >> $pos & 1) {
269 $this->getOwner()->{$this->attribute} = $new_states;
277 public function afterFind($event)
280 $this->getOwner()->{$this->attribute} = (int)$this->getOwner()->{$this->attribute};
282 if ($this->convertAfterFind) {
283 $this->toOutputArray();
299 public function checkState($states)
301 $data = $this->getOwner()->{$this->attribute};
305 $keys = array_keys($this->data);
306 if (is_string($states)) {
307 $states = array($states);
311 foreach ($states as $state) {
312 $pos = array_search($state, $keys);
313 if ($pos ===
false || (
$data >> $pos & 1) === 0) {
319 } elseif (is_array(
$data)) {
322 foreach ($states as $state) {
323 if (array_search($state,
$data) ===
false) {
344 public function convertAttribute($type =
'auto')
346 if ($type ===
'int') {
347 $this->toDatabaseInt();
348 } elseif ($type ===
'arr') {
349 $this->toOutputArray();
350 } elseif ($type ===
'auto') {
351 $attribute = $this->getOwner()->{$this->attribute};
352 if (is_array($attribute)) {
353 $this->toDatabaseInt();
354 } elseif (is_int($attribute)) {
355 $this->toOutputArray();
365 public function getStatesData()
377 public function statesIncluded($states)
379 $owner = $this->getOwner();
380 $db = $owner->getDbConnection();
381 $criteria = $owner->getDbCriteria();
382 $bitmask = $this->convertStates($states);
383 $column = $db->quoteColumnName($owner->getTableAlias()) .
'.' . $db->quoteColumnName($this->attribute);
384 $criteria->mergeWith(array(
385 'condition' => $column .
" & {$bitmask} = {$bitmask}",
398 public function statesExact($states)
400 $owner = $this->getOwner();
401 $db = $owner->getDbConnection();
402 $criteria = $owner->getDbCriteria();
403 $bitmask = $this->convertStates($states);
404 $column = $db->quoteColumnName($owner->getTableAlias()) .
'.' . $db->quoteColumnName($this->attribute);
405 $criteria->mergeWith(array(
406 'condition' => $column .
" = " . $bitmask,
419 public function statesAtLeast($states)
421 $owner = $this->getOwner();
422 $db = $owner->getDbConnection();
423 $criteria = $owner->getDbCriteria();
424 $bitmask = $this->convertStates($states);
425 $column = $db->quoteColumnName($owner->getTableAlias()) .
'.' . $db->quoteColumnName($this->attribute);
426 $criteria->mergeWith(array(
427 'condition' => $column .
" & {$bitmask} != 0",
437 public function validateStates($states)