Skip to content

Commit b276ee9

Browse files
committed
NTR - add FacetCategory
1 parent cd4621c commit b276ee9

File tree

7 files changed

+116
-8
lines changed

7 files changed

+116
-8
lines changed

examples/index.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,47 @@
9191
echo '</label>';
9292
echo '</div>';
9393
}
94+
} elseif ($facet->getType() === 'category') {
95+
$categories = $facet->getCategories();
96+
foreach ($categories as $categoryName => $categoryValues) {
97+
$isChecked = $categoryValues['checked'] ?? false;
98+
$count = $categoryValues['count'] ?? 0;
99+
$value = $categoryName;
100+
$childs = $categoryValues['childs'] ?? [];
101+
echo '<div class="form-check">';
102+
echo '<input class="form-check-input" type="checkbox" name="f[' . $facet->getFieldName() . '][]" value="' . $value . '" id="' . $facet->getFieldName() . '_' . $value . '" ' . ($isChecked ? 'checked' : '') . ' onchange="this.form.submit()">';
103+
echo '<label class="form-check-label" for="' . $facet->getFieldName() . '_' . $value . '">';
104+
echo $value . ' ' . $facet->getFieldUnit() . ' (' . $count . ')';
105+
echo '</label>';
106+
echo '</div>';
107+
if ($isChecked === true && count($childs) > 0) {
108+
foreach ($childs as $childName => $childValues) {
109+
$isChecked1 = $childValues['checked'] ?? false;
110+
$count1 = $childValues['count'] ?? 0;
111+
$value1 = $childName;
112+
$childs1 = $childValues['childs'] ?? [];
113+
echo '<div class="form-check">';
114+
echo '<input class="form-check-input" type="checkbox" name="f[' . $facet->getFieldName() . '][]" value="' . $value . ' > ' . $value1 . '" id="' . $facet->getFieldName() . '_' . $value1 . '" ' . ($isChecked1 ? 'checked' : '') . ' onchange="this.form.submit()">';
115+
echo '<label class="form-check-label" for="' . $facet->getFieldName() . '_' . $value . '">';
116+
echo $value1 . ' ' . $facet->getFieldUnit() . ' (' . $count1 . ')';
117+
echo '</label>';
118+
echo '</div>';
119+
if ($isChecked1 === true && count($childs1) > 0) {
120+
foreach ($childs1 as $childName1 => $childValues1) {
121+
$isChecked2 = $childValues1['checked'] ?? false;
122+
$count2 = $childValues1['count'] ?? 0;
123+
$value2 = $childName1;
124+
echo '<div class="form-check">';
125+
echo '<input class="form-check-input" type="checkbox" name="f[' . $facet->getFieldName() . '][]" value="' . $value . ' > ' . $value1 . ' > ' . $value2 . '" id="' . $facet->getFieldName() . '_' . $value . '" ' . ($isChecked2 ? 'checked' : '') . ' onchange="this.form.submit()">';
126+
echo '<label class="form-check-label" for="' . $facet->getFieldName() . '_' . $value . '">';
127+
echo $value2 . ' ' . $facet->getFieldUnit() . ' (' . $count2 . ')';
128+
echo '</label>';
129+
echo '</div>';
130+
}
131+
}
132+
}
133+
}
134+
}
94135
}
95136
}
96137
?>

