Skip to content

Commit f5975c8

Browse files
author
Charles THIBAULT
committed
[PHPAPI-31] Add support for retrieving all facets labels: AfsFacetHelper::get_labels
1 parent aaf578a commit f5975c8

File tree

3 files changed

+62
-348
lines changed

3 files changed

+62
-348
lines changed

AFS/SEARCH/TEST/facetHelperTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,35 @@ public function testRetrieveFacetLabel()
5454
$this->assertEquals(AfsFacetType::BOOL_TYPE, $helper->get_type());
5555
$this->assertEquals(AfsFacetLayout::TREE, $helper->get_layout());
5656
$this->assertEquals(false, $helper->is_sticky());
57+
58+
$labels = $helper->get_labels();
59+
$this->assertEquals(array("ES" => "Faceta booleana", "FR" => "Facette booléenne", "Boolean facet"), $labels);
60+
}
61+
62+
public function testRetrieveLabelsWhenNoLabelsExists() {
63+
$input = json_decode('{
64+
"afs:t": "FacetTree",
65+
"node": [
66+
{
67+
"key": "false",
68+
"labels": [
69+
{
70+
"label": "BAD"
71+
}
72+
],
73+
"items": 67
74+
}
75+
],
76+
"layout": "TREE",
77+
"type": "BOOL",
78+
"id": "FOO",
79+
"sticky": "true" }');
80+
81+
$config = new AfsHelperConfiguration();
82+
$helper = new AfsFacetHelper($input, new AfsQuery(), $config);
83+
$labels = $helper->get_labels();
84+
$this->assertEquals(array("FOO"), $labels);
85+
$this->assertEquals("FOO", $helper->get_label());
5786
}
5887

5988
public function testRetrieveStickyness()

AFS/SEARCH/afs_facet_helper.php

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ function is_not_null($value) {
1111
class AfsFacetHelper extends AfsHelperBase
1212
{
1313
private $id = null;
14-
private $label = null;
14+
private $labels = null;
1515
private $layout = null;
1616
private $type = null;
1717
private $sticky = null;
@@ -28,9 +28,9 @@ public function __construct($facet, AfsQuery $query, AfsHelperConfiguration $con
2828
$this->id = $facet->id;
2929
if (property_exists($facet, 'labels') && ! empty($facet->labels)
3030
&& property_exists($facet->labels[0], 'label')) {
31-
$this->label = $facet->labels[0]->label;
31+
$this->labels = $facet->labels;
3232
} else {
33-
$this->label = $this->id;
33+
$this->labels = null;
3434
}
3535
$this->layout = $facet->layout;
3636
$this->type = $facet->type;
@@ -55,7 +55,32 @@ public function __construct($facet, AfsQuery $query, AfsHelperConfiguration $con
5555
*/
5656
public function get_label()
5757
{
58-
return $this->label;
58+
if (!is_null($this->labels)) {
59+
return $this->labels[0]->label;
60+
} else {
61+
return $this->id;
62+
}
63+
}
64+
65+
/** @brief Retrieve all facet labels
66+
*
67+
* @return labels as a string array
68+
*/
69+
public function get_labels() {
70+
$labels = array();
71+
if (!is_null($this->labels)) {
72+
foreach ($this->labels as $label) {
73+
if (property_exists($label, "lang")) {
74+
$labels[$label->lang] = ($label->label);
75+
} else {
76+
$labels[] = ($label->label);
77+
}
78+
}
79+
}
80+
else {
81+
$labels[] = $this->id;
82+
}
83+
return $labels;
5984
}
6085

6186
/** @brief Retrieves facet id.

0 commit comments

Comments
 (0)