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