Login   Register  
PHP Classes
elePHPant
Icontem

File: bitwise/string.bitwise.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Mahmut Namli  >  Bitwise on String Operations  >  bitwise/string.bitwise.php  >  Download  
File: bitwise/string.bitwise.php
Role: Class source
Content type: text/plain
Description: string.bitwise class file
Class: Bitwise on String Operations
Define user permissions with bitwise operations
Author: By
Last change: 1) E-mail corrected
2) Ruler limited to 120 words
3) Removed unnecessary single and double quotes
4) Made documentations and comment lines clear
Date: 2014-01-04 06:40
Size: 11,427 bytes
 

Contents

Class file image Download
<?php
/**
 * +------------------------------------------------------------------------+
 * | string.bitwise.php                                                     |
 * +------------------------------------------------------------------------+
 * | Copyright (c) Mahmut Namli 2006-2013. All rights reserved.             |
 * | Version       0.10                                                     |
 * | Last modified 29/12/2013                                               |
 * | Email         mahmudnamli@gmail.com                                    |
 * | Web           http://whiteeffect.com                                   |
 * +------------------------------------------------------------------------+
 * | This program is free software; you can redistribute it and/or modify   |
 * | it under the terms of the GNU General Public License version 2 as      |
 * | published by the Free Software Foundation.                             |
 * |                                                                        |
 * | This program is distributed in the hope that it will be useful,        |
 * | but WITHOUT ANY WARRANTY; without even the implied warranty of         |
 * | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          |
 * | GNU General Public License for more details.                           |
 * |                                                                        |
 * | You should have received a copy of the GNU General Public License      |
 * | along with this program; if not, write to the                          |
 * |   Free Software Foundation, Inc., 59 Temple Place, Suite 330,          |
 * |   Boston, MA 02111-1307 USA                                            |
 * |                                                                        |
 * | Please give credit on sites that use string.bitwise and submit changes |
 * | of the script so other people can use them as well.                    |
 * | This script is free to use, don't abuse.                               |
 * +------------------------------------------------------------------------+
 * 
 * This class' idea has emerged from the &#8203;&#8203;using of bitwise on the strings
 * so, you don't have any restrictions anymore about using bitwise such as using on database or elsewhere.
 * 
 * You can use bitwises on menugroups in DB's or relative structures on other similar uses.
 * Also on determining <i><b>user privileges</b></i> on your system, this's very very useful and have control statement
 * so you can use this very basically.
 * 
 * For example; you have some usergroups like this:
 * 
 *  /**************************************
 *  /******* BITWISE USER GROUPS **********
 *  /**************************************
 *  const User = 1;			//1
 *  const NormalAdmin = 3;		//11
 *  const SysAdmin = 7;			//111
 *
 *  const Reading = 8;			//1000
 *  const Writing = 16;			//10000
 *  const ToAuthorize = 32;		//100000
 *
 *  const Management = 64;		//1000000
 *  const ContentManagement = 128;	//10000000
 *  const SubOperator = 256;		//100000000
 *  const Operator = 768;		//1100000000
 *  const Accounting = 1024;		//10000000000
 * 
 * Note: I set up Operator as 768, because Operator needs to have the SubOperator rights..
 * Don't make your own math addition process, use always methods..
 * 
 * <b>For a quick example:</b>
 * 
 * I have NormalAdmin, Reading and Writing permissions, so:
 *	$myPerms = BitOperator::mOrInt( array(
 *		BitOperator::NormalAdmin,
 *		BitOperator::Reading,
 *		BitOperator::Writing
 *	) );	<b>//gives 27</b>
 *
 * this gives 27, please don't make your own math addition process..
 * probably you can say;
 * 27 => NormalAdmin, Reading and Writing
 * 		AND
 * 27 => User, NormalAdmin, SysAdmin, and Writing
 * 
 * but NOT!!
 * 
 * BTW, the addtion of User, NormalAdmin, SysAdmin, and Writing will gives you 23, not 27 :) because if you are a
 * SysAdmin, so you have the NormalAdmin and User privileges already and don't need to be add.
 * 
 * If I have User, NormalAdmin, SysAdmin and Writing permissions, so:
 *	$myPerms = BitOperator::mOrInt( array(
 *		BitOperator::User,
 *		BitOperator::NormalAdmin,
 *		BitOperator::SysAdmin,
 *		BitOperator::Writing
 *	) );	<b>//gives 23</b>
 * 
 * if you have SysAdmin, so you have NormalAdmin and User privileges, as we want it to be. If you have SysAdmin, in
 * the addition process automatically eliminate the NormalAdmin and User privileges, if you use methods, you don't
 * need to be worry about it.
 * 
 * @version 	0.10
 * @author		Mahmut Namli <mahmudnamli@gmail.com>
 * @license		http://opensource.org/licenses/gpl-license.php GNU Public License
 * @copyright	Mahmut Namli
 * @package		any MVC or FW structure
 * @subpackage	external
 *
 */
