-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
eZObjectWrapperFactory.php
93 lines (78 loc) · 3.21 KB
/
eZObjectWrapperFactory.php
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
89
90
91
92
93
<?php
namespace Kaliop\eZObjectWrapperBundle\Core;
use eZ\Publish\API\Repository\Values\Content\Content;
use eZ\Publish\API\Repository\Values\Content\Location;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Foncia\eZObjectWrapper\HelpBlock;
/**
* Factory which provide eZObjectWrapper objects or eZObjectWrapper children objects, according to parameters sets in eZObjectWrapper.yaml
* Class eZObjectWrapperFactory
* @package Kaliop\eZObjectWrapperBundle\Core
*/
class eZObjectWrapperFactory
{
/**
* @var \Symfony\Component\DependencyInjection\ContainerInterface
*/
private $container;
/**
* @var \eZ\Publish\API\Repository\Repository
*/
private $repository;
/**
* set the repository
* @param ContainerInterface $container
*/
public function __construct(ContainerInterface $container)
{
$this->container = $container;
$this->repository = $this->container->get('ezpublish.api.repository');
}
/**
* Create a eZObjectWrapper object, or a child class of eZObjectWrapper, according to parameters set in eZObjectWrapper.yml
* @param $source integer|Location|Content
* @return \Kaliop\eZObjectWrapperBundle\Core\eZObjectWrapper
*/
public function buildeZObjectWrapper($source)
{
$locationSource = null;
$contentSource = null;
if(is_numeric($source)) {
$locationSource = $source = $this->repository->getLocationService()->loadLocation($source);
} elseif($source instanceof Content) {
$contentSource = $source;
} else {
$locationSource = $source;
}
$contentTypeIdentifier = $this->repository->getContentTypeService()->loadContentType($source->contentInfo->contentTypeId)->identifier;
$mappingEntities = $this->container->getParameter('class_mapping');
$defaultClass = $this->container->getParameter('default_ezobject_class');
if(isset($mappingEntities[$contentTypeIdentifier])){
$className = $mappingEntities[$contentTypeIdentifier];
} else {
$className = $defaultClass;
}
$objectWrapper = new $className($this->container, $locationSource, $contentSource);
return $objectWrapper;
}
/**
* Returns an array of eZObjectWrapper objects - wrapper's location is the content's main location
* @param array $contentIds
* @return array
*/
public function buildeZObjectWrappersByContentIds(array $contentIds)
{
$objectWrapperList = array();
foreach ($contentIds as $contentId) {
$content = $this->repository->getContentService()->loadContent($contentId);
$contentInfo = $content->contentInfo;
if ($contentInfo->mainLocationId !== null) {
$location = $this->repository->getLocationService()->loadLocation($contentInfo->mainLocationId);
$objectWrapper = $this->buildeZObjectWrapper($location);
$objectWrapper->setContent($content);
$objectWrapperList[]=$objectWrapper;
}
}
return $objectWrapperList;
}
}