2017 © Pedro Peláez
 

library line-map

Do stuff for each line of text

image

mattmezza/line-map

Do stuff for each line of text

  • Wednesday, March 21, 2018
  • by mattmezza
  • Repository
  • 1 Watchers
  • 1 Stars
  • 16 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 5 Versions
  • 14 % Grown

The README.md

PHP Line Map

Packagist PHP from Packagist GitHub license, (*1)

Maps a function through the lines of a file, line by line, (*2)

Install it with composer require mattmezza/line-map, (*3)

Use it like this:, (*4)

$map = Map\CSV::fileNamed("example.csv");
$rows = $map->with(function ($row, $idx) {
    return array_merge([$idx + 1], $row);
})->get();
$header = $map->getHeader();

or, (*5)

Map\Txt::fileNamed("lines.txt")->with(function ($line, $idx) {
    echo "$idx: $line";
});

TXT

// the callback function - can be also an anonymous closure
$doStuff = function($line, $lineNumber) {
    return $lineNumber . ": " . $line;
};

// from a string variable
$text = "line 1
line2
line3";
$newLines = Map\Txt::string($text)->with($doStuff)->toArray();

// from a filename
$filename = "./somelines.txt";
$newLines = Map\Txt::fileNamed($filename)->with($doStuff)->toArray();

// from a file resource handle
$file = fopen($filename, "r");
$newLines = Map\Txt::file($file)->with($doStuff)->toArray();

CSV

// the callback function - can be also an anonymous closure
$doStuff = function($row, $lineNumber, $headers) {
    echo $row[$headers[0]];
    return $row;
};

// from a string variable
$csv = "name,surname
John,Doe
Foo,Bar";
$newRows = Map\CSV::string($csv)->with($doStuff)->toArray();

// from a filename
$filename = "./somedata.csv";
$newRows = Map\CSV::fileNamed($filename)->with($doStuff)->toArray();

// from a file resource handle
$file = fopen($filename, "r");
$newRows = Map\CSV::file($file)->with($doStuff)->toArray();
example

Rapidly cycle through this CSV, (*6)

name,username,email
Matt,mattmezza,mattmezza@gmail.com

for each line generate custom message replacing values using the following template, (*7)

Hello {{name}},
your username is {{username}}.

send each message to the right user and collect some logs, (*8)

$tpl = "Hello __name__,
your username is __username__.";
$logs = Map\CSV::fileNamed("file.csv")->with(function($row, $idx, $headers) use ($tpl) {
    $msg = $tpl;
    foreach ($headers as $header) {
        $msg = str_replace("{{".$header."}}", $row[$header], $msg);
    }
    return sendMail($row["email"]);
})->toArray();
Matteo Merola mattmezza@gmail.com

The Versions

21/03 2018

dev-master

9999999-dev

Do stuff for each line of text

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

21/03 2018

3.0.1

3.0.1.0

Do stuff for each line of text

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

20/03 2018

3.0.0

3.0.0.0

Do stuff for each line of text

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

19/03 2018

2.0.0

2.0.0.0

Do stuff for each line of text

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

18/12 2017

1.0.0

1.0.0.0

Do stuff for each line of text

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires