Esempio n. 1
0
 /**
  * Convert DB value to PHP representation
  *
  * @param mixed $value Value to convert
  * @return ExtReference|null
  */
 public static function convertToPhp($value)
 {
     if (is_array($value) && isset($value['$ref'], $value['$id'])) {
         return ExtReference::create($value['$ref'], $value['$id']);
     } elseif (is_object($value) && isset($value->{'$ref'}, $value->{'$id'})) {
         return ExtReference::create($value->{'$ref'}, $value->{'$id'});
     } else {
         return null;
     }
 }
Esempio n. 2
0
 /**
  * return the extref from URL
  *
  * @param string $url Extref URL
  * @return ExtReference
  * @throws \InvalidArgumentException
  */
 public function getExtReference($url)
 {
     $path = parse_url($url, PHP_URL_PATH);
     if ($path === false) {
         throw new \InvalidArgumentException(sprintf('URL %s', $url));
     }
     $id = null;
     $collection = null;
     foreach ($this->router->getRouteCollection()->all() as $route) {
         list($collection, $id) = $this->getDataFromRoute($route, $path);
         if ($collection !== null && $id !== null) {
             return ExtReference::create($collection, $id);
         }
     }
     throw new \InvalidArgumentException(sprintf('Could not read URL %s', $url));
 }
Esempio n. 3
0
 /**
  * {@inheritDoc}
  *
  * @param ObjectManager $manager Object Manager
  *
  * @return void
  */
 public function load(ObjectManager $manager)
 {
     $deGerman = new Translatable();
     $deGerman->setId('i18n-de-German');
     $deGerman->setDomain('i18n');
     $deGerman->setLocale('de');
     $deGerman->setOriginal('German');
     $deGerman->setTranslated('Deutsch');
     $deGerman->setIsLocalized(true);
     $manager->persist($deGerman);
     $deEnglish = new Translatable();
     $deEnglish->setId('i18n-de-English');
     $deEnglish->setDomain('i18n');
     $deEnglish->setLocale('de');
     $deEnglish->setOriginal('English');
     $deEnglish->setTranslated('Englisch');
     $deEnglish->setIsLocalized(true);
     $language = new TranslatableLanguage();
     $language->setRef(ExtReference::create('Language', 'de'));
     $deEnglish->setLanguage($language);
     $manager->persist($deEnglish);
     $manager->flush();
 }
