summaryrefslogtreecommitdiff
path: root/src/NodeClass
diff options
context:
space:
mode:
authorAndres Rey <[email protected]>2017-11-28 20:47:09 +0000
committerAndres Rey <[email protected]>2017-11-28 20:47:09 +0000
commitb06bbf1374f09db17504d17b59e714a5be21df6f (patch)
tree0d31d72932fd0b67763baf44677ec1f63afb361a /src/NodeClass
parentf09e346ecb9f6977161ec6196bc796bab5ac463e (diff)
Add initialization function and property
Diffstat (limited to 'src/NodeClass')
-rw-r--r--src/NodeClass/NodeClassTrait.php79
1 files changed, 57 insertions, 22 deletions
diff --git a/src/NodeClass/NodeClassTrait.php b/src/NodeClass/NodeClassTrait.php
index b2c0fcc..467b170 100644
--- a/src/NodeClass/NodeClassTrait.php
+++ b/src/NodeClass/NodeClassTrait.php
@@ -10,7 +10,12 @@ trait NodeClassTrait
/**
* @var int
*/
- protected $contentScore = 0;
+ public $contentScore = 0;
+
+ /**
+ * @var bool
+ */
+ public $initialized = false;
/**
* @var array
@@ -21,6 +26,57 @@ trait NodeClassTrait
];
/**
+ * Initializer. Calculates the current score of the node and returns a full Readability object.
+ *
+ * @return self
+ */
+ public function initializeNode()
+ {
+ if (!$this->initialized) {
+ $contentScore = 0;
+
+ switch ($this->nodeName) {
+ case 'div':
+ $contentScore += 5;
+ break;
+
+ case 'pre':
+ case 'td':
+ case 'blockquote':
+ $contentScore += 3;
+ break;
+
+ case 'address':
+ case 'ol':
+ case 'ul':
+ case 'dl':
+ case 'dd':
+ case 'dt':
+ case 'li':
+ case 'form':
+ $contentScore -= 3;
+ break;
+
+ case 'h1':
+ case 'h2':
+ case 'h3':
+ case 'h4':
+ case 'h5':
+ case 'h6':
+ case 'th':
+ $contentScore -= 5;
+ break;
+ }
+
+ $this->contentScore = $contentScore + $this->getClassWeight();
+
+ $this->initialized = true;
+ }
+
+ return $this;
+ }
+
+ /**
* Placeholder for getAttribute method. Some nodes have the getAttribute method, some don't.
*
* @param $attributeName string Attribute to retrieve
@@ -175,27 +231,6 @@ trait NodeClassTrait
}
/**
- * Returns the current score of the Readability object.
- *
- * @return int
- */
- public function getContentScore()
- {
- return $this->contentScore;
- }
-
- /**
- * Returns the current score of the Readability object.
- *
- * @param int $score
- */
- public function setContentScore($score)
- {
- $this->contentScore = $score;
- }
-
-
- /**
* Returns the full text of the node.
*
* @param bool $normalize Normalize white space?