Giter Site home page Giter Site logo

reverse-print-r's Introduction

Reverse print_r

This library provides six different handlers for reversing output of PHP's print_r function back to original variables. If there's no handler available for a type it's returned as string.

Assumptions

  • all values should be type-casted, not returned as string
  • empty string ("") is treated as null (see NullHandler)
  • integers are treated as integers (no boolean support)
  • multi-level array MUST be supported with type-casting
  • public, protected and private properties of Objects MUST be supported with type-casting

Known issues

  • no support for Object Inheritance

Installation

Package is available via Composer.

composer require simivar/reverse-print-r

Usage

<?php

$print_r_output = print_r([
    'string' => 'some text',
    'integer' => 1,
    'float' => 2.3,
    'subArray' => [
        'Hello World.',
    ],
], true);

$reverser = new \ReversePrintR\ReversePrintR($print_r_output);
echo $reverser->reverse()['float']; 
// outputs "2.3"

Changing behavior of Handlers

All handlers are defined as final, but thanks to Dependency Injection it's easy to change the behavior of library and it's type-casting. Let's say you want to keep all the empty strings "" as string, not null. All you have to do is create your own HandlerRunner without NullHandler.

<?php

$print_r_output = print_r([
    'string' => '',
    'null' => null,
], true);

$handlerRunner = new \ReversePrintR\HandlerRunner(
    new \ReversePrintR\Handler\FloatHandler(),
    new \ReversePrintR\Handler\IntegerHandler(),
    new \ReversePrintR\Handler\ArrayHandler(),
    new \ReversePrintR\Handler\ObjectHandler()
);

$reverser = new \ReversePrintR\ReversePrintR($print_r_output, $handlerRunner);
var_dump($reverser->reverse()['null']); 
// outputs ""

Own Handlers

The same way to removed NullHandler you can add your own handlers. All you have to do is make sure that it implements \ReversePrintR\Handler\HandlerInterface and you are good to go.

Versioning

Library is following Semver. All minor and patch updates are backwards compatible.

License

Please see the license file for more information.

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.