2017 © Pedro PelΓ‘ez
 

library smbarrayto

Array to csv, tsv, ltsv, xml, json, ...

image

shimabox/smbarrayto

Array to csv, tsv, ltsv, xml, json, ...

  • Friday, November 17, 2017
  • by shimabox
  • Repository
  • 1 Watchers
  • 0 Stars
  • 426 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 17 % Grown

The README.md

SMBArrayto

License Build Status Maintainability Coverage Status Latest Stable Version Latest Unstable Version, (*1)

Array to csv, tsv, ltsv, xml, json, ..., (*2)

Overview

  • Array to csv, tsv, ltsv, xml, json, ...
  • And provides a download function, output function, write function

Requirements

Installation

composer require shimabox/smbarrayto

Basic Usage

In the case of csv download

  • It does not create the actual file
    • It makes use of the temporary file
  • Use the header function
getDownloader();

// download
$csvDownloader->setHeader($header) // optional
              ->setRows($rows) // set the rows
              ->download('example.csv');
exit;
```

or

```php
getDownloader();

// download
$csvDownloader->setHeader($header) // optional
              ->addRow(['apple', 'γ‚Šγ‚“γ”', '1,000']) // add the row
              ->addRow(['pineapple', 'パむンをップル', '800']) // add the row
              ->download('example.csv');
exit;
```

#### Result

```csv
name,名前,price
apple,γ‚Šγ‚“γ”,"1,000"
pineapple,パむンをップル,800
```

- The default line feed code ```CRLF``` (only csv, tsv)
- The default encoding ```SJIS-win``` (only csv, tsv)
- Please perform the following if you want to change this
- e.g.)
```php
  // download
  $csvDownloader->setHeader($header)
                ->setRows($rows)
                ->setToConvert(false) // Not carried out the convert
                ->download('example.csv');
  exit;
```
  - Line feed code is ```LF```
  - Encoding is ```UTF-8```

## Example

- Use basically the following three interfaces
  - ```download($fileName);```
    - It does not create the actual fileγ€€(csv, tsv)
      - It makes use of the temporary file
    - Use the header function
  - ```output();```
    - It does not create the actual fileγ€€(csv, tsv)
      - It makes use of the temporary file
    - Use the header function
    - Simply echo()
  - ```write();```
    - Need to specify the path where you want to save by setFileName()

### In the case of CSV

**download($fileName);**

```php
getDownloader();

// download
$csvDownloader->setHeader($csvHeader)
              ->setRows($csvRows)
              ->download('example.csv');
exit;
```

**output();**

```php
getOutputter();

// output
$csvOutputter->setHeader($csvHeader)
             ->setRows($csvRows)
             ->output();
exit;
```

**write();**

```php
getWriter();

// write
$csvWriter->setHeader($csvHeader)
          ->setRows($csvRows)
          ->setFileName('/path/to/your/example.csv') // specify the path
          ->write();
```

### In the case of TSV

- With csv interface

```php
getDownloader();

// outputter object
$tsvOutputter = $tsv->getOutputter();

// writer object
$tsvWriter = $tsv->getWriter();
```

#### CSV, TSV configuration

- ``` setHeader(array $header); ```
  - You can set the header row
- ``` clearHeader(); ```
  - You can clear the header row
- ``` setToConvert($toConvert); ```
  - The default is ```true```
    - Convert the line feed code to ```CRLF```
    - To convert the encoding to ```SJIS-win```
  - If you set the ```false```, it does not perform the conversion

### In the case of LTSV

- It depends on [GitHub - fjyuu/monolog-ltsv-formatter: LTSV Formatter for Monolog](https://github.com/fjyuu/monolog-ltsv-formatter)

**download($fileName);**

```php
hoge = 123;
$obj->piyo = ['abc' => null, 'def' => false];

$ltsvRows = [
    'time' => "[2017-01-01 08:59:60]",
    'foo' => null,
    'bar' => true,
    'buz' => 0,
    'url' => 'http://example.net',
    'arr' => ['foo' => 'bar'],
    'obj' => $obj
];

// ltsv
$ltsv = Arrayto\Ltsv::factory();

// downloader object
$ltsvDownloader = $ltsv->getDownloader();

// download
$ltsvDownloader->setRows($ltsvRows)
               ->download('example.log');
