PHP code example of tobento / service-file-creator

1. Go to this page and download the library: Download tobento/service-file-creator library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tobento / service-file-creator example snippets


use Tobento\Service\FileCreator\FileCreator;
use Tobento\Service\FileCreator\FileCreatorException;

try {
    (new FileCreator())
        ->content('Lorem ipsum')
        ->newline()
        ->content('Lorem ipsum')
        ->create('home/public/files/filename.txt', FileCreator::CONTENT_NEW);
    // it is ok.
} catch (FileCreatorException $e) {
    // it failed.
}

use Tobento\Service\FileCreator\FileCreator;
use Tobento\Service\FileCreator\FileCreatorException;

try {
    (new FileCreator())
        ->content('Lorem ipsum')
        ->create(
            file: 'home/public/files/filename.txt',
            handling: FileCreator::NO_OVERWRITE,
            modeFile: 0644,
            modeDir: 0755
        );
    // it is ok.
} catch (FileCreatorException $e) {
    // it failed.
}

use Tobento\Service\FileCreator\FileCreator;
use Tobento\Service\FileCreator\FileCreatorException;

try {
    (new FileCreator())
        ->content('Lorem ipsum')
        ->newline()
        ->content('Lorem ipsum')
        ->create('home/public/files/filename.txt', FileCreator::CONTENT_NEW)
        ->newline(num: 2)
        ->content('Lorem ipsum')
        ->create('home/public/files/filename.txt', FileCreator::CONTENT_APPEND);
    // it is ok.
} catch (FileCreatorException $e) {
    // it failed.
}

namespace Tobento\Service\FileCreator;

/**
 * WriterInterface
 */
interface WriterInterface
{
    /**
     * Write the content.
     *
     * @param resource $resource
     * @return void
     */
    public function write($resource): void;
}

use Tobento\Service\FileCreator\FileCreator;
use Tobento\Service\FileCreator\FileCreatorException;
use Tobento\Service\FileCreator\Writer\Csv;

$items = [
    ['id' => 1, 'title' => 'cars'],
    ['id' => 2, 'title' => 'plants'],
];

$csvWriter = new Csv(
    items: $items,
    delimiter: ',', // default
    enclosure: '"', // default
    escapeChar: '\\', // default
);

try {
    (new FileCreator())
        ->writer($csvWriter)
        ->create('home/public/files/filename.csv', FileCreator::CONTENT_NEW);
    // it is ok.
} catch (FileCreatorException $e) {
    // it failed.
}

use Tobento\Service\FileCreator\FileCreator;
use Tobento\Service\FileCreator\FileCreatorException;
use Tobento\Service\FileCreator\Formatter\Printr;

$items = [
    ['id' => 1, 'title' => 'cars'],
    ['id' => 2, 'title' => 'plants'],
];

try {
    (new FileCreator())
        ->content((new Printr())->format($items))
        ->create('home/public/files/filename.txt', FileCreator::CONTENT_NEW);
    // it is ok.
} catch (FileCreatorException $e) {
    // it failed.
}