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\Country\Persistence\Propel\Mapper; |
||
9 | |||
10 | use Generated\Shared\Transfer\CountryCollectionTransfer; |
||
11 | use Generated\Shared\Transfer\CountryTransfer; |
||
12 | use Generated\Shared\Transfer\RegionTransfer; |
||
13 | use Orm\Zed\Country\Persistence\SpyCountry; |
||
14 | use Orm\Zed\Country\Persistence\SpyRegion; |
||
15 | use Propel\Runtime\Collection\Collection; |
||
16 | |||
17 | class CountryMapper |
||
18 | { |
||
19 | /** |
||
20 | * @param \Propel\Runtime\Collection\Collection<\Orm\Zed\Country\Persistence\SpyRegion> $regionEntities |
||
21 | * |
||
22 | * @return array<int, list<\Generated\Shared\Transfer\RegionTransfer>> |
||
0 ignored issues
–
show
Documentation
Bug
introduced
by
![]() |
|||
23 | */ |
||
24 | public function mapRegionEntitiesToRegionTransfersGroupedByIdCountry(Collection $regionEntities): array |
||
25 | { |
||
26 | $regionTransfersGroupedByIdCountry = []; |
||
27 | |||
28 | foreach ($regionEntities as $regionEntity) { |
||
29 | $regionTransfersGroupedByIdCountry[(int)$regionEntity->getFkCountry()][] = $this->mapRegionEntityToRegionTransfer( |
||
30 | $regionEntity, |
||
31 | new RegionTransfer(), |
||
32 | ); |
||
33 | } |
||
34 | |||
35 | return $regionTransfersGroupedByIdCountry; |
||
36 | } |
||
37 | |||
38 | /** |
||
39 | * @param \Orm\Zed\Country\Persistence\SpyRegion $regionEntity |
||
40 | * @param \Generated\Shared\Transfer\RegionTransfer $regionTransfer |
||
41 | * |
||
42 | * @return \Generated\Shared\Transfer\RegionTransfer |
||
43 | */ |
||
44 | public function mapRegionEntityToRegionTransfer(SpyRegion $regionEntity, RegionTransfer $regionTransfer): RegionTransfer |
||
45 | { |
||
46 | return $regionTransfer->fromArray($regionEntity->toArray(), true); |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * @param iterable<\Orm\Zed\Country\Persistence\SpyCountry> $countryEntities |
||
51 | * @param \Generated\Shared\Transfer\CountryCollectionTransfer $countryCollectionTransfer |
||
52 | * |
||
53 | * @return \Generated\Shared\Transfer\CountryCollectionTransfer |
||
54 | */ |
||
55 | public function mapCountryTransferCollection(iterable $countryEntities, CountryCollectionTransfer $countryCollectionTransfer): CountryCollectionTransfer |
||
56 | { |
||
57 | foreach ($countryEntities as $countryEntity) { |
||
58 | $countryCollectionTransfer->addCountries( |
||
59 | $this->mapCountryTransfer( |
||
60 | $countryEntity, |
||
61 | new CountryTransfer(), |
||
62 | ), |
||
63 | ); |
||
64 | } |
||
65 | |||
66 | return $countryCollectionTransfer; |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * @param \Orm\Zed\Country\Persistence\SpyCountry $countryEntity |
||
71 | * @param \Generated\Shared\Transfer\CountryTransfer $countryTransfer |
||
72 | * |
||
73 | * @return \Generated\Shared\Transfer\CountryTransfer |
||
74 | */ |
||
75 | public function mapCountryTransfer(SpyCountry $countryEntity, CountryTransfer $countryTransfer): CountryTransfer |
||
76 | { |
||
77 | $countryTransfer = $countryTransfer |
||
78 | ->fromArray($countryEntity->toArray(), true); |
||
79 | |||
80 | foreach ($countryEntity->getSpyRegions() as $regionEntity) { |
||
81 | $countryTransfer->addRegion( |
||
82 | $this->mapRegionEntityToRegionTransfer($regionEntity, new RegionTransfer()), |
||
83 | ); |
||
84 | } |
||
85 | |||
86 | return $countryTransfer; |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * @param \Generated\Shared\Transfer\CountryTransfer $countryTransfer |
||
91 | * @param \Orm\Zed\Country\Persistence\SpyCountry $countryEntity |
||
92 | * |
||
93 | * @return \Orm\Zed\Country\Persistence\SpyCountry |
||
94 | */ |
||
95 | public function mapCountryTransferToCountryEntity(CountryTransfer $countryTransfer, SpyCountry $countryEntity): SpyCountry |
||
96 | { |
||
97 | return $countryEntity->setName($countryTransfer->getNameOrFail()) |
||
98 | ->setPostalCodeMandatory($countryTransfer->getPostalCodeMandatory()) |
||
99 | ->setPostalCodeRegex($countryTransfer->getPostalCodeRegex()) |
||
100 | ->setIso2Code($countryTransfer->getIso2CodeOrFail()) |
||
101 | ->setIso3Code($countryTransfer->getIso3CodeOrFail()); |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * @param \Orm\Zed\Country\Persistence\SpyCountry $countryEntity |
||
106 | * @param \Generated\Shared\Transfer\CountryTransfer $countryTransfer |
||
107 | * |
||
108 | * @return \Generated\Shared\Transfer\CountryTransfer |
||
109 | */ |
||
110 | public function mapCountryEntityToCountryTransfer(SpyCountry $countryEntity, CountryTransfer $countryTransfer): CountryTransfer |
||
111 | { |
||
112 | return $countryTransfer->fromArray($countryEntity->toArray(), true); |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * @param \Generated\Shared\Transfer\RegionTransfer $regionTransfer |
||
117 | * @param \Orm\Zed\Country\Persistence\SpyRegion $regionEntity |
||
118 | * |
||
119 | * @return \Orm\Zed\Country\Persistence\SpyRegion |
||
120 | */ |
||
121 | public function mapRegionTransferToRegionEntity(RegionTransfer $regionTransfer, SpyRegion $regionEntity): SpyRegion |
||
122 | { |
||
123 | return $regionEntity |
||
124 | ->setIso2Code($regionTransfer->getIso2CodeOrFail()) |
||
125 | ->setFkCountry($regionTransfer->getFkCountryOrFail()) |
||
126 | ->setName($regionTransfer->getNameOrFail()); |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * @param \Propel\Runtime\Collection\Collection<\Orm\Zed\Country\Persistence\SpyCountry> $countryEntities |
||
131 | * @param \Generated\Shared\Transfer\CountryCollectionTransfer $countryCollectionTransfer |
||
132 | * |
||
133 | * @return \Generated\Shared\Transfer\CountryCollectionTransfer |
||
134 | */ |
||
135 | public function mapCountryEntitiesToCountryCollectionTransfer( |
||
136 | Collection $countryEntities, |
||
137 | CountryCollectionTransfer $countryCollectionTransfer |
||
138 | ): CountryCollectionTransfer { |
||
139 | foreach ($countryEntities as $countryEntity) { |
||
140 | $countryCollectionTransfer->addCountries( |
||
141 | $this->mapCountryEntityToCountryTransfer($countryEntity, new CountryTransfer()), |
||
142 | ); |
||
143 | } |
||
144 | |||
145 | return $countryCollectionTransfer; |
||
146 | } |
||
147 | } |
||
148 |