summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndres Rey <[email protected]>2018-11-01 23:11:23 +0000
committerAndres Rey <[email protected]>2018-11-01 23:11:23 +0000
commit3a6a923391f540638ec8bae8ed62e75cea2dbea4 (patch)
tree6001acf6c4c1a69a18e8c4401244846a0200a64d
parent1a383195c8fe44ee4d934c06cd441a5a7fbc7bc8 (diff)
Improve metadata searching function
-rw-r--r--src/Readability.php29
1 files changed, 18 insertions, 11 deletions
diff --git a/src/Readability.php b/src/Readability.php
index 5d6c78e..506a478 100644
--- a/src/Readability.php
+++ b/src/Readability.php
@@ -327,14 +327,13 @@ class Readability
// get title
/*
* This is a very convoluted way of extracting the first matching key of the $values array
- * against a set of options. First you define the target keys, flip it, intersect them (discarding the
- * target keys without a match), then reversing it (because array_intersect_key reverses the original array) and
- * finally get the item.
+ * against a set of options.
*
* This could be easily replaced with an ugly set of isset($values['key']) or a bunch of ??s.
* Will probably replace it with ??s after dropping support of PHP5.6
*/
- $this->setTitle(current(array_reverse(array_intersect_key($values, array_flip([
+
+ $key = current(array_intersect_key([
'dc:title',
'dcterm:title',
'og:title',
@@ -342,21 +341,25 @@ class Readability
'weibo:webpage:title',
'title',
'twitter:title'
- ])))));
+ ], array_keys($values)));
+
+ $this->setTitle(isset($values[$key]) ? $values[$key] : null);
if (!$this->getTitle()) {
$this->setTitle($this->getArticleTitle());
}
// get author
- $this->setAuthor(current(array_reverse(array_intersect_key($values, array_flip([
+ $key = current(array_intersect_key([
'dc:creator',
'dcterm:creator',
'author'
- ])))));
+ ], array_keys($values)));
+
+ $this->setAuthor(isset($values[$key]) ? $values[$key] : null);
// get description
- $this->setExcerpt(current(array_reverse(array_intersect_key($values, array_flip([
+ $key = current(array_intersect_key([
'dc:description',
'dcterm:description',
'og:description',
@@ -364,13 +367,17 @@ class Readability
'weibo:webpage:description',
'description',
'twitter:description'
- ])))));
+ ], array_keys($values)));
+
+ $this->setExcerpt(isset($values[$key]) ? $values[$key] : null);
// get main image
- $this->setImage(current(array_reverse(array_intersect_key($values, array_flip([
+ $key = current(array_intersect_key([
'og:image',
'twitter:image'
- ])))));
+ ], array_keys($values)));
+
+ $this->setImage(isset($values[$key]) ? $values[$key] : null);
}
/**