PHP code example of satooshi / file-client

1. Go to this page and download the library: Download satooshi/file-client 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/ */

    

satooshi / file-client example snippets




use Contrib\Component\File\Client\Plain\FileReader;

// construction
$path = '/path/to/file';
$client = new FileReader($path);



use Contrib\Component\File\Client\Plain\FileReader;

// default options
$options = array(
    'newLine'              => PHP_EOL,
    'throwException'       => true, // throw exception on runtime error
    'autoDetectLineEnding' => true, // better line ending handling on Mac
);

// construct with options
$client = new FileReader($path, $options);



use Contrib\Component\File\Client\Plain\FileReader;

$path = '/path/to/file';
$client = new FileReader($path);

// read
$content = $client->read();
$lines = $client->readLines();



use Contrib\Component\File\Client\Plain\FileWriter;

$path = '/path/to/file';
$client = new FileWriter($path);

// write
$content = 'hello world!';
$client->write($content);

$lines = array(
    'line1',
    'line2',
);
$client->writeLines($lines);



use Contrib\Component\File\Client\Plain\FileAppender;

$path = '/path/to/file';
$client = new FileAppender($path);

// append
$content = 'hello world!';
$client->write($content);

$lines = array(
    'line1',
    'line2',
);
$client->writeLines($lines);



use Contrib\Component\File\Client\Plain\FileReaderIterator;

// construction
$path = '/path/to/file';
$client = new FileReaderIterator($path);



use Contrib\Component\File\Client\Plain\FileReaderIterator;

// default options
$options = array(
    'newLine'              => PHP_EOL,
    'throwException'       => true, // throw exception on runtime error
    'autoDetectLineEnding' => true, // better line ending handling on Mac
    'skipEmptyCount'       => true,
    'limit'                => 0,
    'offset'               => 0,
);

// construct with options
$client = new FileReaderIterator($path, $options);



use Contrib\Component\File\Client\Plain\FileReaderIterator;

// construction
$path = '/path/to/file';
$client = new FileReaderIterator($path);

// walk
$client->walk(
    funtion ($line, $numLine) {
        if ($numLine === 1) {
            // do something at line 1
       }
    }
);



use Contrib\Component\File\Client\Generic\GenericFileReader;

// construction
$path = '/path/to/file';
$client = new GenericFileReader($path);



use Contrib\Component\File\Client\Generic\GenericFileReader;

// default options
$options = array(
    'newLine'              => PHP_EOL,
    'throwException'       => true, // throw exception on runtime error
    'autoDetectLineEnding' => true, // better line ending handling on Mac
);

// construct with options
$client = new GenericFileReader($path, $options);



use Contrib\Component\File\Client\Generic\GenericFileReader;

$path = '/path/to/file';
$client = new GenericFileReader($path);

// read as json
$content = $client->readAs('json');
$lines = $client->readLinesAs('ltsv');

// read as json to object
$content = $client->readAs('json', 'Entity');
$lines = $client->readLinesAs('ltsv', 'Entity');



use Contrib\Component\File\Client\Generic\GenericFileWriter;

$path = '/path/to/file';
$client = new GenericFileWriter($path);

// write Entity
$content = new Entity();
$client->writeAs($content);

$lines = array(
    new Entity(),
    new Entity(),
);
$client->writeLinesAs($lines);



use Contrib\Component\File\Client\Generic\GenericFileAppender;

$path = '/path/to/file';
$client = new GenericFileAppender($path);

// append Entity
$content = new Entity();
$client->writeAs($content);

$lines = array(
    new Entity(),
    new Entity(),
);
$client->writeLinesAs($lines);



use Contrib\Component\File\Client\Plain\GenericFileReaderIterator;

// construction
$path = '/path/to/file';
$client = new GenericFileReaderIterator($path);



use Contrib\Component\File\Client\Plain\GenericFileReaderIterator;

// default options
$options = array(
    'newLine'              => PHP_EOL,
    'throwException'       => true, // throw exception on runtime error
    'autoDetectLineEnding' => true, // better line ending handling on Mac
    'skipEmptyCount'       => true,
    'limit'                => 0,
    'offset'               => 0,
);

// construct with options
$client = new GenericFileReaderIterator($path, $options);



use Contrib\Component\File\Client\Plain\GenericFileReaderIterator;

// construction
$path = '/path/to/file';
$client = new GenericFileReaderIterator($path);

// walk as json
$client->walkAs(
    funtion ($line, $numLine) {
        if ($numLine === 1) {
            // do something at line 1
       }
    },
    'json'
);

// walk as json to object
$client->walkAs(
    funtion ($line, $numLine) {
        if ($numLine === 1) {
            // do something at line 1
       }
    },
    'json',
    'Entity'
);