summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndres Rey <[email protected]>2018-03-12 11:13:40 +0000
committerGitHub <[email protected]>2018-03-12 11:13:40 +0000
commit081f66134e464082de0299494e0adb4160cb536a (patch)
tree92342ccd38880c1e77f0290d36a0c0c321fbc9f7
parenta2db9880e856bcc6c2f49438c55d62749ed68a3b (diff)
parent3a4988ed07047fd6bce4db077ed4152b6faf58b8 (diff)
Merge pull request #48 from topotru/configuration-constructor
Added: Configuration parameters array constructor injection
-rw-r--r--README.md16
-rw-r--r--src/Configuration.php52
-rw-r--r--test/ConfigurationTest.php68
3 files changed, 128 insertions, 8 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..a0f363f 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()
@@ -286,9 +333,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',
+ ],
+ ],
+ ];
+ }
+}