Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementation of boosting rules for Matej9 (fixes #109)
- Loading branch information
1 parent
9f5663f
commit f9ff273
Showing
7 changed files
with
170 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Lmc\Matej\Model\Command; | ||
|
||
use Lmc\Matej\Model\Assertion; | ||
|
||
/** | ||
* Boosting items is a way how to modify results returend by Matej by specifying | ||
* rules to increase items relevance. | ||
*/ | ||
class Boost | ||
{ | ||
/** @var string */ | ||
private $query; | ||
/** @var float */ | ||
private $multiplier; | ||
|
||
private function __construct(string $query, float $multiplier) | ||
{ | ||
$this->setQuery($query); | ||
$this->setMultiplier($multiplier); | ||
} | ||
|
||
/** | ||
* Create boost rule to prioritize items | ||
* | ||
* @return static | ||
*/ | ||
public static function create(string $query, float $multiplier): self | ||
{ | ||
return new static($query, $multiplier); | ||
} | ||
|
||
public function setQuery(string $query): void | ||
{ | ||
$this->query = $query; | ||
} | ||
|
||
public function setMultiplier(float $multiplier): void | ||
{ | ||
Assertion::greaterThan($multiplier, 0); | ||
|
||
$this->multiplier = $multiplier; | ||
} | ||
|
||
public function jsonSerialize(): array | ||
{ | ||
return [ | ||
'query' => $this->query, | ||
'multiplier' => $this->multiplier, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Lmc\Matej\Model\Command; | ||
|
||
use Lmc\Matej\Exception\DomainException; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class BoostTest extends TestCase | ||
{ | ||
/** | ||
* @test | ||
*/ | ||
public function shouldBeJsonSerializable(): void | ||
{ | ||
$boost = Boost::create('valid_to >= NOW()', 2.1); | ||
$this->assertSame( | ||
['query' => 'valid_to >= NOW()', 'multiplier' => 2.1], | ||
$boost->jsonSerialize() | ||
); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function shouldNotAllowMultiplierLessThan0(): void | ||
{ | ||
$this->expectException(DomainException::class); | ||
$this->expectExceptionMessage('Provided "-1" is not greater than "0".'); | ||
|
||
Boost::create('valid_to >= NOW()', -1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters