/** * {@inheritDoc} */ public function getOptions($object) { $meta = $this->om->getClassMetadata(get_class($object)); if (!isset($this->options[$meta->name])) { $config = $this->sluggable->getConfiguration($this->om, $meta->name); $options = $config['handlers'][get_called_class()]; $default = array('separator' => '/'); $this->options[$meta->name] = array_merge($default, $options); } return $this->options[$meta->name]; }
/** * {@inheritDoc} */ public function onSlugCompletion(SluggableAdapter $ea, array &$config, $object, &$slug) { if (!$this->isInsert) { $options = $this->getOptions($object); $wrapped = AbstractWrapper::wrapp($object, $this->om); $meta = $wrapped->getMetadata(); $extConfig = $this->sluggable->getConfiguration($this->om, $meta->name); $config['useObjectClass'] = $extConfig['useObjectClass']; $target = $wrapped->getPropertyValue($config['slug']); $config['pathSeparator'] = $options['separator']; $ea->replaceRelative($object, $config, $target.$options['separator'], $slug); $uow = $this->om->getUnitOfWork(); // update in memory objects foreach ($uow->getIdentityMap() as $className => $objects) { // for inheritance mapped classes, only root is always in the identity map if ($className !== $wrapped->getRootObjectName()) { continue; } foreach ($objects as $object) { if (property_exists($object, '__isInitialized__') && !$object->__isInitialized__) { continue; } $oid = spl_object_hash($object); $objectSlug = $meta->getReflectionProperty($config['slug'])->getValue($object); if (preg_match("@^{$target}{$options['separator']}@smi", $objectSlug)) { $objectSlug = str_replace($target, $slug, $objectSlug); $meta->getReflectionProperty($config['slug'])->setValue($object, $objectSlug); $ea->setOriginalObjectProperty($uow, $oid, $config['slug'], $objectSlug); } } } } }
/** * {@inheritDoc} */ public function onSlugCompletion(SluggableAdapter $ea, array &$config, $object, &$slug) { $this->om = $ea->getObjectManager(); $isInsert = $this->om->getUnitOfWork()->isScheduledForInsert($object); if (!$isInsert) { $options = $config['handlers'][get_called_class()]; $wrapped = AbstractWrapper::wrapp($object, $this->om); $oldSlug = $wrapped->getPropertyValue($config['slug']); $mappedByConfig = $this->sluggable->getConfiguration($this->om, $options['relationClass']); if ($mappedByConfig) { $meta = $this->om->getClassMetadata($options['relationClass']); if (!$meta->isSingleValuedAssociation($options['mappedBy'])) { throw new InvalidMappingException("Unable to find " . $wrapped->getMetadata()->name . " relation - [{$options['mappedBy']}] in class - {$meta->name}"); } if (!isset($mappedByConfig['slugs'][$options['inverseSlugField']])) { throw new InvalidMappingException("Unable to find slug field - [{$options['inverseSlugField']}] in class - {$meta->name}"); } $mappedByConfig['slug'] = $mappedByConfig['slugs'][$options['inverseSlugField']]['slug']; $mappedByConfig['mappedBy'] = $options['mappedBy']; $ea->replaceInverseRelative($object, $mappedByConfig, $slug, $oldSlug); $uow = $this->om->getUnitOfWork(); // update in memory objects foreach ($uow->getIdentityMap() as $className => $objects) { // for inheritance mapped classes, only root is always in the identity map if ($className !== $mappedByConfig['useObjectClass']) { continue; } foreach ($objects as $object) { if (property_exists($object, '__isInitialized__') && !$object->__isInitialized__) { continue; } $oid = spl_object_hash($object); $objectSlug = $meta->getReflectionProperty($mappedByConfig['slug'])->getValue($object); if (preg_match("@^{$oldSlug}@smi", $objectSlug)) { $objectSlug = str_replace($oldSlug, $slug, $objectSlug); $meta->getReflectionProperty($mappedByConfig['slug'])->setValue($object, $objectSlug); $ea->setOriginalObjectProperty($uow, $oid, $mappedByConfig['slug'], $objectSlug); } } } } } }
/** * @test */ public function shouldBeAbleToMapSluggableMetadata() { $meta = $this->em->getClassMetadata('Mapping\\Fixture\\Xml\\Sluggable'); $config = $this->sluggable->getConfiguration($this->em, $meta->name); $this->assertArrayHasKey('slug', $config['slugs']); $this->assertCount(1, $config['slugs']); $config = $config['slugs']['slug']; $this->assertEquals('slug', $config['slug']); $this->assertArrayHasKey('style', $config); $this->assertEquals('camel', $config['style']); $this->assertArrayHasKey('updatable', $config); $this->assertFalse($config['updatable']); $this->assertArrayHasKey('unique', $config); $this->assertTrue($config['unique']); $this->assertArrayHasKey('separator', $config); $this->assertEquals('_', $config['separator']); $this->assertArrayHasKey('fields', $config); $this->assertCount(3, $config['fields']); $fields = $config['fields']; $this->assertEquals('title', $fields[0]); $this->assertEquals('ean', $fields[1]); $this->assertEquals('code', $fields[2]); $this->assertArrayHasKey('handlers', $config); $this->assertEquals(2, count($config['handlers'])); $handlers = $config['handlers']; $this->assertArrayHasKey('Gedmo\\Sluggable\\Handler\\TreeSlugHandler', $handlers); $this->assertArrayHasKey('Gedmo\\Sluggable\\Handler\\RelativeSlugHandler', $handlers); $first = $handlers['Gedmo\\Sluggable\\Handler\\TreeSlugHandler']; $this->assertEquals(2, count($first)); $this->assertArrayHasKey('parentRelationField', $first); $this->assertArrayHasKey('separator', $first); $this->assertEquals('parent', $first['parentRelationField']); $this->assertEquals('/', $first['separator']); $second = $handlers['Gedmo\\Sluggable\\Handler\\RelativeSlugHandler']; $this->assertEquals(3, count($second)); $this->assertArrayHasKey('relationField', $second); $this->assertArrayHasKey('relationSlugField', $second); $this->assertArrayHasKey('separator', $second); $this->assertEquals('parent', $second['relationField']); $this->assertEquals('test', $second['relationSlugField']); $this->assertEquals('-', $second['separator']); }
/** * @test */ public function shouldBeAbleToMapSluggableMetadata() { $meta = $this->em->getClassMetadata('Mapping\\Fixture\\Xml\\Sluggable'); $config = $this->sluggable->getConfiguration($this->em, $meta->name); $this->assertArrayHasKey('slug', $config['slugs']); $this->assertCount(1, $config['slugs']); $config = $config['slugs']['slug']; $this->assertEquals('slug', $config['slug']); $this->assertArrayHasKey('style', $config); $this->assertEquals('camel', $config['style']); $this->assertArrayHasKey('updatable', $config); $this->assertFalse($config['updatable']); $this->assertArrayHasKey('unique', $config); $this->assertTrue($config['unique']); $this->assertArrayHasKey('separator', $config); $this->assertEquals('_', $config['separator']); $this->assertArrayHasKey('fields', $config); $this->assertCount(3, $config['fields']); $fields = $config['fields']; $this->assertEquals('title', $fields[0]); $this->assertEquals('ean', $fields[1]); $this->assertEquals('code', $fields[2]); }
public function testSluggableMetadata() { $meta = $this->em->getClassMetadata('Mapping\\Fixture\\Xml\\Sluggable'); $config = $this->sluggable->getConfiguration($this->em, $meta->name); $this->assertArrayHasKey('slugFields', $config); $this->assertEquals('slug', $config['slugFields']['slug']['slug']); $this->assertArrayHasKey('style', $config['slugFields']['slug']); $this->assertEquals('camel', $config['slugFields']['slug']['style']); $this->assertArrayHasKey('updatable', $config['slugFields']['slug']); $this->assertTrue($config['slugFields']['slug']['updatable']); $this->assertArrayHasKey('unique', $config['slugFields']['slug']); $this->assertTrue($config['slugFields']['slug']['unique']); $this->assertArrayHasKey('separator', $config['slugFields']['slug']); $this->assertEquals('_', $config['slugFields']['slug']['separator']); $this->assertArrayHasKey('fields', $config); $this->assertEquals(3, count($config['fields']['slug'])); $fields = $config['fields']; $this->assertEquals('title', $fields['slug'][0]['field']); $this->assertEquals(0, $fields['slug'][0]['position']); $this->assertEquals('code', $fields['slug'][1]['field']); $this->assertEquals(false, $fields['slug'][1]['position']); $this->assertEquals('ean', $fields['slug'][2]['field']); $this->assertEquals(1, $fields['slug'][2]['position']); }