From e2ceb450e96b1a66af597ae0a09792815ecbd28f Mon Sep 17 00:00:00 2001 From: topot Date: Fri, 9 Mar 2018 18:52:57 +0300 Subject: Added: Configuration parameters array constructor injection --- src/Configuration.php | 130 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 79 insertions(+), 51 deletions(-) (limited to 'src') 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; } -- cgit v1.2.3 From a1c1e96e7fead2873b54b2d37e02312ec8e567b3 Mon Sep 17 00:00:00 2001 From: topot Date: Sat, 10 Mar 2018 03:24:38 +0300 Subject: StyleCI diff applied --- src/Configuration.php | 112 ++++++++++++++++++++++++++++---------------------- 1 file changed, 63 insertions(+), 49 deletions(-) (limited to 'src') diff --git a/src/Configuration.php b/src/Configuration.php index ad429a3..50b19e2 100644 --- a/src/Configuration.php +++ b/src/Configuration.php @@ -12,64 +12,65 @@ 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 = []) @@ -81,9 +82,10 @@ class Configuration } } } - + /** - * Returns an array-representation of configuration + * Returns an array-representation of configuration. + * * @return array */ public function toArray() @@ -91,13 +93,14 @@ class Configuration $out = []; foreach ($this as $key => $value) { $getter = "get{$key}"; - if (! is_object($value) && method_exists($this, $getter)) { + if (!is_object($value) && method_exists($this, $getter)) { $out[$key] = call_user_func([$this, $getter]); } } + return $out; } - + /** * @return LoggerInterface */ @@ -110,7 +113,7 @@ class Configuration return $this->logger; } } - + /** * @return int */ @@ -118,18 +121,19 @@ class Configuration { return $this->maxTopCandidates; } - + /** * @param int $maxTopCandidates + * * @return $this */ public function setMaxTopCandidates($maxTopCandidates) { $this->maxTopCandidates = $maxTopCandidates; - + return $this; } - + /** * @return int */ @@ -137,18 +141,19 @@ class Configuration { return $this->wordThreshold; } - + /** * @param int $wordThreshold + * * @return $this */ public function setWordThreshold($wordThreshold) { $this->wordThreshold = $wordThreshold; - + return $this; } - + /** * @return bool */ @@ -156,18 +161,19 @@ class Configuration { return $this->articleByLine; } - + /** * @param bool $articleByLine + * * @return $this */ public function setArticleByLine($articleByLine) { $this->articleByLine = $articleByLine; - + return $this; } - + /** * @return bool */ @@ -175,18 +181,19 @@ class Configuration { return $this->stripUnlikelyCandidates; } - + /** * @param bool $stripUnlikelyCandidates + * * @return $this */ public function setStripUnlikelyCandidates($stripUnlikelyCandidates) { $this->stripUnlikelyCandidates = $stripUnlikelyCandidates; - + return $this; } - + /** * @return bool */ @@ -194,18 +201,19 @@ class Configuration { return $this->cleanConditionally; } - + /** * @param bool $cleanConditionally + * * @return $this */ public function setCleanConditionally($cleanConditionally) { $this->cleanConditionally = $cleanConditionally; - + return $this; } - + /** * @return bool */ @@ -213,18 +221,19 @@ class Configuration { return $this->weightClasses; } - + /** * @param bool $weightClasses + * * @return $this */ public function setWeightClasses($weightClasses) { $this->weightClasses = $weightClasses; - + return $this; } - + /** * @return bool */ @@ -232,18 +241,19 @@ class Configuration { return $this->fixRelativeURLs; } - + /** * @param bool $fixRelativeURLs + * * @return $this */ public function setFixRelativeURLs($fixRelativeURLs) { $this->fixRelativeURLs = $fixRelativeURLs; - + return $this; } - + /** * @return bool */ @@ -251,18 +261,19 @@ class Configuration { return $this->substituteEntities; } - + /** * @param bool $substituteEntities + * * @return $this */ public function setSubstituteEntities($substituteEntities) { $this->substituteEntities = $substituteEntities; - + return $this; } - + /** * @return bool */ @@ -270,18 +281,19 @@ class Configuration { return $this->normalizeEntities; } - + /** * @param bool $normalizeEntities + * * @return $this */ public function setNormalizeEntities($normalizeEntities) { $this->normalizeEntities = $normalizeEntities; - + return $this; } - + /** * @return string */ @@ -289,18 +301,19 @@ class Configuration { return $this->originalURL; } - + /** * @param string $originalURL + * * @return $this */ public function setOriginalURL($originalURL) { $this->originalURL = $originalURL; - + return $this; } - + /** * @return bool */ @@ -308,15 +321,16 @@ class Configuration { return $this->summonCthulhu; } - + /** * @param bool $summonCthulhu + * * @return $this */ public function setSummonCthulhu($summonCthulhu) { $this->summonCthulhu = $summonCthulhu; - + return $this; } } -- cgit v1.2.3 From 3a4988ed07047fd6bce4db077ed4152b6faf58b8 Mon Sep 17 00:00:00 2001 From: topot Date: Mon, 12 Mar 2018 13:35:42 +0300 Subject: removed class doc-block + method-name-builder switched to sprintf --- src/Configuration.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/Configuration.php b/src/Configuration.php index 50b19e2..a0f363f 100644 --- a/src/Configuration.php +++ b/src/Configuration.php @@ -76,7 +76,7 @@ class Configuration public function __construct(array $params = []) { foreach ($params as $key => $value) { - $setter = "set{$key}"; + $setter = sprintf('set%s', $key); if (method_exists($this, $setter)) { call_user_func([$this, $setter], $value); } @@ -92,7 +92,7 @@ class Configuration { $out = []; foreach ($this as $key => $value) { - $getter = "get{$key}"; + $getter = sprintf('get%s', $key); if (!is_object($value) && method_exists($this, $getter)) { $out[$key] = call_user_func([$this, $getter]); } -- cgit v1.2.3