@@ -37,10 +37,10 @@ public function __construct(
37
37
* @return array{
38
38
* id: string|int,
39
39
* name: string,
40
- * description: string,
40
+ * description? : string,
41
41
* price: float,
42
42
* list_price: float,
43
- * image: string,
43
+ * image? : string,
44
44
* url: string,
45
45
* categories: array<int|string>,
46
46
* created_at: string,
@@ -72,9 +72,6 @@ public function normalize(mixed $object, ?string $format = null, array $context
72
72
throw new \InvalidArgumentException ('Product name must not be null for the product " ' . $ productId . '". ' );
73
73
}
74
74
$ productDescription = $ productTranslation ->getDescription ();
75
- if ($ productDescription === null ) {
76
- throw new \InvalidArgumentException ('Product description must not be null for the product " ' . $ productId . '". ' );
77
- }
78
75
$ productVariant = $ this ->productVariantResolver ->getVariant ($ product );
79
76
if (!$ productVariant instanceof ProductVariantInterface) {
80
77
throw new \InvalidArgumentException ('At least one product variant should exists for the product " ' . $ productId . '". ' );
@@ -83,7 +80,10 @@ public function normalize(mixed $object, ?string $format = null, array $context
83
80
$ originalPrice = $ this ->productVariantPricesCalculator ->calculateOriginal ($ productVariant , ['channel ' => $ channel ]);
84
81
85
82
$ productMainImage = $ this ->getMainImage ($ product );
86
- $ productMainImageUrl = $ this ->getUrlOfImage ($ productMainImage , $ channel );
83
+ $ productMainImageUrl = null ;
84
+ if ($ productMainImage !== null ) {
85
+ $ productMainImageUrl = $ this ->getUrlOfImage ($ productMainImage , $ channel );
86
+ }
87
87
$ productUrl = $ this ->getUrlOfProduct ($ productTranslation , $ channel );
88
88
$ createdAt = $ product ->getCreatedAt ();
89
89
if ($ createdAt === null ) {
@@ -93,14 +93,18 @@ public function normalize(mixed $object, ?string $format = null, array $context
93
93
$ productData = [
94
94
'id ' => $ productId ,
95
95
'name ' => $ productName ,
96
- 'description ' => $ productDescription ,
97
96
'price ' => $ price / 100 ,
98
97
'list_price ' => $ originalPrice / 100 ,
99
- 'image ' => $ productMainImageUrl ,
100
98
'url ' => $ productUrl ,
101
99
'categories ' => $ this ->getCategoryIds ($ product ),
102
100
'created_at ' => $ createdAt ->format ('c ' ),
103
101
];
102
+ if ($ productDescription !== null ) {
103
+ $ productData ['description ' ] = $ productDescription ;
104
+ }
105
+ if ($ productMainImageUrl !== null ) {
106
+ $ productData ['image ' ] = $ productMainImageUrl ;
107
+ }
104
108
105
109
$ productNormalizerEvent = new ProductNormalizerEvent (
106
110
$ productData ,
@@ -123,19 +127,18 @@ public function supportsNormalization(mixed $data, ?string $format = null, array
123
127
;
124
128
}
125
129
126
- public function getMainImage (ProductInterface $ product ): ProductImageInterface
130
+ public function getMainImage (ProductInterface $ product ): ? ProductImageInterface
127
131
{
128
132
$ imageByType = $ product ->getImagesByType ($ this ->imageType )->first ();
129
133
if ($ imageByType instanceof ProductImageInterface) {
130
134
return $ imageByType ;
131
135
}
132
-
133
136
$ image = $ product ->getImages ()->first ();
134
137
if ($ image instanceof ProductImageInterface) {
135
138
return $ image ;
136
139
}
137
140
138
- throw new \ InvalidArgumentException ( ' Product " ' . ( string ) $ product -> getId () . ' " has no images. ' ) ;
141
+ return null ;
139
142
}
140
143
141
144
public function getUrlOfImage (ProductImageInterface $ productMainImage , ChannelInterface $ channel ): string
@@ -146,8 +149,11 @@ public function getUrlOfImage(ProductImageInterface $productMainImage, ChannelIn
146
149
Assert::stringNotEmpty ($ channelHost );
147
150
$ channelRequestContext ->setHost ($ channelHost );
148
151
152
+ $ imagePath = $ productMainImage ->getPath ();
153
+ Assert::stringNotEmpty ($ imagePath , 'Product image path must not be empty. ' );
154
+
149
155
$ imageUrl = $ this ->cacheManager ->getBrowserPath (
150
- ( string ) $ productMainImage -> getPath () ,
156
+ $ imagePath ,
151
157
$ this ->imageFilterToApply ,
152
158
);
153
159
0 commit comments