-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathBaseMapper.php
More file actions
88 lines (72 loc) · 2.17 KB
/
BaseMapper.php
File metadata and controls
88 lines (72 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
/**
* This file is part of the contentful/contentful package.
*
* @copyright 2015-2026 Contentful GmbH
* @license MIT
*/
declare(strict_types=1);
namespace Contentful\Delivery\Mapper;
use Contentful\Core\Resource\ResourceInterface;
use Contentful\Core\Resource\SystemPropertiesInterface;
use Contentful\Core\ResourceBuilder\MapperInterface;
use Contentful\Core\ResourceBuilder\ObjectHydrator;
use Contentful\Core\ResourceBuilder\ResourceBuilderInterface;
use Contentful\Delivery\Client\ClientInterface;
use Contentful\RichText\ParserInterface;
/**
* BaseMapper class.
*/
abstract class BaseMapper implements MapperInterface
{
/**
* @var ResourceBuilderInterface
*/
protected $builder;
/**
* @var ClientInterface
*/
protected $client;
/**
* @var ParserInterface
*/
protected $richTextParser;
/**
* @var ObjectHydrator
*/
protected $hydrator;
/**
* BaseMapper constructor.
*/
public function __construct(ResourceBuilderInterface $builder, ClientInterface $client, ParserInterface $richTextParser)
{
$this->builder = $builder;
$this->client = $client;
$this->richTextParser = $richTextParser;
$this->hydrator = new ObjectHydrator();
}
protected function createSystemProperties(string $class, array $data): SystemPropertiesInterface
{
$sys = $data['sys'];
if (isset($sys['space']) && !$sys['space'] instanceof ResourceInterface) {
$sys['space'] = $this->client->getSpace();
}
if (isset($sys['environment']) && !$sys['environment'] instanceof ResourceInterface) {
$sys['environment'] = $this->client->getEnvironment();
}
if (isset($sys['contentType']) && !$sys['contentType'] instanceof ResourceInterface) {
$sys['contentType'] = $this->client->getContentType($sys['contentType']['sys']['id']);
}
return new $class($sys);
}
/**
* @return array
*/
protected function normalizeFieldData($fieldData, ?string $locale = null)
{
if (!$locale) {
return $fieldData;
}
return [$locale => $fieldData];
}
}