diff options
author | Andres Rey <[email protected]> | 2016-11-04 18:59:57 +0000 |
---|---|---|
committer | Andres Rey <[email protected]> | 2016-11-04 18:59:57 +0000 |
commit | 891568a88b24b82d1388c6fe73b3722980bfc1d7 (patch) | |
tree | f533e1efa24055977990e704763ba0e39d2f0772 /src/Readability.php | |
parent | 9df514b344b6cb842e1976b5b90ea4d9d80a7713 (diff) |
Fixed score setting and retrieving
Diffstat (limited to 'src/Readability.php')
-rw-r--r-- | src/Readability.php | 30 |
1 files changed, 27 insertions, 3 deletions
diff --git a/src/Readability.php b/src/Readability.php index a9c1592..39e1a28 100644 --- a/src/Readability.php +++ b/src/Readability.php @@ -38,6 +38,22 @@ class Readability extends Element implements ReadabilityInterface public function __construct(\DOMNode $node) { parent::__construct($node); + + if (get_class($node) !== 'DOMText') { + /* + * Restore the score if the object has been already scored. + * + * And if must be added before calling the getAttribute function, because if we reacht eh DOMDocument + * by geting the node parents we'll get a undefined function fatal error + */ + $score = 0; + + if (get_class($node) !== 'DOMDocument') { + $score = $node->getAttribute('readability'); + } + + $this->setContentScore(($score) ? $score : 0); + } } /** @@ -240,10 +256,18 @@ class Readability extends Element implements ReadabilityInterface */ public function setContentScore($score) { - // To prevent the -0 value - $this->contentScore = ($score === (double)-0) ? 0 : $score; + if (get_class($this->node) !== 'DOMDocument') { - return $this->contentScore; + // To prevent the -0 value + $this->contentScore = ($score === (double)-0) ? 0 : $score; + + // Set score in an attribute of the tag to prevent losing it while creating new Readability objects. + $this->node->setAttribute('readability', $this->contentScore); + + return $this->contentScore; + } + + return 0; } /** |