Code
Explorer
You are a developer and looking for Shopware projects?
Apply Now!
ValueListItem example
$condition
=
$criteria
->
getCondition
(
$criteriaPart
->
getName
(
)
)
;
if
(
$condition
instanceof ProductAttributeCondition
)
{
$actives
=
$condition
->
getValue
(
)
;
// $condition->getValue() can return a string
if
(
!\
is_array
(
$actives
)
)
{
$actives
=
[
$actives
]
;
}
}
$items
=
array_map
(
function
D
$row
)
use
(
$actives
)
{
return
new
ValueListItem
(
$row
,
$row
, \
in_array
(
$row
,
$actives
)
)
;
}
,
$values
)
;
if
(
$criteriaPart
->
getMode
(
)
=== ProductAttributeFacet::MODE_RADIO_LIST_RESULT
)
{
return
new
RadioFacetResult
(
$criteriaPart
->
getName
(
)
,
$criteria
->
hasCondition
(
$criteriaPart
->
getName
(
)
)
,
$criteriaPart
->
getLabel
(
)
,
$items
,
$criteriaPart
->
getFormFieldName
(
)
)
;
}
private
function
createListItems
(
Criteria
$criteria
, array
$manufacturers
)
: array
{
$actives
=
[
]
;
$condition
=
$criteria
->
getCondition
(
'manufacturer'
)
;
if
(
$condition
instanceof ManufacturerCondition
)
{
$actives
=
$condition
->
getManufacturerIds
(
)
;
}
$items
=
[
]
;
foreach
(
$manufacturers
as
$manufacturer
)
{
$items
[
]
=
new
ValueListItem
(
$manufacturer
->
getId
(
)
,
$manufacturer
->
getName
(
)
,
\
in_array
(
$manufacturer
->
getId
(
)
,
$actives
)
,
$manufacturer
->
getAttributes
(
)
)
;
}
usort
(
$items
,
function
DValueListItem
$a
, ValueListItem
$b
)
{
return
strcasecmp
(
$a
->
getLabel
(
)
,
$b
->
getLabel
(
)
)
;
}
)
;
if
(
!\
is_array
(
$actives
)
)
{
$actives
=
[
$actives
]
;
}
$items
=
array_map
(
function
D
$row
)
use
(
$actives
,
$facet
)
{
$viewName
=
$row
[
$facet
->
getField
(
)
]
;
$translation
=
$this
->
extractTranslations
(
$row
, '__attribute_' .
$facet
->
getField
(
)
)
;
if
(
$translation
!== null
)
{
$viewName
=
$translation
;
}
return
new
ValueListItem
(
$row
[
$facet
->
getField
(
)
]
,
$viewName
, \
in_array
(
$row
[
$facet
->
getField
(
)
]
,
$actives
)
)
;
}
,
$result
)
;
if
(
$facet
->
getMode
(
)
=== ProductAttributeFacet::MODE_RADIO_LIST_RESULT
)
{
return
new
RadioFacetResult
(
$facet
->
getName
(
)
,
$criteria
->
hasCondition
(
$facet
->
getName
(
)
)
,
$facet
->
getLabel
(
)
,
$items
,
$facet
->
getFormFieldName
(
)
)
;
}
private
function
getFacet
(
ManufacturerFacet
$facet
,
array
$manufacturers
,
array
$activeIds
)
: ValueListFacetResult
{
$listItems
=
[
]
;
foreach
(
$manufacturers
as
$manufacturer
)
{
$listItem
=
new
ValueListItem
(
$manufacturer
->
getId
(
)
,
$manufacturer
->
getName
(
)
,
\
in_array
(
$manufacturer
->
getId
(
)
,
$activeIds
)
,
$manufacturer
->
getAttributes
(
)
)
;
$listItems
[
]
=
$listItem
;
}
usort
(
$listItems
,
function
DValueListItem
$a
, ValueListItem
$b
)
{
return
strcasecmp
(
$a
->
getLabel
(
)
,
$b
->
getLabel
(
)
)
;
}
)
;
$values
=
[
]
;
for
(
$i
= 1;
$i
<= 4; ++
$i
)
{
$affected
=
array_filter
(
$data
,
static
function
D
$value
)
use
(
$i
)
{
return
$value
[
'average'
]
>=
$i
;
}
)
;
$count
=
array_sum
(
array_column
(
$affected
, 'count'
)
)
;
if
(
$count
=== 0
)
{
continue
;
}
$values
[
]
=
new
ValueListItem
(
$i
,
(string)
$count
,
(int)
$activeAverage
===
$i
)
;
}
usort
(
$values
,
static
function
DValueListItem
$a
, ValueListItem
$b
)
{
return
$b
->
getId
(
)
<=>
$a
->
getId
(
)
;
}
)
;
return
$values
;
}
}
$values
=
[
]
;
for
(
$i
= 1;
$i
<= 4; ++
$i
)
{
$affected
=
array_filter
(
$data
,
static
function
D
$value
)
use
(
$i
)
{
return
$i
<=
(
$value
[
'key'
]
/ 2
)
;
}
)
;
$count
=
array_sum
(
array_column
(
$affected
, 'doc_count'
)
)
;
if
(
$count
=== 0
)
{
continue
;
}
$values
[
]
=
new
ValueListItem
(
$i
,
(string)
$count
,
(int)
$activeAverage
===
$i
)
;
}
usort
(
$values
,
static
function
DValueListItem
$a
, ValueListItem
$b
)
{
return
$b
->
getId
(
)
<=>
$a
->
getId
(
)
;
}
)
;
return
$values
;
}
}