Code
Explorer
You are a developer and looking for Shopware projects?
Apply Now!
sortByPosition example
$output
[
$lineItem
->
getId
(
)
]
=
array_filter
(
$data
,
fn
(
$value
)
=>
$value
!== null
)
;
if
(
$lineItem
->
hasChildren
(
)
)
{
$output
=
[
...
$output
, ...self::
transformCollection
(
$lineItem
->
getChildren
(
)
,
$id
)
]
;
}
return
$output
;
}
public
static
function
transformFlatToNested
(
OrderLineItemCollection
$lineItems
)
: LineItemCollection
{
$lineItems
->
sortByPosition
(
)
;
$index
=
[
]
;
$root
=
new
LineItemCollection
(
)
;
foreach
(
$lineItems
as
$id
=>
$lineItem
)
{
if
(
!\
array_key_exists
(
$id
,
$index
)
)
{
$index
[
$id
]
= self::
createLineItem
(
$lineItem
)
;
}
$currentLineItem
=
$index
[
$id
]
;
self::
updateLineItem
(
$currentLineItem
,
$lineItem
,
$id
)
;
public
function
getNestedLineItems
(
)
: ?OrderLineItemCollection
{
$lineItems
=
$this
->
getLineItems
(
)
;
if
(
!
$lineItems
)
{
return
null;
}
/** @var OrderLineItemCollection $roots */
$roots
=
$lineItems
->
filterByProperty
(
'parentId', null
)
;
$roots
->
sortByPosition
(
)
;
$this
->
addChildren
(
$lineItems
,
$roots
)
;
return
$roots
;
}
public
function
getAffiliateCode
(
)
: ?string
{
return
$this
->affiliateCode;
}
public
function
setAffiliateCode
(
?string
$affiliateCode
)
: void
{
$children
=
new
CategoryCollection
(
)
;
foreach
(
$categories
as
$key
=>
$category
)
{
if
(
$category
->
getParentId
(
)
!==
$parentId
)
{
continue
;
}
unset
(
$categories
[
$key
]
)
;
$children
->
add
(
$category
)
;
}
$children
->
sortByPosition
(
)
;
$items
=
new
CategoryCollection
(
)
;
foreach
(
$children
as
$child
)
{
if
(
!
$child
->
getActive
(
)
|| !
$child
->
getVisible
(
)
)
{
continue
;
}
$child
->
setChildren
(
$this
->
buildTree
(
$child
->
getId
(
)
,
$categories
)
)
;
$items
->
add
(
$child
)
;
}
private
function
assertOrderWithoutGrantedAccess
(
string
$orderId
, array
$productDownloads
)
: string
{
$criteria
=
new
Criteria
(
[
$orderId
]
)
;
$criteria
->
addAssociation
(
'lineItems.downloads'
)
;
$criteria
->
addAssociation
(
'deliveries'
)
;
$order
=
$this
->orderRepository->
search
(
$criteria
,
$this
->salesChannelContext->
getContext
(
)
)
->
first
(
)
;
static
::
assertInstanceOf
(
OrderEntity::
class
,
$order
)
;
$lineItems
=
$order
->
getLineItems
(
)
;
static
::
assertNotNull
(
$lineItems
)
;
$lineItems
->
sortByPosition
(
)
;
static
::
assertEquals
(
\
count
(
$productDownloads
)
,
$lineItems
->
count
(
)
)
;
static
::
assertTrue
(
$lineItems
->
hasLineItemWithState
(
State::IS_DOWNLOAD
)
)
;
foreach
(
$productDownloads
as
$key
=>
$downloadFiles
)
{
$lineItem
=
$lineItems
->
getAt
(
$key
)
;
static
::
assertNotNull
(
$lineItem
)
;
static
::
assertNotNull
(
$lineItem
->
getDownloads
(
)
)
;
static
::
assertEquals
(
\
count
(
$downloadFiles
)
,
$lineItem
->
getDownloads
(
)
->
count
(
)
)
;
foreach
(
$lineItem
->
getDownloads
(
)
as
$download
)
{
static
::
assertFalse
(
$download
->
isAccessGranted
(
)
)
;
$profile
=
$this
->profileRepository->
search
(
new
Criteria
(
[
$profileId
]
)
,
$context
)
->
first
(
)
;
if
(
$profile
=== null
)
{
throw
new
EntityNotFoundException
(
'import_export_profile',
$profileId
)
;
}
$mappings
=
$profile
->
getMapping
(
)
;
if
(
empty
(
$mappings
)
)
{
throw
new
\
RuntimeException
(
'ImportExportProfile "' .
$profileId
. '" has no mappings'
)
;
}
$config
=
new
Config
(
$mappings
,
[
]
,
[
]
)
;
$headers
=
[
]
;
$mappings
= MappingCollection::
fromIterable
(
$mappings
)
->
sortByPosition
(
)
;
/** @var Mapping $mapping */
foreach
(
$mappings
as
$mapping
)
{
$headers
[
$mapping
->
getMappedKey
(
)
]
= '';
}
// create the file
$expireDate
=
new
\
DateTimeImmutable
(
)
;
$expireDate
=
$expireDate
->
modify
(
'+1 hour'
)
;
$fileEntity
=
$this
->fileService->
storeFile
(
$context
,
/** @var CategoryEntity $first */
$first
=
$result
->
first
(
)
;
// First Level Category should have Level 1
static
::
assertEquals
(
$parent
,
$first
->
getId
(
)
)
;
static
::
assertEquals
(
1,
$first
->
getLevel
(
)
)
;
// Second Level Categories should have Level 2 /** @var CategoryCollection $children */
$children
=
$first
->
getChildren
(
)
;
$children
->
sortByPosition
(
)
;
$childrenArray
=
array_values
(
$children
->
getElements
(
)
)
;
static
::
assertEquals
(
$recordA
,
$childrenArray
[
0
]
->
getId
(
)
)
;
static
::
assertEquals
(
2,
$childrenArray
[
0
]
->
getLevel
(
)
)
;
static
::
assertEquals
(
$recordB
,
$childrenArray
[
1
]
->
getId
(
)
)
;
static
::
assertEquals
(
2,
$childrenArray
[
1
]
->
getLevel
(
)
)
;
$criteria
=
new
Criteria
(
[
$recordA
]
)
;
$criteria
->
addAssociation
(
'children'
)
;
/** @var CategoryCollection $result */
$result
=
$this
->repository->
search
(
$criteria
, Context::
createDefaultContext
(
)
)
;
if
(
!\
in_array
(
$profileEntity
->
getType
(
)
,
[
ImportExportProfileEntity::TYPE_EXPORT, ImportExportProfileEntity::TYPE_IMPORT_EXPORT
]
, true
)
)
{
throw
new
ProfileWrongTypeException
(
$profileEntity
->
getId
(
)
,
$profileEntity
->
getType
(
)
)
;
}
if
(
$originalFileName
=== null
)
{
$originalFileName
=
$this
->fileService->
generateFilename
(
$profileEntity
)
;
}
if
(
$profileEntity
->
getMapping
(
)
!== null
)
{
$mappings
= MappingCollection::
fromIterable
(
$profileEntity
->
getMapping
(
)
)
;
$profileEntity
->
setMapping
(
$mappings
->
sortByPosition
(
)
)
;
}
$fileEntity
=
$this
->fileService->
storeFile
(
$context
,
$expireDate
, null,
$originalFileName
,
$activity
,
$destinationPath
)
;
return
$this
->
createLog
(
$context
,
$activity
,
$fileEntity
,
$profileEntity
,
$config
)
;
}
/** * @param Config $config */
public
function
prepareImport
(
private
function
loadByIds
(
ProductCrossSellingEntity
$crossSelling
, SalesChannelContext
$context
, Criteria
$criteria
)
: CrossSellingElement
{
$element
=
new
CrossSellingElement
(
)
;
$element
->
setCrossSelling
(
$crossSelling
)
;
$element
->
setProducts
(
new
ProductCollection
(
)
)
;
$element
->
setTotal
(
0
)
;
if
(
!
$crossSelling
->
getAssignedProducts
(
)
)
{
return
$element
;
}
$crossSelling
->
getAssignedProducts
(
)
->
sortByPosition
(
)
;
$ids
=
array_values
(
$crossSelling
->
getAssignedProducts
(
)
->
getProductIds
(
)
)
;
$filter
=
new
ProductAvailableFilter
(
$context
->
getSalesChannel
(
)
->
getId
(
)
,
ProductVisibilityDefinition::VISIBILITY_LINK
)
;
if
(
!\
count
(
$ids
)
)
{
return
$element
;
}