File parsing tool with a configured blueprint design with support for ods
, xlsx
, xls
, xml
, html
, sylk
, csv
file types out-of-the-box.
Require PowerParser using Composer:
composer require husam-awadhi/power-parser
To parse a file you'll need to a Parser
instance which can be done by using these 3 lines. which will return the parsed file data, ready for your magic! 🎇
try {
$pp = new PowerParser();
$parser = $pp->getParserBuilder(
stream: 'path-to-blueprint.yml',
file: 'path-to-file.ext'
)->build();
$parsedData = $parser->parse()->getAsArray();
} catch (Exception $e) {
// hmm
}
refer to example, Valid Blueprint
blueprint version. not yet utilized but will be used for backward compatibility.
object contains meta data.
object contains parsed file meta data.
parsed file extension.
parsed file name. only used in the returned parsed file data.
array of main parameters to capture data in parsed file.
code of captured data, when returning the parsed as array this will be used as array key for the matched data.
when true, parsing will throw an exception if data not matched in the parsed file.
rules and clauses for finding an element.
expected location, a column number in Excel or CSV file
Used to match the condition with the given value. If you want to use null as a value, use "{null}"
, which will be converted in the BlueprintInterpreter
.
once a match has been found, any data defined here will be captured.
The name of the data found in the field. When returning the parsed data as an array, this will be used as the array key for the matched data.
cell number in Excel or CSV files
formats applied to matched data, refer to Processors
processors has 2 types,
- bool-strict:
true
- if value =
true
,1
,"true"
- if value =
- bool:
false
,- if value has a value and is not equal to
true
. - If value is
true
,1
, or"true"
.
- if value has a value and is not equal to
- int: PHP casting
- float: PHP casting
Example:
blueprint:
- fields:
- name: cash
position: 1
type: int
- money
- string length limits
Example:
blueprint:
- fields:
- name: cash
position: 1
format: f%2 # input: 23.441 output: 23.44
- name: cash
position: 1
format: s%5 # input: 'sweets' output: 'sweet'
PowerParser is an open-sourced software licensed under the MIT license.