/**
  * Serialize extref to JSON
  *
  * @param JsonDeserializationVisitor $visitor Visitor
  * @param string                     $url     Extref URL
  * @param array                      $type    Type
  * @param Context                    $context Context
  * @return ExtReference|null
  */
 public function deserializeExtReferenceFromJson(JsonDeserializationVisitor $visitor, $url, array $type, Context $context)
 {
     try {
         return $this->converter->getExtReference($visitor->visitString($url, $type, $context));
     } catch (\InvalidArgumentException $e) {
         return null;
     }
 }
 /**
  * Transforms a string (url) to an object (extref)
  *
  * @param string $url extref url
  * @return ExtReference|null
  * @throws TransformationFailedException if url is not valid
  */
 public function reverseTransform($url)
 {
     if ($url === '' || $url === null) {
         return null;
     }
     try {
         return $this->converter->getExtReference($url);
     } catch (\InvalidArgumentException $e) {
         throw new TransformationFailedException(sprintf('Cannot transform URL "%s" to extref', $url), 0, $e);
     }
 }
Exemple #3
0
 /**
  * loops our structure recursively to find all $ref objects that need to be converted
  * either from that or to that..
  *
  * @param mixed $input input structure
  *
  * @return array altered structure with replaced $ref objects
  */
 public static function processDynExtRefs($input)
 {
     if ($input instanceof \stdClass) {
         if (!empty(get_object_vars($input))) {
             $input = self::processDynExtRefs(get_object_vars($input));
         }
         return $input;
     }
     $externalRefFieldName = '$ref';
     $internalRefFieldName = 'ref';
     if (is_array($input)) {
         foreach ($input as $key => $value) {
             if ($key === $internalRefFieldName) {
                 if (is_array($value) && isset($value['$ref']) && isset($value['$id'])) {
                     $extRef = ExtReference::create($value['$ref'], $value['$id']);
                     $input[$externalRefFieldName] = self::$extRefConverter->getUrl($extRef);
                     unset($input[$internalRefFieldName]);
                 }
             } elseif ($key === $externalRefFieldName) {
                 $extRef = self::$extRefConverter->getExtReference($value);
                 $input[$internalRefFieldName] = $extRef->jsonSerialize();
                 unset($input[$externalRefFieldName]);
             } else {
                 if (is_array($value)) {
                     $value = self::processDynExtRefs($value);
                 }
                 $input[$key] = $value;
             }
         }
     }
     return $input;
 }
 /**
  * Test ExtRefTransformer::reverseTransform() with valid url
  *
  * @return string
  */
 public function testReverseTransformValid()
 {
     $extref = ExtReference::create('ref', 'id');
     $url = 'extref-url';
     $this->converter->expects($this->once())->method('getExtReference')->with($url)->willReturn($extref);
     $this->assertEquals($extref, $this->transformer->reverseTransform($url));
 }
 /**
  * Test ExtReferenceHandler::deserializeExtReferenceFromJson()
  *
  * @return void
  */
 public function testDeserializeExtReferenceFromJson()
 {
     $type = [__LINE__];
     $context = DeserializationContext::create();
     $url = __FUNCTION__;
     $extref = ExtReference::create(__METHOD__, __FILE__);
     $this->converter->expects($this->once())->method('getExtReference')->with($url)->willReturn($extref);
     $this->deserializationVisitor->expects($this->once())->method('visitString')->with($url, $type, $context)->willReturn($url);
     $handler = new ExtReferenceHandler($this->converter);
     $this->assertEquals($extref, $handler->deserializeExtReferenceFromJson($this->deserializationVisitor, $url, $type, $context));
 }
 /**
  * Get DbRef from extref URL
  *
  * @param string $url Extref URL representation
  * @return object
  */
 private function getDbRefValue($url)
 {
     if ($url === null) {
         return null;
     }
     try {
         $extref = $this->converter->getExtReference($url);
         return \MongoDBRef::create($extref->getRef(), $extref->getId());
     } catch (\InvalidArgumentException $e) {
         //make up some invalid refs to ensure we find nothing if an invalid url was given
         return \MongoDBRef::create(false, false);
     }
 }
 /**
  * Test ExtReferenceSearchListener::onVisitNode() with context
  *
  * @return void
  */
 public function testOnVisitNodeWithContext()
 {
     $extrefMapping = ['route.id' => ['array.0.field.$ref']];
     $extrefUrl = 'extref.url';
     $extrefValue = ExtReference::create('Ref', 'id');
     $dbRefValue = \MongoDBRef::create($extrefValue->getRef(), $extrefValue->getId());
     $this->requestAttrs->expects($this->once())->method('get')->with('_route')->willReturn('route.id');
     $this->converter->expects($this->once())->method('getExtReference')->with($extrefUrl)->willReturn($extrefValue);
     $node = new EqNode('field.$ref', $extrefUrl);
     $context = new \SplStack();
     $context->push(new ElemMatchNode('array', $node));
     $builder = $this->getMockBuilder('Doctrine\\ODM\\MongoDB\\Query\\Builder')->disableOriginalConstructor()->getMock();
     $event = new VisitNodeEvent($node, $builder, $context);
     $listener = $this->createListener($extrefMapping);
     $listener->onVisitNode($event);
     $this->assertNotSame($node, $event->getNode());
     $this->assertSame($builder, $event->getBuilder());
     $this->assertEquals(new EqNode('field.$ref', $dbRefValue), $event->getNode());
 }