|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of Sulu. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) MASSIVE ART WebServices GmbH |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the MIT license that is bundled |
|
9
|
|
|
* with this source code in the file LICENSE. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Sulu\Component\DocumentManager\Strategy; |
|
13
|
|
|
|
|
14
|
|
|
use PHPCR\NodeInterface; |
|
15
|
|
|
use PHPCR\Query\QOM\QueryObjectModelConstantsInterface as QOMConstants; |
|
16
|
|
|
use PHPCR\Query\QOM\QueryObjectModelFactoryInterface; |
|
17
|
|
|
use PHPCR\Util\UUIDHelper; |
|
18
|
|
|
use Sulu\Component\DocumentManager\DocumentStrategyInterface; |
|
19
|
|
|
use Sulu\Component\DocumentManager\MetadataFactoryInterface; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Manage nodes via. a jcr mixin. |
|
23
|
|
|
*/ |
|
24
|
|
|
class MixinStrategy implements DocumentStrategyInterface |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var MetadataFactoryInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
private $metadataFactory; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param MetadataFactoryInterface $metadataFactory |
|
33
|
|
|
*/ |
|
34
|
|
|
public function __construct(MetadataFactoryInterface $metadataFactory) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->metadataFactory = $metadataFactory; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* {@inheritdoc} |
|
41
|
|
|
*/ |
|
42
|
|
|
public function createNodeForDocument($document, NodeInterface $parentNode, $name) |
|
43
|
|
|
{ |
|
44
|
|
|
$metadata = $this->metadataFactory->getMetadataForClass(get_class($document)); |
|
45
|
|
|
|
|
46
|
|
|
$node = $parentNode->addNode($name); |
|
47
|
|
|
$node->addMixin($metadata->getPhpcrType()); |
|
48
|
|
|
$node->setProperty('jcr:uuid', UUIDHelper::generateUUID()); |
|
49
|
|
|
|
|
50
|
|
|
return $node; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* {@inheritdoc} |
|
55
|
|
|
*/ |
|
56
|
|
|
public function resolveMetadataForNode(NodeInterface $node) |
|
57
|
|
|
{ |
|
58
|
|
|
if (false === $node->hasProperty('jcr:mixinTypes')) { |
|
59
|
|
|
return; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$mixinTypes = (array) $node->getPropertyValue('jcr:mixinTypes'); |
|
63
|
|
|
|
|
64
|
|
|
foreach ($mixinTypes as $mixinType) { |
|
65
|
|
|
if (true == $this->metadataFactory->hasMetadataForPhpcrType($mixinType)) { |
|
|
|
|
|
|
66
|
|
|
return $this->metadataFactory->getMetadataForPhpcrType($mixinType); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
return; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* {@inheritdoc} |
|
75
|
|
|
*/ |
|
76
|
|
|
public function getPrimaryNodeType($classFqn) |
|
77
|
|
|
{ |
|
78
|
|
|
return 'nt:unstructured'; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* {@inheritdoc} |
|
83
|
|
|
*/ |
|
84
|
|
|
public function createSourceConstraint(QueryObjectModelFactoryInterface $qomf, $sourceAlias, $classFqn) |
|
85
|
|
|
{ |
|
86
|
|
|
$metadata = $this->metadataFactory->getMetadataForClass($classFqn); |
|
87
|
|
|
|
|
88
|
|
|
return $qomf->comparison( |
|
|
|
|
|
|
89
|
|
|
$qomf->propertyValue( |
|
90
|
|
|
$sourceAlias, |
|
91
|
|
|
'jcr:mixinTypes' |
|
92
|
|
|
), |
|
93
|
|
|
QOMConstants::JCR_OPERATOR_EQUAL_TO, |
|
94
|
|
|
$qomf->literal($metadata->getPhpcrType()) |
|
95
|
|
|
); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
When comparing two booleans, it is generally considered safer to use the strict comparison operator.