1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Doctrine\ODM\CouchDB\Mapping\MetadataResolver; |
5
|
|
|
|
6
|
|
|
use Doctrine\ODM\CouchDB\Mapping\ClassMetadata; |
7
|
|
|
use Doctrine\ODM\CouchDB\DocumentManager; |
8
|
|
|
use Doctrine\ODM\CouchDB\PersistentIdsCollection; |
9
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
10
|
|
|
|
11
|
|
|
class DoctrineResolver implements MetadataResolver |
12
|
|
|
{ |
13
|
|
|
public function createDefaultDocumentStruct(ClassMetadata $class) |
14
|
|
|
{ |
15
|
|
|
$struct = array('type' => str_replace("\\", ".", $class->name)); |
16
|
|
|
if ($class->indexed) { |
17
|
|
|
$struct['doctrine_metadata']['indexed'] = true; |
18
|
|
|
} |
19
|
|
|
if ($class->indexes) { |
|
|
|
|
20
|
|
|
$struct['doctrine_metadata']['indexes'] = $class->indexes; |
21
|
|
|
} |
22
|
|
|
return $struct; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function canResolveJsonField($jsonName) |
26
|
|
|
{ |
27
|
|
|
return ($jsonName === 'doctrine_metadata'); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function getDocumentType(array $documentData) |
31
|
|
|
{ |
32
|
|
|
return (str_replace(".", "\\", $documentData['type'])); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function resolveJsonField(ClassMetadata $class, DocumentManager $dm, $documentState, $jsonName, $originalData) |
36
|
|
|
{ |
37
|
|
|
$uow = $dm->getUnitOfWork(); |
38
|
|
|
$couchClient = $dm->getCouchDBClient(); |
39
|
|
|
|
40
|
|
|
if ($jsonName == 'doctrine_metadata' && isset($originalData['doctrine_metadata']['associations'])) { |
41
|
|
|
foreach ($originalData['doctrine_metadata']['associations'] AS $assocName) { |
42
|
|
|
$assocValue = $originalData[$assocName]; |
43
|
|
|
if (isset($class->associationsMappings[$assocName])) { |
44
|
|
|
if ($class->associationsMappings[$assocName]['type'] & ClassMetadata::TO_ONE) { |
45
|
|
|
if ($assocValue) { |
46
|
|
|
if ($class->associationsMappings[$assocName]['targetDocument'] && |
47
|
|
|
! $dm->getClassMetadata($class->associationsMappings[$assocName]['targetDocument'])->inInheritanceHierachy) { |
48
|
|
|
|
49
|
|
|
$assocValue = $dm->getReference($class->associationsMappings[$assocName]['targetDocument'], $assocValue); |
50
|
|
|
} else { |
51
|
|
|
$response = $couchClient->findDocument($assocValue); |
52
|
|
|
|
53
|
|
|
if ($response->status == 404) { |
54
|
|
|
$assocValue = null; |
55
|
|
|
} else { |
56
|
|
|
$hints = array(); |
57
|
|
|
$assocValue = $uow->createDocument(null, $response->body, $hints); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
$documentState[$class->associationsMappings[$assocName]['fieldName']] = $assocValue; |
62
|
|
|
} else if ($class->associationsMappings[$assocName]['type'] & ClassMetadata::MANY_TO_MANY) { |
63
|
|
|
if ($class->associationsMappings[$assocName]['isOwning']) { |
64
|
|
|
$documentState[$class->associationsMappings[$assocName]['fieldName']] = new PersistentIdsCollection( |
65
|
|
|
new ArrayCollection(), |
66
|
|
|
$class->associationsMappings[$assocName]['targetDocument'], |
67
|
|
|
$dm, |
68
|
|
|
$assocValue |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
return $documentState; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function canMapDocument(array $documentData) |
79
|
|
|
{ |
80
|
|
|
return isset($documentData['type']); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function storeAssociationField($data, ClassMetadata $class, DocumentManager $dm, $fieldName, $fieldValue) |
84
|
|
|
{ |
85
|
|
|
$data['doctrine_metadata']['associations'][] = $fieldName; |
86
|
|
|
$data[$fieldName] = $fieldValue; |
87
|
|
|
return $data; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.