popphp/pop-data

This package is abandoned and no longer maintained. No replacement package was suggested.

Pop Data Component for Pop PHP Framework

2.1.0p1 2017-03-02 15:18 UTC

This package is auto-updated.

Last update: 2022-02-01 12:42:27 UTC


README

END OF LIFE

The pop-data component v2.1.0 is now end-of-life. The CSV sub-component has been forked and pushed into its own repository:

  • popphp/pop-csv

Build Status Coverage Status

OVERVIEW

pop-data provides a streamlined way to convert common data types. With it, you can easily give it some native PHP data and quickly produce a serialized version of that data in a common data type, such as CSV, JSON, SQL, XML or YAML. Or, conversely, you can give it some serialized data, an it will auto-detect the format and convert it to native PHP data.

pop-datais a component of the Pop PHP Framework.

INSTALL

Install pop-data using Composer.

composer require popphp/pop-data

BASIC USAGE

Serialize Data

$phpData = [
    [
        'first_name' => 'Bob',
        'last_name'  => 'Smith'
    ],
    [
        'first_name' => 'Jane',
        'last_name'  => 'Smith'
    ]
];

$data = new Pop\Data\Data($phpData);

$csvString   = $data->serialize('csv');
$jsonString  = $data->serialize('json');
$sqlString   = $data->serialize('sql');
$xmlString   = $data->serialize('xml');
$yamlString  = $data->serialize('yaml');

The $csvString variable now contains:

first_name,last_name
Bob,Smith
Jane,Smith

The $jsonString variable now contains:

[
    {
        "first_name": "Bob",
        "last_name": "Smith"
    },
    {
        "first_name": "Jane",
        "last_name": "Smith"
    }
]

The $sqlString variable now contains:

INSERT INTO data (first_name, last_name) VALUES
('Bob', 'Smith'),
('Jane', 'Smith');

The $xmlString variable now contains:

<?xml version="1.0" encoding="utf-8"?>
<data>
  <row>
    <first_name>Bob</first_name>
    <last_name>Smith</last_name>
  </row>
  <row>
    <first_name>Jane</first_name>
    <last_name>Smith</last_name>
  </row>
</data>

The $yamlString variable now contains:

---
- first_name: Bob
  last_name: Smith
- first_name: Jane
  last_name: Smith
...

Unserialize Data

You can either pass the data object a direct string of serialized data or a file containing a string of serialized data. It will detect which one it is and parse it accordingly.

String
$csv     = new Pop\Data\Data($csvString);
$phpData = $csv->unserialize();
File
$xml     = new Pop\Data\Data('/path/to/file.xml');
$phpData = $xml->unserialize();

Convert Between Data Types

$csv = new Pop\Data\Data($csvString);
$xml = $csv->convert('xml');

Write to File

$phpData = [ ... ];

$data = new Pop\Data\Data($phpData);
$data->serialize('csv');
$data->writeToFile('/path/to/file.csv');

Output to HTTP

$phpData = [ ... ];

$data = new Pop\Data\Data($phpData);
$data->serialize('csv');
$data->outputToHttp();
Force download of file
$phpData = [ ... ];

$data = new Pop\Data\Data($phpData);
$data->serialize('csv');
$data->outputToHttp('my-file.csv', true);