search_param.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 2017/3/19
  6. * Time: 下午10:46
  7. */
  8. require_once (BASE_ROOT_PATH . '/helper/history_helper.php');
  9. class search_param
  10. {
  11. private $mKeyWord;
  12. private $mBrandId;
  13. private $mHotId;
  14. public function __construct($param)
  15. {
  16. $this->mKeyWord = '';
  17. $this->mBrandId = 0;
  18. $this->mHotId = 0;
  19. if (!empty(trim($param['keyword']))) {
  20. $keywod = trim(urldecode($param['keyword']));
  21. if ($this->check($keywod)) {
  22. $this->mKeyWord = trim(urldecode($param['keyword']));
  23. $helper = new history_helper();
  24. $helper->add_word($this->mKeyWord);
  25. }
  26. }
  27. if (intval($param['brand_id']) > 0) {
  28. $this->mBrandId = intval($param['brand_id']);
  29. }
  30. if (!empty(trim($param['hot_id']))) {
  31. $this->mHotId = intval($param['hot_id']);
  32. }
  33. }
  34. public function format()
  35. {
  36. if (empty($this->mKeyWord) && $this->mBrandId == 0 && $this->mHotId == 0) {
  37. return false;
  38. } else {
  39. $result = [];
  40. $result['keyword'] = $this->mKeyWord;
  41. $result['brand_id'] = $this->mBrandId;
  42. $result['hot_id'] = $this->mHotId;
  43. return $result;
  44. }
  45. }
  46. public function brand_id()
  47. {
  48. return $this->mBrandId;
  49. }
  50. private function mb_str_split($string)
  51. {
  52. return preg_split('/(?<!^)(?!$)/u', $string);
  53. }
  54. private function check($keyword)
  55. {
  56. $words = [];
  57. foreach ($this->mb_str_split($keyword) as $word) {
  58. if (search\filter::filter($word)) {
  59. $words[] = $word;
  60. }
  61. }
  62. if (empty($words)) {
  63. return false;
  64. } else {
  65. return true;
  66. }
  67. }
  68. }