Issues (3641)

Persistence/Mapper/MerchantStockMapper.php (1 issue)

1
<?php
2
3
/**
4
 * Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace Spryker\Zed\MerchantStock\Persistence\Mapper;
9
10
use ArrayObject;
11
use Generated\Shared\Transfer\MerchantStockTransfer;
12
use Generated\Shared\Transfer\StockTransfer;
13
use Generated\Shared\Transfer\StoreRelationTransfer;
14
use Orm\Zed\MerchantStock\Persistence\SpyMerchantStock;
15
use Orm\Zed\Stock\Persistence\SpyStock;
16
use Propel\Runtime\Collection\Collection;
17
18
class MerchantStockMapper
19
{
20
    /**
21
     * @var \Spryker\Zed\MerchantStock\Persistence\Mapper\StockStoreRelationMapper
22
     */
23
    protected StockStoreRelationMapper $stockStoreRelationMapper;
24
25
    /**
26
     * @param \Spryker\Zed\MerchantStock\Persistence\Mapper\StockStoreRelationMapper $stockStoreRelationMapper
27
     */
28
    public function __construct(StockStoreRelationMapper $stockStoreRelationMapper)
29
    {
30
        $this->stockStoreRelationMapper = $stockStoreRelationMapper;
31
    }
32
33
    /**
34
     * @param \Orm\Zed\Stock\Persistence\SpyStock $stockEntity
35
     * @param \Generated\Shared\Transfer\StockTransfer $stockTransfer
36
     *
37
     * @return \Generated\Shared\Transfer\StockTransfer
38
     */
39
    public function mapStockEntityToStockTransfer(
40
        SpyStock $stockEntity,
41
        StockTransfer $stockTransfer
42
    ): StockTransfer {
43
        $stockTransfer->fromArray($stockEntity->toArray(), true);
44
        $stockTransfer->setStoreRelation(
45
            $this->stockStoreRelationMapper->mapStockStoreEntitiesToStoreRelationTransfer(
46
                $stockEntity->getIdStock(),
47
                $stockEntity->getStockStores()->getArrayCopy(),
48
                new StoreRelationTransfer(),
49
            ),
50
        );
51
52
        return $stockTransfer;
53
    }
54
55
    /**
56
     * @param \Orm\Zed\MerchantStock\Persistence\SpyMerchantStock $merchantStockEntity
57
     * @param \Generated\Shared\Transfer\MerchantStockTransfer $merchantStockTransfer
58
     *
59
     * @return \Generated\Shared\Transfer\MerchantStockTransfer
60
     */
61
    public function mapMerchantStockEntityToMerchantStockTransfer(
62
        SpyMerchantStock $merchantStockEntity,
63
        MerchantStockTransfer $merchantStockTransfer
64
    ): MerchantStockTransfer {
65
        return $merchantStockTransfer->setIdMerchantStock($merchantStockEntity->getIdMerchantStock())
66
            ->setIdMerchant($merchantStockEntity->getFkMerchant())
67
            ->setIdStock($merchantStockEntity->getFkStock())
68
            ->setIsDefault($merchantStockEntity->getIsDefault());
69
    }
70
71
    /**
72
     * @param \Generated\Shared\Transfer\MerchantStockTransfer $merchantStockTransfer
73
     * @param \Orm\Zed\MerchantStock\Persistence\SpyMerchantStock $merchantStockEntity
74
     *
75
     * @return \Orm\Zed\MerchantStock\Persistence\SpyMerchantStock
76
     */
77
    public function mapMerchantStockTransferToMerchantStockEntity(
78
        MerchantStockTransfer $merchantStockTransfer,
79
        SpyMerchantStock $merchantStockEntity
80
    ): SpyMerchantStock {
81
        return $merchantStockEntity->setIdMerchantStock($merchantStockTransfer->getIdMerchantStock())
82
            ->setFkMerchant($merchantStockTransfer->getIdMerchant())
83
            ->setFkStock($merchantStockTransfer->getIdStock())
84
            ->setIsDefault($merchantStockTransfer->getIsDefault());
85
    }
86
87
    /**
88
     * @param \Propel\Runtime\Collection\Collection<\Orm\Zed\MerchantStock\Persistence\SpyMerchantStock> $merchantStocksEntities
89
     *
90
     * @return array<int, \ArrayObject<int, \Generated\Shared\Transfer\StockTransfer>>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<int, \ArrayObject<...ransfer\StockTransfer>> at position 4 could not be parsed: Expected '>' at position 4, but found '\ArrayObject'.
Loading history...
91
     */
92
    public function mapMerchantStockEntityCollectionToStocksGroupedByIdMerchant(Collection $merchantStocksEntities): array
93
    {
94
        $stockTransfersGroupedByIdMerchant = [];
95
96
        foreach ($merchantStocksEntities as $merchantStockEntity) {
97
            $idMerchant = $merchantStockEntity->getFkMerchant();
98
99
            if (!isset($stockTransfersGroupedByIdMerchant[$idMerchant])) {
100
                $stockTransfersGroupedByIdMerchant[$idMerchant] = new ArrayObject();
101
            }
102
103
            $stockTransfersGroupedByIdMerchant[$idMerchant]->append(
104
                $this->mapStockEntityToStockTransfer($merchantStockEntity->getSpyStock(), new StockTransfer()),
105
            );
106
        }
107
108
        return $stockTransfersGroupedByIdMerchant;
109
    }
110
}
111