exit;
```

**Result**

```
time:[2017-01-01 08:59:60]foo:NULLbar:truebuz:0url:http://example.netarr:{"foo":"bar"}obj:[object] (stdClass: {"hoge":123,"piyo":{"abc":null,"def":false}})
```

**output();**

- **It does nothing**

**write();**

```php
getWriter();

// write
$ltsvWriter->setRows($ltsvRows)
           ->setFileName('/path/to/your/example.log') // specify the path
           ->write();
```

#### LTSV configuration

- ``` overrideEOL($EOL); ```
  - You can override the line feed code
  - The default line feed code is ``` \n (LF) ```
  - e.g.)
  ```php
  // write
  $ltsvWriter->setRows($ltsvRows)
             ->setFileName('/path/to/your/example.log')
             ->overrideEOL("\r\n") // CRLF
             ->write();
  ```

### In the case of XML

- It depends on [GitHub - spatie/array-to-xml: A simple class to convert an array to xml](https://github.com/spatie/array-to-xml)

**download($fileName);**

```php
 ['category' => 'children', 'currency' => 'USD'],
        'tilte' => [
            '_attributes' => ['lang' => 'en'],
            '_value' => 'Harry Potter'
        ],
        'author' => 'J K. Rowling',
        'year' => 2005,
        'price' => 29.99
    ],
    [
        '_attributes' => ['category' => 'music', 'currency' => 'JPY'],
        'tilte' => [
            '_attributes' => ['lang' => 'ja'],
            '_value' => '[score] Boys&Gilrs'
        ],
        'author' => 'GOING STEADY(ιŠ€ζBOYZ)',
        'year' => 2000,
        'price' => "2,808"
    ]
];

// xml
$xml = Arrayto\Xml::factory();

// downloader object
$xmlDownloader = $xml->getDownloader();

// download
$xmlDownloader->setRows($xmlRows)
              ->setRootElementName('bookstore') // optional
              ->download('example.xml');
exit;
```

**Result**

```xml

<bookstore>
  <book category="children" currency="USD">
    <tilte lang="en">Harry Potter</tilte>
    <author>J K. Rowling</author>
    <year>2005</year>
    <price>29.99</price>
  </book>
  <book category="music" currency="JPY">
    <tilte lang="ja">[score] Boys&amp;Gilrs</tilte>
    <author>GOING STEADY(ιŠ€ζBOYZ)</author>
    <year>2000</year>
    <price>2,808</price>
  </book>
</bookstore>

output();, (*3)

  • It does nothing

write();, (*4)

getWriter();

// write
$xmlWriter->setRows($xmlRows)
          ->setRootElementName('bookstore') // optional
          ->setFileName('/path/to/your/example.xml') // specify the path
          ->write();
```

#### XML configuration

- ``` setRootElementName($name); ```
  - You can set the root element name
  - Default of the root element name is the ```root```
- ``` setReplaceSpacesByUnderScoresInKeyNames($bool); ```
  - Set to enable replacing space with underscore
  - The default is ```true```
- ``` toFormatOutput($toFormatOutput); ```
  - Nicely formats output with indentation and extra space
  - The default is ```true```
  - If you set the ```false```, it does not format
  - e.g.)
  ```php
    $xmlWriter->setRows($xmlRows)
              ->setRootElementName('bookstore') // optional
              ->setFileName('example.xml') // specify the path
              ->toFormatOutput(false) // it does not format
              ->write();
  ```
  **Result**
  ```xml
  
  <bookstore><book category="children" currency="USD"><tilte lang="en">Harry Potter</tilte><author>J K. Rowling</author><year>2005</year><price>29.99</price></book><book category="music" currency="JPY"><tilte lang="ja">[score] Boys&amp;Gilrs</tilte><author>GOING STEADY(ιŠ€ζBOYZ)</author><year>2000</year><price>2,808</price></book></bookstore>
  ```

### In the case of Json

**download($fileName)**

```php
<?php
use SMB\Arrayto;

$obj = new \stdClass();
$obj->hoge = '123';
$obj->piyo = ['abc' => null, 'def' => false];

$jsonRows = [
    ['key1' => null, 'key2' => true, 'key3' => 0],
    ['url' => 'http://example.net'],
    ['arr' => ['foo' => 'bar']],
    ['obj' => $obj]
];

// json
$json = Arrayto\Json::factory();

// downloader object
$jsonDownloader = $json->getDownloader();

