1
|
|
|
<?php |
2
|
|
|
namespace Wambo\Catalog\Mapper; |
3
|
|
|
|
4
|
|
|
use Wambo\Catalog\Error\ProductException; |
5
|
|
|
use Wambo\Catalog\Model\Content; |
6
|
|
|
use Wambo\Catalog\Model\Product; |
7
|
|
|
use Wambo\Catalog\Model\SKU; |
8
|
|
|
use Wambo\Catalog\Model\Slug; |
9
|
|
|
use Wambo\Catalog\Validation\SKUValidator; |
10
|
|
|
use Wambo\Catalog\Validation\SlugValidator; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class ProductMapper creates \Wambo\Model\Product models from data bags with product data. |
14
|
|
|
* |
15
|
|
|
* @package Wambo\Mapper |
16
|
|
|
*/ |
17
|
|
|
class ProductMapper |
18
|
|
|
{ |
19
|
|
|
const FIELD_SKU = "sku"; |
20
|
|
|
const FIELD_SLUG = "slug"; |
21
|
|
|
const FIELD_TITLE = "title"; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var array $mandatoryFields A list of all mandatory fields of a Product |
25
|
|
|
*/ |
26
|
|
|
private $mandatoryFields = [self::FIELD_SKU, self::FIELD_SLUG, self::FIELD_TITLE]; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var ContentMapper |
30
|
|
|
*/ |
31
|
|
|
private $contentMapper; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Creates a new ProductMapper instance |
35
|
|
|
* |
36
|
|
|
* @param ContentMapper $contentMapper A class for mapping product content |
37
|
|
|
*/ |
38
|
|
|
public function __construct(ContentMapper $contentMapper) |
39
|
|
|
{ |
40
|
|
|
$this->contentMapper = $contentMapper; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Get a Catalog model from an array of unstructured product data |
45
|
|
|
* |
46
|
|
|
* @param array $productData An array containing product attributes |
47
|
|
|
* |
48
|
|
|
* @return Product |
49
|
|
|
* |
50
|
|
|
* @throws ProductException If a mandatory field is missing |
51
|
|
|
* @throws ProductException If the no Product could be created from the given product data |
52
|
|
|
*/ |
53
|
|
|
public function getProduct(array $productData) |
54
|
|
|
{ |
55
|
|
|
// check if all mandatory fields are present |
56
|
|
|
foreach ($this->mandatoryFields as $mandatoryField) { |
57
|
|
|
if (!array_key_exists($mandatoryField, $productData)) { |
58
|
|
|
throw new ProductException("The field '$mandatoryField' is missing in the given product data"); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
// try to create a product from the available data |
63
|
|
|
try { |
64
|
|
|
|
65
|
|
|
// sku |
66
|
|
|
$sku = new SKU($productData[self::FIELD_SKU]); |
67
|
|
|
|
68
|
|
|
// slug |
69
|
|
|
$slug = new Slug($productData[self::FIELD_SLUG]); |
70
|
|
|
|
71
|
|
|
// product title |
72
|
|
|
$title = $productData[self::FIELD_TITLE]; |
73
|
|
|
|
74
|
|
|
// product content |
75
|
|
|
$content = $this->contentMapper->getContent($productData); |
76
|
|
|
|
77
|
|
|
$product = new Product($sku, $slug, $title, $content); |
78
|
|
|
return $product; |
79
|
|
|
|
80
|
|
|
} catch (\Exception $productException) { |
81
|
|
|
throw new ProductException(sprintf("Failed to create a product from the given data: %s", |
82
|
|
|
$productException->getMessage()), $productException); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
} |