/** * Initializes a new instance of the <tt>ProxyFactory</tt> class that is * connected to the given <tt>DocumentManager</tt>. * * @param DocumentManager $manager * @param string $proxyDir The directory to use for the proxy classes. It * must exist. * @param string $proxyNamespace The namespace to use for the proxy classes. * @param int $autoGenerate Whether to automatically generate proxy classes. */ public function __construct(DocumentManager $manager, $proxyDir, $proxyNamespace, $autoGenerate = AbstractProxyFactory::AUTOGENERATE_NEVER) { $this->metadataFactory = $manager->getMetadataFactory(); $this->uow = $manager->getUnitOfWork(); $this->proxyNamespace = $proxyNamespace; $proxyGenerator = new ProxyGenerator($proxyDir, $proxyNamespace); $proxyGenerator->setPlaceholder('baseProxyInterface', 'Doctrine\\ODM\\OrientDB\\Proxy\\Proxy'); parent::__construct($proxyGenerator, $this->metadataFactory, $autoGenerate); }
/** * @inheritdoc */ function hydrate($document, $data, array $hints = []) { $hydratedData = []; foreach ($this->metadata->fieldMappings as $fieldName => $mapping) { $name = $mapping['name']; $propertyValue = isset($data[$name]) ? $data[$name] : null; if (!isset($mapping['association'])) { if ($propertyValue === null) { continue; } $type = Type::getType($mapping['type']); $value = $type->convertToPHPValue($propertyValue); $this->metadata->reflFields[$fieldName]->setValue($document, $value); $hydratedData[$fieldName] = $value; continue; } if ($mapping['association'] & ClassMetadata::TO_MANY) { $coll = new PersistentCollection(new ArrayCollection(), $this->dm, $this->uow); $coll->setOwner($document, $mapping); $coll->setInitialized(false); if ($propertyValue) { $coll->setData($propertyValue); } $this->metadata->reflFields[$fieldName]->setValue($document, $coll); $hydratedData[$fieldName] = $coll; continue; } if ($propertyValue === null) { continue; } if ($mapping['association'] === ClassMetadata::LINK) { if (is_string($propertyValue)) { $link = $this->dm->getReference($propertyValue); } else { $link = $this->uow->getOrCreateDocument($propertyValue); } $this->metadata->reflFields[$fieldName]->setValue($document, $link); $hydratedData[$fieldName] = $link; continue; } if ($mapping['association'] === ClassMetadata::EMBED) { // an embed one must have @class, we would support generic JSON properties via another mapping type if (!isset($propertyValue[self::ORIENT_PROPERTY_CLASS])) { throw new HydratorException(sprintf("missing @class for embedded property '%s'", $name)); } $oclass = $propertyValue[self::ORIENT_PROPERTY_CLASS]; $embeddedMetadata = $this->dm->getMetadataFactory()->getMetadataForOClass($oclass); $doc = $embeddedMetadata->newInstance(); $embeddedData = $this->dm->getHydratorFactory()->hydrate($doc, $propertyValue, $hints); $this->uow->registerManaged($doc, null, $embeddedData); $this->metadata->reflFields[$fieldName]->setValue($document, $doc); $hydratedData[$fieldName] = $doc; continue; } } return $hydratedData; }
/** * @before */ public function before() { /** @var BindingInterface|ObjectProphecy $binding */ $binding = $this->prophesize(BindingInterface::class); $binding->getDatabaseName()->willReturn("ODM"); $data = <<<JSON { "classes": [ {"name":"LinkedContact", "clusters":[1]}, {"name":"LinkedEmailAddress", "clusters":[2]}, {"name":"LinkedPhone", "clusters":[3]} ] } JSON; $binding->getDatabaseInfo()->willReturn(json_decode($data, true)); $rawResult = '{ "@type": "d", "@rid": "#2:1", "@version": 1, "@class": "LinkedEmailAddress", "type": "work", "email": "*****@*****.**", "contact": "#1:1" }'; $binding->getDocument(Arg::is("#2:1"), Arg::any())->willReturn(json_decode($rawResult, true)); $rawResult = '[{ "@type": "d", "@rid": "#3:1", "@version": 1, "@class": "LinkedPhone", "type": "work", "phoneNumber": "4805551920", "primary": true },{ "@type": "d", "@rid": "#3:2", "@version": 1, "@class": "LinkedPhone", "type": "home", "phoneNumber": "5552094878", "primary": false }]'; $binding->query(Arg::any())->willReturn(json_decode($rawResult, true)); $this->manager = $this->createDocumentManagerWithBinding($binding->reveal(), [], ['test/Doctrine/ODM/OrientDB/Tests/Document/Stub']); $this->uow = $this->manager->getUnitOfWork(); $this->metadataFactory = $this->manager->getMetadataFactory(); }
/** * @before */ public function before() { $this->manager = $this->createDocumentManager([], ['Doctrine/ODM/OrientDB/Tests/Document/Stub/Linked']); $this->metadataFactory = $this->manager->getMetadataFactory(); }
/** * @return PersisterInterface * @throws \Exception */ private function createPersister() { return new SQLBatchPersister($this->dm->getMetadataFactory(), $this->dm->getBinding()); }