BanModel::explodeBans PHP Method

explodeBans() public static method

Explode a banned bit mask into an array of ban constants.
public static explodeBans ( integer $banned ) : array
$banned integer The banned bit mask to explode.
return array Returns an array of the set bits.
    public static function explodeBans($banned)
    {
        $result = array();
        for ($i = 1; $i <= 8; $i++) {
            $bit = pow(2, $i - 1);
            if (($banned & $bit) === $bit) {
                $result[] = $bit;
            }
        }
        return $result;
    }

Usage Example

 /**
  *
  *
  * @throws Exception
  */
 protected function getData()
 {
     $userID = $this->UserID ?: Gdn::session()->UserID;
     $user = Gdn::userModel()->getID($userID);
     $banned = val('Banned', $user);
     $bits = BanModel::explodeBans($banned);
     $reasons = array();
     foreach ($bits as $bit) {
         if (($bit & $this->ExcludeBans) === 0) {
             $reasons[$bit] = t("BanReason.{$bit}");
         }
     }
     $this->setData('Reasons', $reasons);
     if (!$this->Summary) {
         if ($this->ExcludeBans) {
             $summary = "Also banned for the following:";
         } else {
             $summary = "Banned for the following:";
         }
     }
     $this->setData('Summary', $this->Summary ?: $summary);
     $this->EventArguments['User'] = $user;
     $this->fireEvent('GetData');
 }