Configuration.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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 $charThreshold = 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. }
  95. return $this->logger;
  96. }
  97. /**
  98. * @param LoggerInterface $logger
  99. *
  100. * @return Configuration
  101. */
  102. public function setLogger(LoggerInterface $logger)
  103. {
  104. $this->logger = $logger;
  105. return $this;
  106. }
  107. /**
  108. * @return int
  109. */
  110. public function getMaxTopCandidates()
  111. {
  112. return $this->maxTopCandidates;
  113. }
  114. /**
  115. * @param int $maxTopCandidates
  116. *
  117. * @return $this
  118. */
  119. public function setMaxTopCandidates($maxTopCandidates)
  120. {
  121. $this->maxTopCandidates = $maxTopCandidates;
  122. return $this;
  123. }
  124. /**
  125. * @return int
  126. */
  127. public function getCharThreshold()
  128. {
  129. return $this->charThreshold;
  130. }
  131. /**
  132. * @param int $charThreshold
  133. *
  134. * @return $this
  135. */
  136. public function setCharThreshold($charThreshold)
  137. {
  138. $this->charThreshold = $charThreshold;
  139. return $this;
  140. }
  141. /**
  142. * @deprecated Use getCharThreshold. Will be removed in version 2.0
  143. *
  144. * @return int
  145. */
  146. public function getWordThreshold()
  147. {
  148. @trigger_error('getWordThreshold was replaced with getCharThreshold and will be removed in version 3.0', E_USER_DEPRECATED);
  149. return $this->charThreshold;
  150. }
  151. /**
  152. * @param int $charThreshold
  153. *
  154. * @return $this
  155. */
  156. public function setWordThreshold($charThreshold)
  157. {
  158. @trigger_error('setWordThreshold was replaced with setCharThreshold and will be removed in version 3.0', E_USER_DEPRECATED);
  159. $this->charThreshold = $charThreshold;
  160. return $this;
  161. }
  162. /**
  163. * @return bool
  164. */
  165. public function getArticleByLine()
  166. {
  167. return $this->articleByLine;
  168. }
  169. /**
  170. * @param bool $articleByLine
  171. *
  172. * @return $this
  173. */
  174. public function setArticleByLine($articleByLine)
  175. {
  176. $this->articleByLine = $articleByLine;
  177. return $this;
  178. }
  179. /**
  180. * @return bool
  181. */
  182. public function getStripUnlikelyCandidates()
  183. {
  184. return $this->stripUnlikelyCandidates;
  185. }
  186. /**
  187. * @param bool $stripUnlikelyCandidates
  188. *
  189. * @return $this
  190. */
  191. public function setStripUnlikelyCandidates($stripUnlikelyCandidates)
  192. {
  193. $this->stripUnlikelyCandidates = $stripUnlikelyCandidates;
  194. return $this;
  195. }
  196. /**
  197. * @return bool
  198. */
  199. public function getCleanConditionally()
  200. {
  201. return $this->cleanConditionally;
  202. }
  203. /**
  204. * @param bool $cleanConditionally
  205. *
  206. * @return $this
  207. */
  208. public function setCleanConditionally($cleanConditionally)
  209. {
  210. $this->cleanConditionally = $cleanConditionally;
  211. return $this;
  212. }
  213. /**
  214. * @return bool
  215. */
  216. public function getWeightClasses()
  217. {
  218. return $this->weightClasses;
  219. }
  220. /**
  221. * @param bool $weightClasses
  222. *
  223. * @return $this
  224. */
  225. public function setWeightClasses($weightClasses)
  226. {
  227. $this->weightClasses = $weightClasses;
  228. return $this;
  229. }
  230. /**
  231. * @return bool
  232. */
  233. public function getFixRelativeURLs()
  234. {
  235. return $this->fixRelativeURLs;
  236. }
  237. /**
  238. * @param bool $fixRelativeURLs
  239. *
  240. * @return $this
  241. */
  242. public function setFixRelativeURLs($fixRelativeURLs)
  243. {
  244. $this->fixRelativeURLs = $fixRelativeURLs;
  245. return $this;
  246. }
  247. /**
  248. * @return bool
  249. */
  250. public function getSubstituteEntities()
  251. {
  252. return $this->substituteEntities;
  253. }
  254. /**
  255. * @param bool $substituteEntities
  256. *
  257. * @return $this
  258. */
  259. public function setSubstituteEntities($substituteEntities)
  260. {
  261. $this->substituteEntities = $substituteEntities;
  262. return $this;
  263. }
  264. /**
  265. * @return bool
  266. */
  267. public function getNormalizeEntities()
  268. {
  269. return $this->normalizeEntities;
  270. }
  271. /**
  272. * @param bool $normalizeEntities
  273. *
  274. * @return $this
  275. */
  276. public function setNormalizeEntities($normalizeEntities)
  277. {
  278. $this->normalizeEntities = $normalizeEntities;
  279. return $this;
  280. }
  281. /**
  282. * @return string
  283. */
  284. public function getOriginalURL()
  285. {
  286. return $this->originalURL;
  287. }
  288. /**
  289. * @param string $originalURL
  290. *
  291. * @return $this
  292. */
  293. public function setOriginalURL($originalURL)
  294. {
  295. $this->originalURL = $originalURL;
  296. return $this;
  297. }
  298. /**
  299. * @return bool
  300. */
  301. public function getSummonCthulhu()
  302. {
  303. return $this->summonCthulhu;
  304. }
  305. /**
  306. * @param bool $summonCthulhu
  307. *
  308. * @return $this
  309. */
  310. public function setSummonCthulhu($summonCthulhu)
  311. {
  312. $this->summonCthulhu = $summonCthulhu;
  313. return $this;
  314. }
  315. }