// download
$jsonDownloader->setRows($jsonRows)
               ->download('example.json');
exit;

Result, (*5)

[
    {
        "key1": null,
        "key2": true,
        "key3": 0
    },
    {
        "url": "http://example.net"
    },
    {
        "arr": {
            "foo": "bar"
        }
    },
    {
        "obj": {
            "hoge": "123",
            "piyo": {
                "abc": null,
                "def": false
            }
        }
    }
]

output();, (*6)

<?php
use SMB\Arrayto;

// json
$json = Arrayto\Json::factory();

// outputter object
$jsonOutputter = $json->getOutputter();

// output
$jsonOutputter->setRows($jsonRows)
              ->output();
exit;

write();, (*7)

<?php
use SMB\Arrayto;

// json
$json = Arrayto\Json::factory();

// writer object
$jsonWriter = $json->getWriter();

// write
$jsonWriter->setRows($jsonRows)
           ->setFileName('/path/to/your/example.json') // specify the path
           ->write();

JSON configuration

  • setJsonEncodeOption($option);
    • You can override the json_encode options
    • PHP: json_encode - Manual - options
    • The default is 448 (JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT)
    • e.g.) php $jsonWriter->setRows($jsonRows) ->setFileName('/path/to/your/example.json') // specify the path ->setJsonEncodeOption(JSON_FORCE_OBJECT) // override ->write(); Result json {"0":{"key1":null,"key2":true,"key3":0},"1":{"url":"http:\/\/example.net"},"2":{"arr":{"foo":"bar"}},"3":{"obj":{"hoge":"123","piyo":{"abc":null,"def":false}}}}

Other functions of the Downloader

  • downloadExistsFile($fileName, $aliasOfFileName = '');, (*8)

    • Download an existing file
    <?php
    use SMB\Arrayto;
    
    // csv
    $csv = Arrayto\Csv::factory();
    
    // downloader object
    $csvDownloader = $csv->getDownloader();
    
    // download an existing file
    $csvDownloader->downloadExistsFile('/path/to/your/example.csv'); // file name is example.csv
    
    // or download an existing file as an alias
    $csvDownloader->downloadExistsFile('/path/to/your/example.csv', 'sample.csv'); // file name is sample.csv
    
    exit;
    
  • downloadExistsFileUsingWriter($fileName, Writable $writer);, (*9)

    • Download an existing file using Writer
    • Download after the writing of the file
    <?php
    use SMB\Arrayto;
    
    $rows = [
        ['apple', 'γ‚Šγ‚“γ”', '1,000'],
        ['pineapple', 'パむンをップル', '800']
    ];
    
    // csv
    $csv = Arrayto\Csv::factory();
    
    // writer object
    $csvWriter = $csv->getWriter()
                     ->setRows($rows)
                     ->setFileName('/path/to/your/example.csv'); // specify the path
    
    // downloader object
    $csvDownloader = $csv->getDownloader();
    
    // download after the writing of the file
    $csvDownloader->downloadExistsFileUsingWriter('sample.csv', $csvWriter); // file name is sample.csv
    
    exit;
    

Configuration

