diff options
-rw-r--r-- | CHANGELOG.md | 3 | ||||
-rw-r--r-- | src/Configuration.php | 52 | ||||
-rw-r--r-- | test/ConfigurationTest.php | 68 |
3 files changed, 118 insertions, 5 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index fae01f0..fa71696 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,12 +3,15 @@ All notable changes to this project will be documented in this file. ## Unreleased +## [v1.1.1](https://github.com/andreskrey/readability.php/releases/tag/v1.1.1) + - Switched from assertEquals to assertSame on unit testing to avoid weak comparisons. - Added a safe check to avoid sending the DOMDocument as a node when scanning for node ancestors. - Fix issue #45: Small mistake in documentation - Fix issue #46: Added `data-src` as a image source path - Fixed bug when extracting all the image of the article (Was extracting images from the original DOM instead of the parsed one) - Added the `->getDOMDocument()` getter to retrieve the fully parsed DOMDocument +- Merged PR #48 that allows passing an array as configuration (@topotru) ## [v1.1.0](https://github.com/andreskrey/readability.php/releases/tag/v1.1.0) diff --git a/src/Configuration.php b/src/Configuration.php index b7f1f85..951740e 100644 --- a/src/Configuration.php +++ b/src/Configuration.php @@ -17,44 +17,91 @@ class Configuration * @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 = sprintf('set%s', $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 = sprintf('get%s', $key); + if (!is_object($value) && method_exists($this, $getter)) { + $out[$key] = call_user_func([$this, $getter]); + } + } + + return $out; + } + + /** * @return LoggerInterface */ public function getLogger() @@ -298,9 +345,4 @@ class Configuration return $this; } - - /** - * @var bool - */ - protected $summonCthulhu = false; } diff --git a/test/ConfigurationTest.php b/test/ConfigurationTest.php new file mode 100644 index 0000000..fda9227 --- /dev/null +++ b/test/ConfigurationTest.php @@ -0,0 +1,68 @@ +<?php + +namespace andreskrey\Readability\Test; + +use andreskrey\Readability\Configuration; + +/** + * Class ConfigurationTest. + */ +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', + ], + ], + ]; + } +} |