class BitOperator {
	
	/**************************************/
	/******* BITWISE USER GROUPS **********/
	/**************************************/
	const User = 1;			//1
	const NormalAdmin = 3;		//11
	const SysAdmin = 7;		//111
	
	const Reading = 8;		//1000
	const Writing = 16;		//10000
	const ToAuthorize = 32;		//100000
	
	const Management = 64;		//1000000
	const ContentManagement = 128;	//10000000
	const SubOperator = 256;	//100000000
	const Operator = 768;		//1100000000
	const Accounting = 1024;	//10000000000
    
    public function __construct() { }

    /**
     * Provides access to all const values in the class
     *
     * @return multitype: class constants
     */
    public static function showConstant() {
    	$refl = new ReflectionClass('BitOperator');
    	return $refl->getConstants();
    }
    
	/**
	 * Applies bitwise 'OR' logic for the integer values
	 * 
	 * @param array $bitler
	 * @return number
	 */
    public static function mOrInt($bitler = array()) {
    	$bitler = array_values($bitler);
		$ilkBit = (int) $bitler[0];
		
        for ($z=1; $z < count($bitler); $z++) {
            $ilkBit |= (int) $bitler[$z];
        }
        return $ilkBit;
    }
	
    /**
     * Applies bitwise 'AND' logic for the integer values
     * 
     * @param array $bitler
     * @return number
     */
    public static function mAndInt($bitler = array()) {
		$ilkBit = (int) $bitler[0];
		
        for ($z=0; $z < count($bitler); $z++) {
            $ilkBit &= (int) $bitler[$z];
        }
        return $ilkBit;
    }
    
    /**
     * Applies bitwise 'OR' logic for the string structured values in given type of array
     * 
     * @param array $bitler
     * @return string
     */
    public static function mOr($bitler = array()) {
    	foreach ($bitler as $key => $value) {
    		/* Type casting */
			$bitler[$key] = (string) $value;
		}
		
		$yeniBitler = $bitler[0];
		
		for ($i=1; $i < count($bitler); $i++) {
	        if (strlen($yeniBitler) > strlen($bitler[$i])) {
	            $bitler[$i] = str_pad($bitler[$i], strlen($yeniBitler), '0', STR_PAD_LEFT);
	        } elseif (strlen($yeniBitler) < strlen($bitler[$i])) {
	            $yeniBitler = str_pad($yeniBitler, strlen($bitler[$i]), '0', STR_PAD_LEFT);
	        }
	        
			$strlenYeniBitler = strlen($yeniBitler);
			$ilkBit = '';
			
	        for ($z=0; $z < $strlenYeniBitler; $z++) { 
	            $ilkBit .= (($yeniBitler[$z] == '1' || $bitler[$i][$z] == '1') ? '1' : '0' );
	        }
			$yeniBitler = ltrim($ilkBit, "0");
		}
        return ($yeniBitler != null ? $yeniBitler : '0' );
    }