@see SMB\Arrayto\Traits\Storableγ€€, (*10)

  • addRow($row);, (*11)

    • Add the row
    use SMB\Arrayto;
    
    $obj = new \stdClass();
    $obj->hoge = '123';
    $obj->piyo = ['abc' => null, 'def' => false];
    
    // json
    $json = Arrayto\Json::factory();
    
    // write
    $json->getWriter()
       ->addRow(['key1' => null, 'key2' => true, 'key3' => 0])
       ->addRow(['url' => 'http://example.net'])
       ->addRow(['arr' => ['foo' => 'bar']])
       ->addRow(['obj' => $obj])
       ->setFileName('/path/to/your/example.json')
       ->write();
    

    Result, (*12)

    [
      {
          "key1": null,
          "key2": true,
          "key3": 0
      },
      {
          "url": "http://example.net"
      },
      {
          "arr": {
              "foo": "bar"
          }
      },
      {
          "obj": {
              "hoge": "123",
              "piyo": {
                  "abc": null,
                  "def": false
              }
          }
      }
    ]
    
  • addRowBySpecifyingKV($key, $value);, (*13)

    • To add a specified row keys and values
    <?php
    use SMB\Arrayto;
    
    $obj = new \stdClass();
    $obj->hoge = '123';
    $obj->piyo = ['abc' => null, 'def' => false];
    
    $obj2 = new \stdClass();
    $obj2->hoge = '456';
    $obj2->piyo = ['ghi' => null, 'jkl' => false];
    
    // json
    $json = Arrayto\Json::factory();
    
    // write
    $json->getWriter()
         ->addRowBySpecifyingKV('url', 'http://example.net')
         ->addRowBySpecifyingKV('url', 'http://example.org')
         ->addRowBySpecifyingKV('arr', ['foo' => 'bar'])
         ->addRowBySpecifyingKV('arr', ['baz' => 'fuga'])
         ->addRowBySpecifyingKV('obj', $obj)
         ->addRowBySpecifyingKV('obj', $obj2)
         ->setFileName('/path/to/your/example.json')
         ->write();
    

    Result, (*14)

    {
        "url": [
            "http://example.net",
            "http://example.org"
        ],
        "arr": [
            {
                "foo": "bar"
            },
            {
                "baz": "fuga"
            }
        ],
        "obj": [
            {
                "hoge": "123",
                "piyo": {
                    "abc": null,
                    "def": false
                }
            },
            {
                "hoge": "456",
                "piyo": {
                    "ghi": null,
                    "jkl": false
                }
            }
        ]
    }
    
  • setAllowDuplicateKey($toAllow);, (*15)

    • Whether set to allow duplicate keys
    • The default is true
    • csv,tsv behaves as false
    • If you set the false, it does not allow duplicate keys
    • e.g.)
    <?php
    use SMB\Arrayto;
    
    $obj = new \stdClass();
    $obj->hoge = '123';
    $obj->piyo = ['abc' => null, 'def' => false];
    
    $obj2 = new \stdClass();
    $obj2->hoge = '456';
    $obj2->piyo = ['ghi' => null, 'jkl' => false];
    
    // json
    $json = Arrayto\Json::factory();
    
    // write
    $json->getWriter()
         ->setAllowDuplicateKey(false) // it does not allow duplicate keys
         ->addRowBySpecifyingKV('url', 'http://example.net')
         ->addRowBySpecifyingKV('url', 'http://example.org')
         ->addRowBySpecifyingKV('arr', ['foo' => 'bar'])
         ->addRowBySpecifyingKV('arr', ['baz' => 'fuga'])
         ->addRowBySpecifyingKV('obj', $obj)
         ->addRowBySpecifyingKV('obj', $obj2)
         ->setFileName('/path/to/your/example.json')
         ->write();
    

    Result, (*16)

    {
        "url": "http://example.org",
        "arr": {
            "baz": "fuga"
        },
        "obj": {
            "hoge": "456",
            "piyo": {
                "ghi": null,
                "jkl": false
            }
        }
    }
    

Configuration for write

@see SMB\Arrayto\Traits\File, (*17)

  • setFileName($fileName);
    • Specify where to save the file path (is a required value)
    • '/path/to/your/example.csv' or '../example.csv' or 'example.csv' ...
  • setOpenMode($mode);, (*18)

    • Specify the open mode of the file
    • The default open mode is w
    • PHP: fopen - Manual - A list of possible modes for fopen()
    • e.g.)
    <?php
    use SMB\Arrayto;
    
    $header = ['name', '名前', 'feature'];
    
    $rows1 = [
        ['apple', 'γ‚Šγ‚“γ”', "Sweet\tRed"]
    ];
    
    // tsv
    $tsv = Arrayto\Tsv::factory();
    
    // writer object
    $tsvWriter = $tsv->getWriter();
    
    $tsvWriter->setHeader($header)
              ->setRows($rows1)
              ->setFileName('/path/to/your/example.tsv')
              ->write();
    
    $rows2 = [
        ['pineapple', 'パむンをップル', "Sour\tYellow"]
    ];
    $tsvWriter->clearHeader()
             ->setRows($rows2)
             ->setOpenMode('a') // set open mode 'a'
             ->write();
    
    $rows3 = [
        ['orange', 'γ‚ͺレンジ', "Juicy\tOrange"]
    ];
    $tsvWriter->setRows($rows3)
              ->write();
    

    Result, (*19)

    name<TAB>名前<TAB>feature
    apple<TAB>γ‚Šγ‚“γ”<TAB>"Sweet<TAB>Red"
    pineapple<TAB>パむンをップル<TAB>"Sour<TAB>Yellow"
    orange<TAB>γ‚ͺレンジ<TAB>"Juicy<TAB>Orange"
    
  • setPermission($permission);
    • You can set the file permissions
    • The default permission is 666
    • e.g.) php $csvWriter->setRows($rows) ->setPermission(777) // set the permissions to 777 ->write();

