Browse Source

Added img+alt parsing logic for compatibility with php-mf2 0.5

* Added isImgAlt static method to Formats\Mf2
* Used it in collectArrayURLValues and getPlaintext to handle cases where
  img+alt structures are encountered

Additionally disabled reporting of deprecation warnings while testing for
a cleaner output. All tests are passing.
pull/111/head
Barnaby Walters 2 years ago
parent
commit
676613fb16
2 changed files with 26 additions and 1 deletions
  1. +22
    -1
      lib/XRay/Formats/Mf2.php
  2. +4
    -0
      tests/bootstrap.php

+ 22
- 1
lib/XRay/Formats/Mf2.php View File

@ -356,7 +356,6 @@ class Mf2 extends Format {
private static function collectArrayURLValues($properties, $item, &$data, &$refs, &$http) {
$keys = [];
foreach($properties as $p) {
if(array_key_exists($p, $item['properties'])) {
foreach($item['properties'][$p] as $v) {
@ -365,6 +364,15 @@ class Mf2 extends Format {
$data[$p][] = $v;
$keys[] = $p;
}
elseif(self::isImgAlt($v)) {
// For the moment, disregard the alt value and output a string for compatibility with current consuming code.
$imgURL = $v['value'];
if (is_string($imgURL) and self::isURL($imgURL)) {
if(!array_key_exists($p, $data)) $data[$p] = [];
$data[$p][] = $imgURL;
$keys[] = $p;
}
}
elseif(self::isMicroformat($v) && ($u=self::getPlaintext($v, 'url')) && self::isURL($u)) {
if(!array_key_exists($p, $data)) $data[$p] = [];
$data[$p][] = $u;
@ -881,6 +889,13 @@ class Mf2 extends Format {
return false;
}
private static function isImgAlt($mf) {
return is_array($mf)
and !self::hasNumericKeys($mf)
and array_key_exists('value', $mf)
and array_key_exists('alt', $mf);
}
private static function isMicroformat($mf) {
return is_array($mf)
and !self::hasNumericKeys($mf)
@ -909,6 +924,12 @@ class Mf2 extends Format {
$value = $mf2['properties'][$k][0];
if(is_string($value)) {
return $value;
}
elseif(self::isImgAlt($value)) {
// For back-compatibility, assume that the consuming code wants the URL value.
if (is_string($value['value'])) {
return $value['value'];
}
} elseif(self::isMicroformat($value) && array_key_exists('value', $value)) {
return $value['value'];
}

+ 4
- 0
tests/bootstrap.php View File

@ -2,6 +2,10 @@
const TESTING = true;
require __DIR__ . '/../vendor/autoload.php';
// TODO: fix the many things causing deprecation warnings!
// For the moment, report all errors except for deprecation warnings during testing.
error_reporting(E_ALL ^ E_DEPRECATED);
// Load config file if present, otherwise use default
if(file_exists(dirname(__FILE__).'/../config.php')) {
include dirname(__FILE__).'/../config.php';

Loading…
Cancel
Save