src/Shop/BrowseResponse.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,27 @@ public function getHits(): array
2121
/**
2222
* @return FacetDto[]
2323
*/
24-
public function getFacets(): array
24+
public function getFacets(string $categoryField = '_PRODUCT.categories'): array
2525
{
2626
$result = [];
2727
$facets = $this->getBody()['facet_counts'];
2828
foreach ($facets as $facet) {
29+
if ($facet['field_name'] === $categoryField) {
30+
$facet['type'] = 'category';
31+
}
2932
switch ($facet['type']) {
33+
case 'category':
34+
$result[] = new FacetCategoryDto($facet, $this->searchStruct->getFilters());
35+
break;
3036
case 'range':
3137
$result[] = new FacetRangeDto($facet, $this->searchStruct->getFilters());
3238
break;
3339
case 'select':
3440
$result[] = new FacetSelectDto($facet, $this->searchStruct->getFilters());
3541
break;
36-
// @codeCoverageIgnoreStart
37-
default:
42+
case 'rating':
3843
$result[] = new FacetRatingDto($facet, $this->searchStruct->getFilters());
39-
// @codeCoverageIgnoreEnd
44+
break;
4045
}
4146
}
4247

src/Shop/FacetCategoryDto.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace BatteryIncludedSdk\Shop;
6+
7+
class FacetCategoryDto extends FacetDto
8+
{
9+
private mixed $categories;
10+
11+
public function __construct(array $data, protected array $appliedFilterValues = [])
12+
{
13+
parent::__construct($data, $appliedFilterValues);
14+
15+
$this->categories = $this->buildCategoryTree($data['counts'] ?? []);
16+
}
17+
18+
private function buildCategoryTree(array $items, $separator = '>')
19+
{
20+
$tree = [];
21+
foreach ($this->appliedFilterValues[$this->fieldName] ?? [] as $filter) {
22+
$appliedFilter = array_map('trim', explode($separator, $filter));
23+
$filterPrefix = '';
24+
foreach ($appliedFilter as $filterPart) {
25+
foreach ($items as $key => $item) {
26+
if ($item['value'] === $filterPrefix . $filterPart) {
27+
$items[$key]['checked'] = true;
28+
$filterPrefix .= $filterPart . ' ' . $separator . ' ';
29+
}
30+
}
31+
}
32+
}
33+
34+
foreach ($items as $item) {
35+
$parts = array_map('trim', explode($separator, $item['value']));
36+
$count = $item['count'];
37+
$checked = $item['checked'] ?? false;
38+
39+
$current = &$tree;
40+
foreach ($parts as $part) {
41+
/* @phpstan-ignore isset.offset */
42+
if (!isset($current['childs'][$part])) {
43+
$current['childs'][$part] = [];
44+
}
45+
$current = &$current['childs'][$part];
46+
}
47+
48+
$current['count'] = $count;
49+
$current['checked'] = $checked;
50+
}
51+
52+
/* @phpstan-ignore nullCoalesce.offset */
53+
return $tree['childs'] ?? [];
54+
}
55+
56+
public function getCategories(): array
57+
{
58+
return $this->categories;
59+
}
60+
}

src/Shop/FacetRangeDto.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
class FacetRangeDto extends FacetDto
88
{
9-
private float $min = 0.000;
9+
private float $min;
1010

11-
private float $max = 0.000;
11+
private float $max;
1212

1313
public function __construct(array $data, protected array $appliedFilterValues = [])
1414
{

src/Shop/FacetRatingDto.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function __construct(array $data, protected array $appliedFilterValues =
1919
}
2020
}
2121

22-
public function getRatings()
22+
public function getRatings(): array
2323
{
2424
return $this->ratings;
2525
}

src/Shop/FacetValueDto.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class FacetValueDto
1010

1111
private string $name;
1212

13-
private int $count = 0;
13+
private int $count;
1414

1515
public function __construct(array $data, private array $appliedFilterValues = [])
1616
{

tests/Shop/BrowseServiceTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use BatteryIncludedSdk\Shop\BrowseResponse;
1616
use BatteryIncludedSdk\Shop\BrowseSearchStruct;
1717
use BatteryIncludedSdk\Shop\BrowseService;
18+
use BatteryIncludedSdk\Shop\FacetCategoryDto;
1819
use BatteryIncludedSdk\Shop\FacetDto;
1920
use BatteryIncludedSdk\Shop\FacetRangeDto;
2021
use BatteryIncludedSdk\Shop\FacetRatingDto;
@@ -37,6 +38,7 @@
3738
#[UsesClass(FacetRatingDto::class)]
3839
#[UsesClass(FacetSelectDto::class)]
3940
#[UsesClass(FacetValueDto::class)]
41+
#[UsesClass(FacetCategoryDto::class)]
4042
#[UsesClass(ProductBaseDto::class)]
4143
#[UsesClass(ProductPropertyDto::class)]
4244
#[UsesClass(AbstractService::class)]

0 commit comments

Comments
 (0)