summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md16
-rw-r--r--src/Configuration.php130
-rw-r--r--test/ConfigurationTest.php73
3 files changed, 165 insertions, 54 deletions
diff --git a/README.md b/README.md
index 5b19de6..34441b9 100644
--- a/README.md
+++ b/README.md
@@ -68,11 +68,21 @@ You can change the behaviour of Readability via the Configuration object. For ex
```php
$configuration = new Configuration();
-$configuration->setFixRelativeURLs(true)
+$configuration
+ ->setFixRelativeURLs(true)
->setOriginalURL('http://my.newspaper.url/article/something-interesting-to-read.html');
```
+Also you can pass an array of configuration parameters to the constructor:
+```php
+$configuration = new Configuration([
+ 'fixRelativeURLs' => true,
+ 'originalURL' => 'http://my.newspaper.url/article/something-interesting-to-read.html',
+ // other parameters ... listing below
+]);
+```
+
-Then you pass this Configuration object to Readability. The following options are available. Remember to prepend `set` when calling them.
+Then you pass this Configuration object to Readability. The following options are available. Remember to prepend `set` when calling them using native setters.
- **MaxTopCandidates**: default value `5`, max amount of top level candidates.
- **WordThreshold**: default value `500`, minimum amount of characters to consider that the article was parsed successful.
@@ -173,4 +183,4 @@ Based on Arc90's readability.js (1.7.1) script available at: http://code.google.
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
- limitations under the License. \ No newline at end of file
+ limitations under the License.
diff --git a/src/Configuration.php b/src/Configuration.php
index de3d16a..ad429a3 100644
--- a/src/Configuration.php
+++ b/src/Configuration.php
@@ -12,48 +12,92 @@ use Psr\Log\NullLogger;
class Configuration
{
use LoggerAwareTrait;
-
+
/**
* @var int
*/
protected $maxTopCandidates = 5;
+
/**
* @var int
*/
protected $wordThreshold = 500;
+
/**
* @var bool
*/
protected $articleByLine = false;
+
/**
* @var bool
*/
protected $stripUnlikelyCandidates = true;
+
/**
* @var bool
*/
protected $cleanConditionally = true;
+
/**
* @var bool
*/
protected $weightClasses = true;
+
/**
* @var bool
*/
protected $fixRelativeURLs = false;
+
/**
* @var bool
*/
protected $substituteEntities = false;
+
/**
* @var bool
*/
protected $normalizeEntities = false;
+
+ /**
+ * @var bool
+ */
+ protected $summonCthulhu = false;
+
/**
* @var string
*/
protected $originalURL = 'http://fakehost';
-
+
+ /**
+ * Configuration constructor.
+ * @param array $params
+ */
+ public function __construct(array $params = [])
+ {
+ foreach ($params as $key => $value) {
+ $setter = "set{$key}";
+ if (method_exists($this, $setter)) {
+ call_user_func([$this, $setter], $value);
+ }
+ }
+ }
+
+ /**
+ * Returns an array-representation of configuration
+ * @return array
+ */
+ public function toArray()
+ {
+ $out = [];
+ foreach ($this as $key => $value) {
+ $getter = "get{$key}";
+ if (! is_object($value) && method_exists($this, $getter)) {
+ $out[$key] = call_user_func([$this, $getter]);
+ }
+ }
+ return $out;
+ }
+
/**
* @return LoggerInterface
*/
@@ -66,7 +110,7 @@ class Configuration
return $this->logger;
}
}
-
+
/**
* @return int
*/
@@ -74,19 +118,18 @@ class Configuration
{
return $this->maxTopCandidates;
}
-
+
/**
* @param int $maxTopCandidates
- *
* @return $this
*/
public function setMaxTopCandidates($maxTopCandidates)
{
$this->maxTopCandidates = $maxTopCandidates;
-
+
return $this;
}
-
+
/**
* @return int
*/
@@ -94,19 +137,18 @@ class Configuration
{
return $this->wordThreshold;
}
-
+
/**
* @param int $wordThreshold
- *
* @return $this
*/
public function setWordThreshold($wordThreshold)
{
$this->wordThreshold = $wordThreshold;
-
+
return $this;
}
-
+
/**
* @return bool
*/
@@ -114,19 +156,18 @@ class Configuration
{
return $this->articleByLine;
}
-
+
/**
* @param bool $articleByLine
- *
* @return $this
*/
public function setArticleByLine($articleByLine)
{
$this->articleByLine = $articleByLine;
-
+
return $this;
}
-
+
/**
* @return bool
*/
@@ -134,19 +175,18 @@ class Configuration
{
return $this->stripUnlikelyCandidates;
}
-
+
/**
* @param bool $stripUnlikelyCandidates
- *
* @return $this
*/
public function setStripUnlikelyCandidates($stripUnlikelyCandidates)
{
$this->stripUnlikelyCandidates = $stripUnlikelyCandidates;
-
+
return $this;
}
-
+
/**
* @return bool
*/
@@ -154,19 +194,18 @@ class Configuration
{
return $this->cleanConditionally;
}
-
+
/**
* @param bool $cleanConditionally
- *
* @return $this
*/
public function setCleanConditionally($cleanConditionally)
{
$this->cleanConditionally = $cleanConditionally;
-
+
return $this;
}
-
+
/**
* @return bool
*/
@@ -174,19 +213,18 @@ class Configuration
{
return $this->weightClasses;
}
-
+
/**
* @param bool $weightClasses
- *
* @return $this
*/
public function setWeightClasses($weightClasses)
{
$this->weightClasses = $weightClasses;
-
+
return $this;
}
-
+
/**
* @return bool
*/
@@ -194,19 +232,18 @@ class Configuration
{
return $this->fixRelativeURLs;
}
-
+
/**
* @param bool $fixRelativeURLs
- *
* @return $this
*/
public function setFixRelativeURLs($fixRelativeURLs)
{
$this->fixRelativeURLs = $fixRelativeURLs;
-
+
return $this;
}
-
+
/**
* @return bool
*/
@@ -214,19 +251,18 @@ class Configuration
{
return $this->substituteEntities;
}
-
+
/**
* @param bool $substituteEntities
- *
* @return $this
*/
public function setSubstituteEntities($substituteEntities)
{
$this->substituteEntities = $substituteEntities;
-
+
return $this;
}
-
+
/**
* @return bool
*/
@@ -234,19 +270,18 @@ class Configuration
{
return $this->normalizeEntities;
}
-
+
/**
* @param bool $normalizeEntities
- *
* @return $this
*/
public function setNormalizeEntities($normalizeEntities)
{
$this->normalizeEntities = $normalizeEntities;
-
+
return $this;
}
-
+
/**
* @return string
*/
@@ -254,19 +289,18 @@ class Configuration
{
return $this->originalURL;
}
-
+
/**
* @param string $originalURL
- *
* @return $this
*/
public function setOriginalURL($originalURL)
{
$this->originalURL = $originalURL;
-
+
return $this;
}
-
+
/**
* @return bool
*/
@@ -274,21 +308,15 @@ class Configuration
{
return $this->summonCthulhu;
}
-
+
/**
* @param bool $summonCthulhu
- *
* @return $this
*/
public function setSummonCthulhu($summonCthulhu)
{
$this->summonCthulhu = $summonCthulhu;
-
+
return $this;
}
-
- /**
- * @var bool
- */
- protected $summonCthulhu = false;
}
diff --git a/test/ConfigurationTest.php b/test/ConfigurationTest.php
new file mode 100644
index 0000000..7ee8ed2
--- /dev/null
+++ b/test/ConfigurationTest.php
@@ -0,0 +1,73 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: topot
+ * Date: 09.03.2018
+ * Time: 16:26
+ */
+
+namespace andreskrey\Readability\Test;
+
+use andreskrey\Readability\Configuration;
+
+/**
+ * Class ConfigurationTest
+ * @package andreskrey\Readability\Test
+ */
+class ConfigurationTest extends \PHPUnit_Framework_TestCase
+{
+ /**
+ * @dataProvider getParams
+ * @param array $params
+ */
+ public function testConfigurationConstructorSetsParameters(array $params)
+ {
+ $config = new Configuration($params);
+ $this->doEqualsAsserts($config, $params);
+ }
+
+ /**
+ * @dataProvider getParams
+ * @param array $params
+ */
+ public function testInvalidParameterIsNotInConfig(array $params)
+ {
+ $config = new Configuration($params);
+ $this->assertArrayNotHasKey('invalidParameter', $config->toArray(), 'Invalid param key is not present in config');
+ }
+
+ /**
+ * @param Configuration $config
+ * @param array $options
+ */
+ private function doEqualsAsserts(Configuration $config, array $options)
+ {
+ // just part of params, it's enough
+ $this->assertEquals($options['originalURL'], $config->getOriginalURL());
+ $this->assertEquals($options['fixRelativeURLs'], $config->getFixRelativeURLs());
+ $this->assertEquals($options['articleByLine'], $config->getArticleByLine());
+ $this->assertEquals($options['maxTopCandidates'], $config->getMaxTopCandidates());
+ $this->assertEquals($options['stripUnlikelyCandidates'], $config->getStripUnlikelyCandidates());
+ $this->assertEquals($options['substituteEntities'], $config->getSubstituteEntities());
+ }
+
+ /**
+ * @return array
+ */
+ public function getParams()
+ {
+ return [
+ [
+ [
+ 'originalURL' => 'my.original.url',
+ 'fixRelativeURLs' => true,
+ 'articleByLine' => true,
+ 'maxTopCandidates' => 3,
+ 'stripUnlikelyCandidates' => false,
+ 'substituteEntities' => true,
+ 'invalidParameter' => 'invalidParameterValue',
+ ],
+ ],
+ ];
+ }
+}