Esempio n. 4
0
 /**
  * Test ExtReference::closureToPHP()
  *
  * @return void
  */
 public function testClosureToPHP()
 {
     $this->assertEqualsClosure(ExtReference::create(__METHOD__, __FILE__), \MongoDBRef::create(__METHOD__, __FILE__), $this->type->closureToPHP());
     $this->assertEqualsClosure(null, null, $this->type->closureToPHP());
 }
 /**
  * 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));
 }
Esempio n. 6
0
 /**
  * {@inheritDoc}
  *
  * @param ObjectManager $manager Object Manager
  *
  * @return void
  */
 public function load(ObjectManager $manager)
 {
     $deAdmin = new Translatable();
     $deAdmin->setId('i18n-de-Administration');
     $deAdmin->setDomain('core');
     $deAdmin->setLocale('de');
     $deAdmin->setOriginal('Administration');
     $deAdmin->setTranslated('Die Administration');
     $deAdmin->setIsLocalized(true);
     $language = new TranslatableLanguage();
     $language->setRef(ExtReference::create('Language', 'de'));
     $deAdmin->setLanguage($language);
     $manager->persist($deAdmin);
     $frAdmin = new Translatable();
     $frAdmin->setId('i18n-fr-Administration');
     $frAdmin->setDomain('core');
     $frAdmin->setLocale('fr');
     $frAdmin->setOriginal('Administration');
     $frAdmin->setIsLocalized(false);
     $language = new TranslatableLanguage();
     $language->setRef(ExtReference::create('Language', 'fr'));
     $frAdmin->setLanguage($language);
     $manager->persist($frAdmin);
     $enAdmin = new Translatable();
     $enAdmin->setId('i18n-en-Administration');
     $enAdmin->setDomain('core');
     $enAdmin->setLocale('en');
     $enAdmin->setOriginal('Administration');
     $enAdmin->setTranslated('Administration');
     $enAdmin->setIsLocalized(true);
     $language = new TranslatableLanguage();
     $language->setRef(ExtReference::create('Language', 'en'));
     $enAdmin->setLanguage($language);
     $manager->persist($enAdmin);
     /*** THIS TRANSLATABLES HERE SHOULD *NOT* BE TRANSLATED FOR TESTS ***/
     $deTablet = new Translatable();
     $deTablet->setId('i18n-de-Tablet');
     $deTablet->setDomain('core');
     $deTablet->setLocale('de');
     $deTablet->setOriginal('Tablet');
     $deTablet->setIsLocalized(false);
     $language = new TranslatableLanguage();
     $language->setRef(ExtReference::create('Language', 'de'));
     $deTablet->setLanguage($language);
     $manager->persist($deTablet);
     $frTablet = new Translatable();
     $frTablet->setId('i18n-fr-Tablet');
     $frTablet->setDomain('core');
     $frTablet->setLocale('fr');
     $frTablet->setOriginal('Tablet');
     $frTablet->setIsLocalized(false);
     $language = new TranslatableLanguage();
     $language->setRef(ExtReference::create('Language', 'fr'));
     $frTablet->setLanguage($language);
     $manager->persist($frTablet);
     $enTablet = new Translatable();
     $enTablet->setId('i18n-en-Tablet');
     $enTablet->setDomain('core');
     $enTablet->setLocale('en');
     $enTablet->setOriginal('Tablet');
     $enTablet->setTranslated('Tablet');
     $enTablet->setIsLocalized(true);
     $language = new TranslatableLanguage();
     $language->setRef(ExtReference::create('Language', 'en'));
     $enTablet->setLanguage($language);
     $manager->persist($enTablet);
     $manager->flush();
 }
Esempio n. 7
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;
 }
Esempio n. 8
0
 /**
  * [In|Up]serts a Translatable object using an array with language strings.
  *
  * @param array $values array with language strings; key should be language id
  * @throws \Exception
  *
  * @return void
  */
 public function insertTranslatable(array $values)
 {
     if (!isset($values[$this->getDefaultLanguage()])) {
         throw new \Exception(sprintf('Creating new Translatable without "%s" key is not support yet.', $this->getDefaultLanguage()));
     }
     $original = $values[$this->getDefaultLanguage()];
     if ($this->isTranslatableContext()) {
         $languages = $this->getLanguages();
         \array_walk($languages, function ($locale) use($original, $values) {
             $isLocalized = false;
             $translated = '';
             if (array_key_exists($locale, $values)) {
                 $translated = $values[$locale];
                 $isLocalized = true;
             }
             $translatable = new TranslatableDocument();
             $translatable->setId('i18n-' . $locale . '-' . $original);
             $translatable->setLocale($locale);
             $translatable->setDomain($this->getTranslatableDomain());
             $translatable->setOriginal($original);
             $translatable->setTranslated($translated);
             $translatable->setIsLocalized($isLocalized);
             $translatableLang = new TranslatableLanguage();
             $translatableLang->setRef(ExtReference::create('Language', $locale));
             $translatable->setLanguage($translatableLang);
             $this->translatable->insertRecord($translatable);
         });
     }
 }
 /**
  * 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());
 }
 /**
  * @return array
  */
 public function getUrlProvider()
 {
     return [[ExtReference::create('App', 'test'), 'graviton.core.rest.app.get', 'http://localhost/core/app/test'], [ExtReference::create('Language', 'en'), 'graviton.i18n.rest.language.get', 'http://localhost/i18n/language/en']];
 }
 /**
  * Test validate()
  *
  * @return void
  */
 public function testValidateAllowedNeeded()
 {
     $extref = ExtRef::create(__FILE__, __FUNCTION__);
     $constraint = new ExtReference();
     $constraint->collections = [__FILE__];
     $this->context->expects($this->never())->method('addViolation');
     $validator = $this->createValidator();
     $validator->validate($extref, $constraint);
 }
Esempio n. 12
0
 /**
  * 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));
 }