PHP code example of mapsight / pulp-pdo
1. Go to this page and download the library: Download mapsight/pulp-pdo 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/ */
mapsight / pulp-pdo example snippets
[
['source' => 'stops.json', 'id' => '1', 'name' => 'Main Station'],
['source' => 'stops.json', 'id' => '2', 'name' => 'Town Hall'],
]
use OpenMapsight\Pulp;
use OpenMapsight\pulp\File;
use OpenMapsight\PulpJSON;
use OpenMapsight\PulpPdo;
Pulp::start()
->pipe(Pulp::src('.*\.json', __DIR__ . '/stops'))
->pipe(PulpJSON::decodeJSON())
->pipe(Pulp::map(static function (File $file): File {
$file->content = array_map(
static fn(array $stop): array => ['source' => $file->fileName, 'id' => $stop['id'], 'name' => $stop['name']],
$file->content,
);
return $file;
}))
->pipe(PulpPdo::write(
connection: static fn(): PDO => new PDO($dsn, $user, $password),
table: 'stops',
options: ['replace' => static fn(File $file): array => ['source' => $file->fileName]],
))
->run();
->pipe(PulpPdo::transaction($pdo, static function (PDO $pdo, File $file): void {
$pdo->prepare('DELETE FROM stops WHERE source = ?')->execute([$file->fileName]);
// insert, update, compare with stored rows …
}))
Pulp::start()
->pipe(PulpPdo::query($pdo, 'SELECT id, name FROM stops WHERE source = ?', ['a.json'], 'stored.json'))
->pipe(Pulp::results(static function (array $files): void {
// $files[0]->content: [['id' => …, 'name' => …], …]
}))
->run();