Other methods of instantiation

  • Arrayto::factory(XXX);
  <?php
  use SMB\Arrayto;

  $csv = Arrayto::factory(Arrayto::CSV);   // => SMB\Arrayto\Csv
  // $csv == Arrayto\Csv::factory();

  $tsv = Arrayto::factory(Arrayto::TSV);   // => SMB\Arrayto\Tsv
  // $tsv == Arrayto\Tsv::factory();

  $ltsv = Arrayto::factory(Arrayto::LTSV); // => SMB\Arrayto\Ltsv
  // $ltsv == Arrayto\Ltsv::factory();

  $xml = Arrayto::factory(Arrayto::XML);   // => SMB\Arrayto\Xml
  // $xml == Arrayto\Xml::factory();

  $json = Arrayto::factory(Arrayto::JSON); // => SMB\Arrayto\Json
  // $json == Arrayto\Json::factory();
  • Arrayto\Plugins\XXX\XXX
  <?php
  use SMB\Arrayto;

  $csvDownloader = new Arrayto\Plugins\Csv\Downloader();
  // $csvDownloader == Arrayto\Csv::factory()->getDownloader();
  $csvOutputter = new Arrayto\Plugins\Csv\Outputter();
  // $csvOutputter == Arrayto\Csv::factory()->getOutputter();
  $csvWriter = new Arrayto\Plugins\Csv\Writer();
  // $csvWriter == Arrayto\Csv::factory()->getWriter();

  $tsvDownloader = new Arrayto\Plugins\Tsv\Downloader();
  // $tsvDownloader == Arrayto\Tsv::factory()->getDownloader();
  $tsvOutputter = new Arrayto\Plugins\Tsv\Outputter();
  // $tsvOutputter == Arrayto\Tsv::factory()->getOutputter();
  $tsvWriter = new Arrayto\Plugins\Tsv\Writer();
  // $tsvWriter == Arrayto\Tsv::factory()->getWriter();

  $ltsvDownloader = new Arrayto\Plugins\Ltsv\Downloader();
  // $ltsvDownloader == Arrayto\Ltsv::factory()->getDownloader();
  $ltsvWriter = new Arrayto\Plugins\Ltsv\Writer();
  // $ltsvWriter == Arrayto\Ltsv::factory()->getWriter();

  $xmlDownloader = new Arrayto\Plugins\Xml\Downloader();
  // $xmlDownloader == Arrayto\Xml::factory()->getDownloader();
  $xmlWriter = new Arrayto\Plugins\Xml\Writer();
  // $xmlWriter == Arrayto\Xml::factory()->getWriter();

  $jsonDownloader = new Arrayto\Plugins\Json\Downloader();
  // $jsonDownloader == Arrayto\Json::factory()->getDownloader();
  $jsonOutputter = new Arrayto\Plugins\Json\Outputter();
  // $jsonOutputter == Arrayto\Json::factory()->getOutputter();
  $jsonWriter = new Arrayto\Plugins\Json\Writer();
  // $jsonWriter == Arrayto\Json::factory()->getWriter();

Testing

vendor/bin/phpunit

License

The MIT License (MIT). Please see License File for more information., (*20)

The Versions

17/11 2017

dev-master

9999999-dev https://github.com/shimabox/SMBArrayto

Array to csv, tsv, ltsv, xml, json, ...

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar shimabox

csv json xml array convert tsv ltsv

17/11 2017

v1.0.1

1.0.1.0 https://github.com/shimabox/SMBArrayto

Array to csv, tsv, ltsv, xml, json, ...

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar shimabox

csv json xml array convert tsv ltsv

17/11 2017

dev-codeclimate

dev-codeclimate https://github.com/shimabox/SMBArrayto

Array to csv, tsv, ltsv, xml, json, ...

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar shimabox

csv json xml array convert tsv ltsv

21/12 2016

v1.0.0

1.0.0.0 https://github.com/shimabox/SMBArrayto

Array to csv, tsv, ltsv, xml, json, ...

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar shimabox

csv json xml array convert tsv ltsv