    /**
     * Applies bitwise 'AND' logic for the string structured values in given type of array
     * 
     * @param array $bitler
     * @return string
     */
    public static function mAnd($bitler = array()) {
    	foreach ($bitler as $key => $value) {
    		/* Type casting */
			$bitler[$key] = (string) $value;
		}
		
		$yeniBitler = $bitler[0];
		
		for ($i=1; $i < count($bitler); $i++) {
	        if (strlen($yeniBitler) > strlen($bitler[$i])) {
	            $bitler[$i] = str_pad($bitler[$i], strlen($yeniBitler), '0', STR_PAD_LEFT);
	        } elseif (strlen($yeniBitler) < strlen($bitler[$i])) {
	            $yeniBitler = str_pad($yeniBitler, strlen($bitler[$i]), '0', STR_PAD_LEFT);
	        }
	        
			$strlenYeniBitler = strlen($yeniBitler);
			$ilkBit = '';
			
	        for ($z=0; $z < $strlenYeniBitler; $z++) { 
	            $ilkBit .= (($yeniBitler[$z] == '1' && $bitler[$i][$z] == '1') ? '1' : '0' );
	        }
			$yeniBitler = ltrim($ilkBit, "0");
		}
        return ($yeniBitler != null ? $yeniBitler : '0' );
    }

    /**
     * Applies bitwise 'XOR' logic for the string structured values in given type of array
     * 
     * @param array $bitler
     * @return string
     */
    public static function mXoR($bitler = array()) {
    	foreach ($bitler as $key => $value) {
    		/* Type casting */
			$bitler[$key] = (string) $value;
		}
		
		$yeniBitler = $bitler[0];

		for ($i=1; $i < count($bitler); $i++) {
	        if (strlen($yeniBitler) > strlen($bitler[$i])) {
	            $bitler[$i] = str_pad($bitler[$i], strlen($yeniBitler), '0', STR_PAD_LEFT);
	        } elseif (strlen($yeniBitler) < strlen($bitler[$i])) {
	            $yeniBitler = str_pad($yeniBitler, strlen($bitler[$i]), '0', STR_PAD_LEFT);
	        }
	        
			$strlenYeniBitler = strlen($yeniBitler);
			$ilkBit = '';
			
	        for ($z=0; $z < $strlenYeniBitler; $z++) {
	        	$ilkBit .= (($yeniBitler[$z] !== $bitler[$i][$z]) ? '1' : '0' );
	        }
			$yeniBitler = ltrim($ilkBit, "0");
		}
        return ($yeniBitler != null ? $yeniBitler : '0' );
    }
    
    /**
     * Applies bitwise 'NOT' logic for the string structured values in given type of array
     * Returns the inverse of the string with the logic of bitwise
     * 
     * @param string $bit
     * @return string
     */
    public static function mNot($bit) {
        /* Type casting */
        $bit = (string) $bit;

        $yeniBitler = '';
        for ($i=0; $i < strlen($bit); $i++) { 
            $yeniBitler .= ($bit[$i] == '1' ? '0' : '1' );
        }
        return ltrim($yeniBitler, "0");
    }
    
    /**
     * Applies bitwise 'MINUS' logic for the string structured values in given type of array
     * As a result, it makes the second operand from the first operand extraction process
     * 
     * @param array $bitler
     * @return string
     */
    public static function mMinus($bitler = array()) {
    	foreach ($bitler as $key => $value) {
    		/* Type casting */
			$bitler[$key] = (string) $value;
		}
		
		$yeniBitler = $bitler[0];

		for ($i=1; $i < count($bitler); $i++) {
	        if (strlen($yeniBitler) > strlen($bitler[$i])) {
	            $bitler[$i] = str_pad($bitler[$i], strlen($yeniBitler), '0', STR_PAD_LEFT);
	        } elseif (strlen($yeniBitler) < strlen($bitler[$i])) {
	            $yeniBitler = str_pad($yeniBitler, strlen($bitler[$i]), '0', STR_PAD_LEFT);
	        }
	        
			$strlenYeniBitler = strlen($yeniBitler);
			$ilkBit = '';
			
	        for ($z=0; $z < $strlenYeniBitler; $z++) {
	        	$ilkBit .= ($yeniBitler[$z] == '1' ? ($bitler[$i][$z] == '1' ? '0' : '1') : '0' );
	        }
			$yeniBitler = ltrim($ilkBit, "0");
		}
        return ($yeniBitler != null ? $yeniBitler : '0' );
    }
}