Code Monkey home page Code Monkey logo

php-bitmap's Introduction

php-bitmap

Bitmap, also called bit array is a data structure that compactly store set of values as bits of integer. More data can be read at http://en.wikipedia.org/wiki/Bit_array.

It is useful when required compact way to represent combination of values and simple manipulations with them. One byte can represent eight independent values.

Installation

{
    "require": {
        "sokil/php-bitmap": "1.0"
    }
}

Useage

Lets see example. Errors in PHP represents as constants:

E_ERROR = 1 (0);
E_WARNING = 2 (1);
E_PARSE = 4 (2);
E_NOTICE = 8 (3);
E_CORE_ERROR = 16 (4);
E_CORE_WARNING = 32 (5);
E_COMPILE_ERROR = 64 (6);
E_COMPILE_WARNING = 128 (7);
E_USER_ERROR = 256 (8);
E_USER_WARNING = 512 (9);
E_USER_NOTICE = 1024 (10);
E_STRICT = 2048 (11);
E_RECOVERABLE_ERROR = 4096 (12);
E_DEPRECATED = 8192 (13);
E_USER_DEPRECATED = 16384 (14);
E_ALL = 32767 (15);

Every error level represent logical "1", and combination of all this values may be represent only by two bytes. E_ERROR represent first bit of byte, E_WARNING - second, etc.

Combination of E_WARNING and E_NOTICE in binary system is "1010" or 10 in decimal system.

Class that represents bitmap of PHP errors:

class PhpError extends \Sokil\Bitmap
{
    /**
     * Show errors
     * Set first bit, which represents E_ERROR, to "1"
     */
    public function showErrors()
    {
        $this->setBit(0);
        return $this;
    }

    /**
     * Hide errors
     * Set first bit, which represents E_ERROR, to "0"
     */
    public function hideErrors()
    {
        $this->unsetBit(0);
        return $this;
    }

    /**
     * Check if errors shown
     * Check if first bit set to 1
     */
    public function isErrorsShown()
    {
        return $this->isBitSet(0);
    }

    /**
     * Show warnings
     * Set second bit, which represents E_WARNING, to "1"
     */
    public function showWarnings()
    {
        $this->setBit(1);
        return $this;
    }

    /**
     * Hide warnings
     * Set second bit, which represents E_WARNING, to "0"
     */
    public function hideWarnings()
    {
        $this->unsetBit(1);
        return $this;
    }

    /**
     * Check if warnings shown
     * Check if second bit set to 1
     */
    public function isWarningsShown()
    {
        return $this->isBitSet(1);
    }
}

Now we can easely manipulate with errors:

// load current error levels
$error = new PhpError(error_reporting());

// enable errors and warnings
$error->showErrors()->showWarnings();

// set error reporting
error_reporting($error->toInt());

// check if warnings shown
var_dump($error->isWarningsShown());

// value may be set by mask
// E_USER_ERROR | E_USER_WARNING is 256 + 512;
$error->setBitsByMask(E_USER_ERROR + E_USER_WARNING);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.