|
2 | 2 |
|
3 | 3 | namespace Printful\Structures\Sync\Requests; |
4 | 4 |
|
| 5 | +use Printful\Exceptions\PrintfulSdkException; |
| 6 | + |
5 | 7 | class SyncVariantRequest |
6 | 8 | { |
7 | 9 | /** @var int|null */ |
@@ -98,4 +100,85 @@ public static function fromArray(array $array) |
98 | 100 |
|
99 | 101 | return $syncVariantRequest; |
100 | 102 | } |
| 103 | + |
| 104 | + /** |
| 105 | + * Builds POST request array |
| 106 | + * |
| 107 | + * @return array |
| 108 | + * @throws PrintfulSdkException |
| 109 | + */ |
| 110 | + public function toPostArray() |
| 111 | + { |
| 112 | + if (!$this->variantId) { |
| 113 | + throw new PrintfulSdkException('Missing variant_id'); |
| 114 | + } |
| 115 | + |
| 116 | + $files = $this->getFiles(); |
| 117 | + if (empty($files)) { |
| 118 | + throw new PrintfulSdkException('Missing files'); |
| 119 | + } |
| 120 | + |
| 121 | + $syncVariantParams = []; |
| 122 | + |
| 123 | + $syncVariantParams['external_id'] = $this->externalId; |
| 124 | + $syncVariantParams['retail_price'] = $this->retailPrice; |
| 125 | + $syncVariantParams['variant_id'] = $this->variantId; |
| 126 | + |
| 127 | + $syncVariantParams['files'] = []; |
| 128 | + foreach ($files as $file) { |
| 129 | + $syncVariantParams['files'][] = $file->toArray(); |
| 130 | + } |
| 131 | + |
| 132 | + $syncVariantParams['options'] = []; |
| 133 | + foreach ($this->getOptions() as $option) { |
| 134 | + $syncVariantParams['options'][] = $option->toArray(); |
| 135 | + } |
| 136 | + |
| 137 | + return $syncVariantParams; |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Builds PUT request array |
| 142 | + * |
| 143 | + * @return array |
| 144 | + * @throws PrintfulSdkException |
| 145 | + */ |
| 146 | + public function toPutArray() |
| 147 | + { |
| 148 | + $syncVariantParams = []; |
| 149 | + |
| 150 | + if (!is_null($this->externalId)) { |
| 151 | + $syncVariantParams['external_id'] = $this->externalId; |
| 152 | + } |
| 153 | + |
| 154 | + if (!is_null($this->retailPrice)) { |
| 155 | + $syncVariantParams['retail_price'] = $this->retailPrice; |
| 156 | + } |
| 157 | + |
| 158 | + if (!is_null($this->variantId)) { |
| 159 | + if (!$this->variantId) { |
| 160 | + throw new PrintfulSdkException('Variant id is required'); |
| 161 | + } |
| 162 | + |
| 163 | + $syncVariantParams['variant_id'] = $this->variantId; |
| 164 | + } |
| 165 | + |
| 166 | + $files = $this->getFiles(); |
| 167 | + if (!is_null($files)) { |
| 168 | + $syncVariantParams['files'] = []; |
| 169 | + foreach ($files as $file){ |
| 170 | + $syncVariantParams['files'][] = $file->toArray(); |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + $options = $this->getOptions(); |
| 175 | + if (!is_null($options)) { |
| 176 | + $syncVariantParams['options'] = []; |
| 177 | + foreach ($options as $option) { |
| 178 | + $syncVariantParams['options'][] = $option->toArray(); |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + return $syncVariantParams; |
| 183 | + } |
101 | 184 | } |
0 commit comments