Coder Social home page Coder Social logo

open-source-contributions / un-stow Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jordanbrauer/un-stow

0.0 1.0 0.0 71 KB

Offers a nice & powerful OOP and/or FP interface for PHP's `pack` and `unpack` functions.

License: MIT License

PHP 100.00%

un-stow's Introduction

(Un)Stow

Offers a nice & powerful OOP and/or FP interface for PHP's pack and unpack functions.

Install

Install with Composer

$ composer require jordanbrauer/un-stow

Examples

Below are a few basic examples of how to use this library.

Hello World

Simple "Hello World!" example for packing (stow) and unpacking (unstow) hexadecimal data.

stow('48656C6C6F20576F726C6421')->hexNibbleHigh('*')->close();
# = (string) Hello World!

unstow('Hello World!')->hexNibbleHigh('*', 'hello_world')->open();
# = (array) [
#     'hello_world' => 48656C6C6F20576F726C6421,
# ],

GIF Header

Even though we have other methods of checking for GIF image data now, this serves as a good example of how unstow works for unpacking binary data to read headers.

$stream = fopen('./tests/Portal.gif', 'rb');
$bytes = fread($stream, 20);

fclose($stream);

$gifHeader = unstow($bytes)
    ->spacePaddedString(6, 'version')
    ->unsignedChar(2, 'width')
    ->unsignedChar(2, 'height')
    ->unsignedChar(1, 'flag')
    ->nullFill(11)
    ->unsignedChar(1, 'aspect')
    ->open();

Reusing Packers

With this library you are able to create "template" packers and unpackers which have a predefined pattern and can accept arbitrary data to pack/unpack.

Object-Oriented Method

To re-use a packer in an OOP way, simply omit the bytes from your constructor and chain your format method calls to create your pattern. Next you can pass arbitrary data to the load method on and call close/open to process the data according to the format.

E.g.,

$packer = stow()
    ->unsignedShort('', Stow::BIG_ENDIAN_16_BIT)
    ->unsignedShort('', Stow::LITTLE_ENDIAN_16_BIT)
    ->signedChar('*');

echo $packer->load(0x1234, 0x5678, 65, 66)->close();

Functional Method

The functional method is quite similar to the object-oriented way, except two small differences.

  1. After chaining all your format method calls, finish it off by calling the standby method.
    • This method will return a closure that accepts a set of variadic arguments of arbitrary data.
  2. No need to call the open/close methods: the closure simply returns the packed/unpacked data!

E.g.,

$packer = stow()
    ->unsignedShort('', Stow::BIG_ENDIAN_16_BIT)
    ->unsignedShort('', Stow::LITTLE_ENDIAN_16_BIT)
    ->signedChar('*')
    ->standby();

echo $packer(0x1234, 0x5678, 65, 66);

un-stow's People

Contributors

jordanbrauer avatar

Watchers

James Cloos avatar

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.