Giter Site home page Giter Site logo

copycat's Introduction

CopyCat

A simple PHP to "copy" data from one source to another

Latest Stable Version Total Downloads Latest Unstable Version License Build Status Scrutinizer Code Quality codecov

Requirement

PHP >= 5.6

Getting Started

Install library with composer

composer require jackal/copycat

Usage

Basic example: from array to sql insert statement

require_once __DIR__.'/vendor/autoload.php';
$reader = new \Jackal\Copycat\Reader\ArrayReader([
    ['col1' => 'value1','col2' => 'value2'],
    ['col1' => 'value3','col2' => 'value4'],
    /*...*/
]);

$workflow = new \Jackal\Copycat\Workflow($reader);
$workflow->addWriter(new \Jackal\Copycat\Writer\SQLFileWriter('test_table','test_table.sql'));
$workflow->process();

echo file_get_contents(__DIR__.'/test_table.sql');

From array to array

$reader = new \Jackal\Copycat\Reader\ArrayReader([
    ['value1'],
    ['value2'],
    /*...*/
]);

$workflow = new \Jackal\Copycat\Workflow($reader);
$workflow->addWriter(new \Jackal\Copycat\Writer\ArrayWriter($outputArray));

$workflow->process();

var_dump($outputArray);

Filters

With Filters you can apply logic to exclude certain values from the output.

/*[...]*/
/*Define wich column to apply filter*/
$workflow->addFilter(new NotBlankFilter('col1'));
/*[...]*/

Filters are callable objects, you can define your own filter

/*[...]*/
/*Define custom filter*/
$workflow->addFilter(function($values){
    return $values['col1'] > 0;
});
/*[...]*/

Converter

With Converter you can modify values to the output.

/*[...]*/
/*Define custom filter*/
$workflow->addConverter(new DatetimeToStringConverter('col1'));
/*[...]*/

You can set your own converter

/*[...]*/
//apply custom converter
$workflow->addConverter(function ($values){
    foreach ($values as &$value) {
        if ($value == 'to convert') {
            $value = 'converted';
        }
    }
    return $values;
});
/*[...]*/

Sorting

Is it possible to sort output values

/*[...]*/
//add sorter
$workflow->addSorter(new AscendingSorter('col1'));
/*[...]*/

If you want to sort values basing on multiple columns, you can just add multiple params. Use with care! this method could create performance issues on large input data

/*[...]*/
//add sorter
$workflow->addSorter(new AscendingSorter('col1','col2','col3',/*...*/));
/*[...]*/

Authors

  • Luca Giacalone (AKA JackalOne)

License

This project is licensed under the MIT License

copycat's People

Stargazers

Luca Giacalone avatar Ayman Rady avatar Lorenzo avatar

Watchers

James Cloos avatar Lorenzo avatar Luca Giacalone 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.