Skip to content

Commit

Permalink
Datetime support with default dateFormat DateTime::RFC2822
Browse files Browse the repository at this point in the history
  • Loading branch information
Geoffrey Hervet committed Oct 26, 2013
1 parent a2da910 commit 8d6991d
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 21 deletions.
16 changes: 15 additions & 1 deletion doc/sluggable.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ Features:
[blog_reference]: http://gediminasm.org/article/sluggable-behavior-extension-for-doctrine-2 "Sluggable extension for Doctrine 2 makes automatic record field transformations into url friendly names"
[blog_test]: http://gediminasm.org/test "Test extensions on this blog"

Update **2013-10-26**

- Datetime support with default dateFormat DateTime::RFC2822

Update **2013-08-23**

- Added 'prefix' and 'suffix' configuration parameter #812
Expand Down Expand Up @@ -401,18 +405,28 @@ class Article
{
// ...
/**
* @Gedmo\Slug(fields={"title"}, style="camel", separator="_", updatable=false, unique=false)
* @Gedmo\Slug(fields={"title", "created"}, style="camel", separator="_", updatable=false, unique=false, dateFormat="d/m/Y H-i-s")
* @Doctrine\ORM\Mapping\Column(length=128, unique=true)
*/
private $slug;
// ...

// ...
/**
* @Doctrine\ORM\Mapping\Column(type="datetime", name="created_at")
*/
private $createdAt;

// ...
/**
* @Doctrine\ORM\Mapping\Column(length=128)
*/
private $title;
// ...
public function __construct()
{
$this->createdAt = new \DateTime;
}
}
```

Expand Down
4 changes: 2 additions & 2 deletions example/app/Entity/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Category

/**
* @Gedmo\Translatable
* @Gedmo\Slug(fields={"title"})
* @Gedmo\Slug(fields={"created", "title"})
* @ORM\Column(length=64, unique=true)
*/
private $slug;
Expand Down Expand Up @@ -216,4 +216,4 @@ public function __toString()
{
return $this->getTitle();
}
}
}
3 changes: 2 additions & 1 deletion lib/Gedmo/Mapping/Annotation/Slug.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@ final class Slug extends Annotation
public $suffix = '';
/** @var array<Gedmo\Mapping\Annotation\SlugHandler> */
public $handlers = array();
/** @var string */
public $dateFormat = \DateTime::RFC2822;
}

2 changes: 2 additions & 0 deletions lib/Gedmo/Sluggable/Mapping/Driver/Annotation.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class Annotation extends AbstractAnnotationDriver
'text',
'integer',
'int',
'datetime',
);

/**
Expand Down Expand Up @@ -125,6 +126,7 @@ public function readExtendedMetadata($meta, array &$config) {
'fields' => $slug->fields,
'slug' => $field,
'style' => $slug->style,
'dateFormat' => $slug->dateFormat,
'updatable' => $slug->updatable,
'unique' => $slug->unique,
'unique_base' => $slug->unique_base,
Expand Down
3 changes: 3 additions & 0 deletions lib/Gedmo/Sluggable/Mapping/Driver/Xml.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class Xml extends BaseXml
'text',
'integer',
'int',
'datetime',
);

/**
Expand Down Expand Up @@ -89,6 +90,8 @@ public function readExtendedMetadata($meta, array &$config)
$this->_getAttribute($slug, 'style') : 'default',
'updatable' => $this->_isAttributeSet($slug, 'updatable') ?
$this->_getBooleanAttribute($slug, 'updatable') : true,
'dateFormat' => $this->_isAttributeSet($slug, 'dateFormat') ?
$this->_getAttribute($slug, 'dateFormat') : \DateTime::RFC2822,
'unique' => $this->_isAttributeSet($slug, 'unique') ?
$this->_getBooleanAttribute($slug, 'unique') : true,
'unique_base' => $this->_isAttributeSet($slug, 'unique_base') ?
Expand Down
4 changes: 4 additions & 0 deletions lib/Gedmo/Sluggable/Mapping/Driver/Yaml.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class Yaml extends File implements Driver
'text',
'integer',
'int',
'datetime',
);

/**
Expand Down Expand Up @@ -80,6 +81,9 @@ public function readExtendedMetadata($meta, array &$config)
$config['slugs'][$field]['style'] = isset($slug['style']) ?
(string)$slug['style'] : 'default';

$config['slugs'][$field]['dateFormat'] = isset($slug['dateFormat']) ?
(bool)$slug['dateFormat'] : \DateTime::RFC2822;

$config['slugs'][$field]['updatable'] = isset($slug['updatable']) ?
(bool)$slug['updatable'] : true;

Expand Down
5 changes: 3 additions & 2 deletions lib/Gedmo/Sluggable/Sluggable.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ interface Sluggable
* prefix (optional, default="") - suffix which will be added to the generated slug
* suffix (optional, default="") - prefix which will be added to the generated slug
* style (optional, default="default") - "default" all letters will be lowercase, "camel" - first word letter will be uppercase
*
* dateFormat (optional, default="default") - "default" all letters will be lowercase, "camel" - first word letter will be uppercase
*
* example:
*
* @gedmo:Slug(style="camel", separator="_", prefix="", suffix="" updatable=false, unique=false)
* @Column(type="string", length=64)
* $property
*/
}
}
4 changes: 3 additions & 1 deletion lib/Gedmo/Sluggable/SluggableListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,9 @@ private function generateSlug(SluggableAdapter $ea, $object)
if (isset($changeSet[$sluggableField]) || isset($changeSet[$slugField])) {
$needToChangeSlug = true;
}
$slug .= $meta->getReflectionProperty($sluggableField)->getValue($object) . ' ';
$value = $meta->getReflectionProperty($sluggableField)->getValue($object);
$slug .= ($value instanceof \DateTime) ? $value->format($options['dateFormat']) : $value;
$slug .= ' ';
}
} else {
// slug was set manually
Expand Down
28 changes: 14 additions & 14 deletions tests/Gedmo/Translator/TranslatableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,22 +143,22 @@ public function testTranslatableProxyWithUpperCaseProperty()
{
$person = new Person();
$person->setName('Jen');
$person->translate('ru_RU')->name = 'Женя';
$person->setLastName('Abramowicz');
$person->translate('ru_RU')->name = 'Женя';
$person->setLastName('Abramowicz');
$person->translate('ru_RU')->setLastName('Абрамович');
$person->setDescription('description');
$person->translate('ru_RU')->setDescription('multilingual description');
$this->em->persist($person);
$this->em->flush();
$this->em->clear();
$personProxy = $this->em->getReference(self::PERSON, array('id' => 1));
$person->setDescription('description');
$person->translate('ru_RU')->setDescription('multilingual description');

$this->em->persist($person);
$this->em->flush();
$this->em->clear();

$personProxy = $this->em->getReference(self::PERSON, array('id' => 1));
$this->assertTrue($personProxy instanceof Proxy);
$name = $personProxy->translate('ru_RU')->getName();
$this->assertSame('Женя', $name);
$lastName = $personProxy->translate('ru_RU')->getLastName();
$this->assertSame('Абрамович', $lastName);
$name = $personProxy->translate('ru_RU')->getName();
$this->assertSame('Женя', $name);
$lastName = $personProxy->translate('ru_RU')->getLastName();
$this->assertSame('Абрамович', $lastName);
}

public function testTranslatableWithMagicProperties()
Expand Down

0 comments on commit 8d6991d

Please sign in to comment.