Skip to content
Snippets Groups Projects
Commit 035ebbee authored by llaffont's avatar llaffont
Browse files

Consolidation des paniers, meilleur algorithme

parent 01dfdfc9
No related merge requests found
......@@ -41,6 +41,16 @@ class Class_Notice_ClefAlpha {
}
public function getClefComplementTitre() {
return $this->_clef[self::POS_COMPLEMENT_TITRE];
}
public function getClefEditeur() {
return $this->_clef[self::POS_EDITEUR];
}
public function getTypeDoc() {
return isset($this->_clef[self::POS_TYPE_DOC]) ? $this->_clef[self::POS_TYPE_DOC] : '';
}
......@@ -51,13 +61,39 @@ class Class_Notice_ClefAlpha {
}
public function likeKeyWith($positions) {
$cle = [];
for($i=0; $i <=6; $i++) {
$cle []= in_array($i, $positions) ? $this->_clef[$i] : '%';
}
return implode(self::SEPARATOR, $cle);
}
public function numberOfMatchingElements($clef_alpha) {
return count(array_intersect($clef_alpha->_clef, $this->_clef));
}
public function getUniqueNoticeWithSameTitleTomeTypeDoc() {
$search_params = ['where' => 'clef_alpha like "'.$this->getClefTitre().self::SEPARATOR.'%"',
'tome_alpha' => $this->getTome(),
'type_doc' => $this->getTypeDoc()];
if (!$notices = Class_Notice::findAllBy(['where' => 'clef_alpha like "'.$this->_clef[self::POS_TITRE].self::SEPARATOR.'%"']))
return null;
$current_notice = $notices[0];
$max_score = 0;
foreach($notices as $notice) {
$score = (new static($notice->getClefAlpha()))->numberOfMatchingElements($this);
if ($score > $max_score)
$current_notice = $notice;
}
return $current_notice;
}
$current_params = [];
public function getUniqueNoticeWithParams($search_params) {
$current_params = [];
foreach($search_params as $key => $value) {
$current_params[$key] = $value;
$notices = Class_Notice::findAllBy($current_params);
......
......@@ -35,6 +35,14 @@ class Notice_ClefAlphaSusAImprevuTest extends Storm_Test_ModelTestCase {
}
/** @test */
public function numberOfMatchingElementsWithSameTitleAndAuteurShouldReturnTwo() {
$other_clef = new Class_Notice_ClefAlpha('SUSALIMPREVU-LERETOUR-BOUCQF-3-LELOU-2011-3');
$this->assertEquals(2,
$this->_clef->numberOfMatchingElements($other_clef));
}
/** @test */
public function cleTitreShouldBeSUSALIMPREVU() {
$this->assertEquals('SUSALIMPREVU', $this->_clef->getClefTitre());
......@@ -55,28 +63,19 @@ class Notice_ClefAlphaSusAImprevuTest extends Storm_Test_ModelTestCase {
/** @test */
public function withTwoNoticesWithSameTitleTomeTypeDocGetUniqueNoticeShouldAnswerNull() {
$_sus_imprevu2 = Class_Notice::newInstanceWithId(5,
public function withTwoNoticesWithSameTitleShouldReturnOneWithHighestNumberOfMatchingElements() {
$sus_imprevu2 = Class_Notice::newInstanceWithId(5,
['clef_alpha' => 'SUSALIMPREVU--BOUCQF-3-LELOMBARD-2010-1']);
$sus_imprevu3 = Class_Notice::newInstanceWithId(6,
['clef_alpha' => 'SUSALIMPREVU--BOUCQF-3-LELOMBARD-2012-1']);
Storm_Test_ObjectWrapper::onLoaderOfModel('Class_Notice')
->whenCalled('findAllBy')
->with(['where' => 'clef_alpha like "SUSALIMPREVU-%"'])
->answers([$this->_sus_imprevu, $_sus_imprevu2])
->whenCalled('findAllBy')
->with(['where' => 'clef_alpha like "SUSALIMPREVU-%"',
'tome_alpha' => 2])
->answers([$this->_sus_imprevu, $_sus_imprevu2])
->whenCalled('findAllBy')
->with(['where' => 'clef_alpha like "SUSALIMPREVU-%"',
'tome_alpha' => 2,
'type_doc' => 1])
->answers([$this->_sus_imprevu, $_sus_imprevu2]);
->answers([$sus_imprevu2, $sus_imprevu3]);
$this->assertEmpty($this->_clef->getUniqueNoticeWithSameTitleTomeTypeDoc());
$this->assertEquals($sus_imprevu3, $this->_clef->getUniqueNoticeWithSameTitleTomeTypeDoc());
}
}
......
......@@ -115,7 +115,7 @@ class PanierNoticeWithThreeNoticesTest extends ModelTestCase {
->whenCalled('findAllBy')
->with(['where' => 'clef_alpha like "ZORK-%"'])
->answers([null]);
->answers([]);
Storm_Test_ObjectWrapper::onLoaderOfModel('Class_PanierNotice')
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment