microformats / php-mf2

php-mf2 is a pure, generic microformats-2 parser for PHP. It makes HTML as easy to consume as JSON.
Creative Commons Zero v1.0 Universal
191 stars 38 forks source link

Improve main parser entry point API and config #232

Open barnabywalters opened 2 years ago

barnabywalters commented 2 years ago

Currently, the parser has a variety of different entry points:

Mf2\fetch('url');
Mf2\parse('html', 'url');
$p = new Parser('html', 'url');
$p->parse();
$p->parseFromId();

Additionally, there is a variety of different ways of setting various configuration options and feature flags, often just by passing a boolean to a positional argument. Depending on the entry point, a boolean in a particular position might do something completely different.

For v1.0 we could make the most of the possibility for breaking changes to improve the API for better consistency, readability and extensibility.

Inspired by the XRay API, one potential improvement could be replacing the fetch and parse functions with a single parse function which does both depending on its arguments:

Mf2\parse($url); // Fetches and parses the contents of $url
Mf2\parse($url, $html); // Parses $html using $url as a base URL

One potential solution to the config issue would be to allow passing a $config array or object to any of the main entry points, e.g.

$config = [
  'lang' => true,
  'curl_info' => &$curlInfo,
  'json_mode' => true,
  'debug' => true,
  'parse_classic' => true
];

// Used with the existing entry point functions:
Mf2\fetch('url', $config);
Mf2\parse('html', 'url', $config);
$p = new Parser('html', 'url', $config);

// Or, with the fetch/parse change suggested above:
Mf2\parse('url', $config);
Mf2\parse('url', 'html', $config);

The config keys themselves are only suggestions and could likely be improved.

With some type checking, it may even be possible to implement this while maintaining back compatibility with the old APIs.