chi-teck / trafaret
This package is abandoned and no longer maintained.
No replacement package was suggested.
Trafaret data processor
1.0.0
2022-02-25 18:00 UTC
Requires
- php: >=7.3
- ext-ctype: *
- ext-dom: *
- ext-libxml: *
- symfony/polyfill-php80: ^1.16
- symfony/validator: ^4.4 || ^5.1 || ^6.0
Requires (Dev)
- phpunit/phpunit: ^9.3
- slevomat/coding-standard: ^6.3
- squizlabs/php_codesniffer: ^3.5
- symfony/var-dumper: ^5.0 || ^6.0
Suggests
- ext-tidy: *
This package is auto-updated.
Last update: 2024-08-25 23:54:07 UTC
README
A simple way to extract data from multiline textual snippets.
Installation
composer require chi-teck/trafaret --dev
Usage
$input = <<< 'HTML' <h1>Example</h1> <div> <time>12:08</time> <span class="total">15</span> </div> HTML; $trafaret = new Trafaret( <<< 'HTML' <h1>Example</h1> <div> <time>{{ time }}</time> <span class="total">{{ total }}</span> </div> HTML, [ 'time' => new Regex('/^\d\d:\d\d$/'), 'total' => new GreaterThan(10), ], ); $manager = Manager::createDefault(); try { $data = $manager->apply($trafaret, $input); } catch (ExceptionInterface $exception) { \file_put_contents('php://stderr', $exception->getMessage() . "\n"); exit(1); } print_r($data);
Placeholders are validated using Symfony Validator.
For tests based on PHPUnit you can make use of TrafaretTrait to validate markup as shown below.
final class HomePageTest extends SiteTestCase { use TrafaretTrait; public function testMarkup(): void { $trafaret = Trafaret::createFromFile(__DIR__ . '/../fixtures/home-page.html.trf'); $actual_html = $this->findByXpath('//div[@class = "page"]')->getOuterHtml(); $this->assertStringByTrafaret($trafaret, $actual_html); } }
License
MIT License