@ -70,50 +68,28 @@ class Microformats implements \ArrayAccess, \JsonSerializable {
* @param string $url The effective URL (after redirections) of the document if known
* @param array $options Options for the parser; please see the class documentetation for details
*/
public static function fromHTMLElement(\DOMElement $input, string $url, array $options = []): self {
return new static((new MfParser)->parseHTMLElement($input, $url, $options));
public static function fromHTMLElement(\DOMElement $input, string $url, array $options = []): array {
return (new MfParser)->parseHTMLElement($input, $url, $options);
}
/** Imports a plain array into this wrapper class
/** Serializes a Microformats structure to JSON.
*
* This is mainly useful for proper JSON serialization.
* This is necessary to serialize empty hash tables (JSON objects)
* correctly. It cannot cover all possible cases of manipulation, but
* does cover cases which normally occur with data in the wild.
*
* @param array $data The complete Microformats associative array
* @param array $data The Microformats structure to serialize
* @param int $flags [optional] Bitmask consisting of JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT, JSON_UNESCAPED_UNICODE. JSON_THROW_ON_ERROR The behaviour of these constants is described on the JSON constants page
* @param int $depth [optional] Set the maximum depth. Must be greater than zero.
*/
public function __construct(array $data) {
$this->data = $data;
}
public function offsetExists(mixed $offset): bool {
return isset($this->data[$offset]);
}
public function &offsetGet(mixed $offset): mixed {
return $this->data[$offset];
}
public function offsetSet(mixed $offset, mixed $value): void {
$this->data[$offset] = $value;
}
public function offsetUnset(mixed $offset): void {
unset($this->data[$offset]);
}
public function jsonSerialize(): mixed {
// In order for a Microformats structure to serialize to JSON correctly
// we must ensure empty hash tables serialize to objects rather than
// arrays as they otherwise would. This cannot cover all possible
// cases of manipulation, but does cover cases which normally occur
// with data in the wild.
$data = $this->data;
$walk = function(&$arr) {
public static function toJson(array $data, int $flags = 0, int $depth = 512): string {
$walk = function(&$arr) use(&$walk) {
foreach ($arr as $k => &$v) {
if (is_array($v)) {
if ($k === "properties" && !$v) {
$v = new \stdClass;
} else {
__FUNCTION__($v);
$walk($v);
}
}
}
@ -125,6 +101,6 @@ class Microformats implements \ArrayAccess, \JsonSerializable {
@ -19,8 +19,6 @@ use MensBeam\HTML\Parser\Serializer;
* - `impliedTz` (bool) Whether to allow an implied datetime value to supply an implied timezone to datetimes without a timezone
* - `lang` (bool) Whether to include language information in microformat and rich-text structures
* - `simpleTrim` (bool) Whether to use the traditional "simple" whitespace trimming algorithm rather than the default, more aggressive trimming algorithm
*
* @internal
*/
class Parser {
/** @var array A ranking of prefixes (with 1 being least preferred) to break ties when multiple properties of the same name exist on one element */