Configuration.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. <?php
  2. namespace andreskrey\Readability;
  3. use Psr\Log\LoggerAwareTrait;
  4. use Psr\Log\LoggerInterface;
  5. use Psr\Log\NullLogger;
  6. /**
  7. * Class Configuration.
  8. */
  9. class Configuration
  10. {
  11. use LoggerAwareTrait;
  12. /**
  13. * @var int
  14. */
  15. protected $maxTopCandidates = 5;
  16. /**
  17. * @var int
  18. */
  19. protected $wordThreshold = 500;
  20. /**
  21. * @var bool
  22. */
  23. protected $articleByLine = false;
  24. /**
  25. * @var bool
  26. */
  27. protected $stripUnlikelyCandidates = true;
  28. /**
  29. * @var bool
  30. */
  31. protected $cleanConditionally = true;
  32. /**
  33. * @var bool
  34. */
  35. protected $weightClasses = true;
  36. /**
  37. * @var bool
  38. */
  39. protected $fixRelativeURLs = false;
  40. /**
  41. * @var bool
  42. */
  43. protected $substituteEntities = false;
  44. /**
  45. * @var bool
  46. */
  47. protected $normalizeEntities = false;
  48. /**
  49. * @var bool
  50. */
  51. protected $summonCthulhu = false;
  52. /**
  53. * @var string
  54. */
  55. protected $originalURL = 'http://fakehost';
  56. /**
  57. * Configuration constructor.
  58. *
  59. * @param array $params
  60. */
  61. public function __construct(array $params = [])
  62. {
  63. foreach ($params as $key => $value) {
  64. $setter = sprintf('set%s', $key);
  65. if (method_exists($this, $setter)) {
  66. call_user_func([$this, $setter], $value);
  67. }
  68. }
  69. }
  70. /**
  71. * Returns an array-representation of configuration.
  72. *
  73. * @return array
  74. */
  75. public function toArray()
  76. {
  77. $out = [];
  78. foreach ($this as $key => $value) {
  79. $getter = sprintf('get%s', $key);
  80. if (!is_object($value) && method_exists($this, $getter)) {
  81. $out[$key] = call_user_func([$this, $getter]);
  82. }
  83. }
  84. return $out;
  85. }
  86. /**
  87. * @return LoggerInterface
  88. */
  89. public function getLogger()
  90. {
  91. // If no logger has been set, just return a null logger
  92. if ($this->logger === null) {
  93. return new NullLogger();
  94. } else {
  95. return $this->logger;
  96. }
  97. }
  98. /**
  99. * @param LoggerInterface $logger
  100. *
  101. * @return Configuration
  102. */
  103. public function setLogger(LoggerInterface $logger)
  104. {
  105. $this->logger = $logger;
  106. return $this;
  107. }
  108. /**
  109. * @return int
  110. */
  111. public function getMaxTopCandidates()
  112. {
  113. return $this->maxTopCandidates;
  114. }
  115. /**
  116. * @param int $maxTopCandidates
  117. *
  118. * @return $this
  119. */
  120. public function setMaxTopCandidates($maxTopCandidates)
  121. {
  122. $this->maxTopCandidates = $maxTopCandidates;
  123. return $this;
  124. }
  125. /**
  126. * @return int
  127. */
  128. public function getWordThreshold()
  129. {
  130. return $this->wordThreshold;
  131. }
  132. /**
  133. * @param int $wordThreshold
  134. *
  135. * @return $this
  136. */
  137. public function setWordThreshold($wordThreshold)
  138. {
  139. $this->wordThreshold = $wordThreshold;
  140. return $this;
  141. }
  142. /**
  143. * @return bool
  144. */
  145. public function getArticleByLine()
  146. {
  147. return $this->articleByLine;
  148. }
  149. /**
  150. * @param bool $articleByLine
  151. *
  152. * @return $this
  153. */
  154. public function setArticleByLine($articleByLine)
  155. {
  156. $this->articleByLine = $articleByLine;
  157. return $this;
  158. }
  159. /**
  160. * @return bool
  161. */
  162. public function getStripUnlikelyCandidates()
  163. {
  164. return $this->stripUnlikelyCandidates;
  165. }
  166. /**
  167. * @param bool $stripUnlikelyCandidates
  168. *
  169. * @return $this
  170. */
  171. public function setStripUnlikelyCandidates($stripUnlikelyCandidates)
  172. {
  173. $this->stripUnlikelyCandidates = $stripUnlikelyCandidates;
  174. return $this;
  175. }
  176. /**
  177. * @return bool
  178. */
  179. public function getCleanConditionally()
  180. {
  181. return $this->cleanConditionally;
  182. }
  183. /**
  184. * @param bool $cleanConditionally
  185. *
  186. * @return $this
  187. */
  188. public function setCleanConditionally($cleanConditionally)
  189. {
  190. $this->cleanConditionally = $cleanConditionally;
  191. return $this;
  192. }
  193. /**
  194. * @return bool
  195. */
  196. public function getWeightClasses()
  197. {
  198. return $this->weightClasses;
  199. }
  200. /**
  201. * @param bool $weightClasses
  202. *
  203. * @return $this
  204. */
  205. public function setWeightClasses($weightClasses)
  206. {
  207. $this->weightClasses = $weightClasses;
  208. return $this;
  209. }
  210. /**
  211. * @return bool
  212. */
  213. public function getFixRelativeURLs()
  214. {
  215. return $this->fixRelativeURLs;
  216. }
  217. /**
  218. * @param bool $fixRelativeURLs
  219. *
  220. * @return $this
  221. */
  222. public function setFixRelativeURLs($fixRelativeURLs)
  223. {
  224. $this->fixRelativeURLs = $fixRelativeURLs;
  225. return $this;
  226. }
  227. /**
  228. * @return bool
  229. */
  230. public function getSubstituteEntities()
  231. {
  232. return $this->substituteEntities;
  233. }
  234. /**
  235. * @param bool $substituteEntities
  236. *
  237. * @return $this
  238. */
  239. public function setSubstituteEntities($substituteEntities)
  240. {
  241. $this->substituteEntities = $substituteEntities;
  242. return $this;
  243. }
  244. /**
  245. * @return bool
  246. */
  247. public function getNormalizeEntities()
  248. {
  249. return $this->normalizeEntities;
  250. }
  251. /**
  252. * @param bool $normalizeEntities
  253. *
  254. * @return $this
  255. */
  256. public function setNormalizeEntities($normalizeEntities)
  257. {
  258. $this->normalizeEntities = $normalizeEntities;
  259. return $this;
  260. }
  261. /**
  262. * @return string
  263. */
  264. public function getOriginalURL()
  265. {
  266. return $this->originalURL;
  267. }
  268. /**
  269. * @param string $originalURL
  270. *
  271. * @return $this
  272. */
  273. public function setOriginalURL($originalURL)
  274. {
  275. $this->originalURL = $originalURL;
  276. return $this;
  277. }
  278. /**
  279. * @return bool
  280. */
  281. public function getSummonCthulhu()
  282. {
  283. return $this->summonCthulhu;
  284. }
  285. /**
  286. * @param bool $summonCthulhu
  287. *
  288. * @return $this
  289. */
  290. public function setSummonCthulhu($summonCthulhu)
  291. {
  292. $this->summonCthulhu = $summonCthulhu;
  293. return $this;
  294. }
  295. }