Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ScoreBuilder.php
Go to the documentation of this file.
1 <?php
7 
17 {
21  private $scoreCondition = '';
22 
26  const WEIGHT_FIELD = 'search_weight';
27 
33  public function getScoreAlias()
34  {
35  return 'score';
36  }
37 
43  public function build()
44  {
45  $scoreCondition = $this->scoreCondition;
46  $this->clear();
47  $scoreAlias = $this->getScoreAlias();
48 
49  return "({$scoreCondition}) AS {$scoreAlias}";
50  }
51 
57  public function startQuery()
58  {
59  $this->addPlus();
60  $this->scoreCondition .= '(';
61  }
62 
69  public function endQuery($boost)
70  {
71  if (!empty($this->scoreCondition) && substr($this->scoreCondition, -1) !== '(') {
72  $this->scoreCondition .= ") * {$boost}";
73  } else {
74  $this->scoreCondition .= '0)';
75  }
76  }
77 
85  public function addCondition($score, $useWeights = true)
86  {
87  $this->addPlus();
88  $condition = "{$score}";
89  if ($useWeights) {
90  $condition = "LEAST(($condition), 1000000) * POW(2, " . self::WEIGHT_FIELD . ')';
91  }
92  $this->scoreCondition .= $condition;
93  }
94 
100  private function addPlus()
101  {
102  if (!empty($this->scoreCondition) && substr($this->scoreCondition, -1) !== '(') {
103  $this->scoreCondition .= ' + ';
104  }
105  }
106 
112  private function clear()
113  {
114  $this->scoreCondition = '';
115  }
116 }