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