Skip to content

Commit ba34c26

Browse files
committed
Cleanup and comments about Tags feature implementation
1 parent df2960b commit ba34c26

File tree

5 files changed

+19
-14
lines changed

5 files changed

+19
-14
lines changed

src/AppBundle/Entity/Post.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -186,14 +186,6 @@ public function setAuthor(User $author)
186186
$this->author = $author;
187187
}
188188

189-
/**
190-
* Is the given User the author of this Post?
191-
*/
192-
public function isAuthor(User $user = null)
193-
{
194-
return $user === $this->author;
195-
}
196-
197189
public function getComments()
198190
{
199191
return $this->comments;

src/AppBundle/Entity/Tag.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ public function getName()
5555
*/
5656
public function jsonSerialize()
5757
{
58+
// This method is called when a Tag instance needs be serialized to JSON
59+
// See usage in app/Resources/views/form/fields.html.twig: tags|json_encode
60+
5861
return $this->name;
5962
}
6063

src/AppBundle/Form/DataTransformer/TagArrayToStringTransformer.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ public function __construct(ObjectManager $manager)
3737
*/
3838
public function transform($array)
3939
{
40+
// This transformation expects an array of Tag always thanks to
41+
// Symfony\Bridge\Doctrine\Form\DataTransformer\CollectionToArrayTransformer::transform()
42+
// Just remains transform this array into a comma separated string format.
43+
4044
/* @var Tag[] $array */
4145
return implode(',', $array);
4246
}
@@ -52,17 +56,22 @@ public function reverseTransform($string)
5256

5357
$names = explode(',', $string);
5458

59+
// Now we need to get the current tags and to find the new ones that should be created.
5560
$tags = $this->manager->getRepository(Tag::class)->findBy([
5661
'name' => $names,
5762
]);
58-
5963
$newNames = array_diff($names, $tags);
6064
foreach ($newNames as $name) {
6165
$tag = new Tag();
6266
$tag->setName($name);
6367
$tags[] = $tag;
68+
69+
// Don't worry about persisting these new tags, Doctrine is able to do it automatically
70+
// thanks to cascade={"persist"} configuration in AppBundle\Entity\Post::$tags property.
6471
}
6572

73+
// This one should return an array always to transform these tags into Collection instance again.
74+
// See Symfony\Bridge\Doctrine\Form\DataTransformer\CollectionToArrayTransformer::reverseTransform()
6675
return $tags;
6776
}
6877
}

src/AppBundle/Form/Type/DateTimePickerType.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@
2828
*/
2929
class DateTimePickerType extends AbstractType
3030
{
31-
/**
32-
* @var MomentFormatConverter
33-
*/
3431
private $formatConverter;
3532

3633
public function __construct()

src/AppBundle/Form/Type/TagsInputType.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,12 @@ public function buildForm(FormBuilderInterface $builder, array $options)
4545
{
4646
$builder
4747
->setByReference(false)
48-
->addModelTransformer(new TagArrayToStringTransformer($this->manager))
49-
->addModelTransformer(new CollectionToArrayTransformer())
48+
// We need to transform the tags collection into a comma separated string format
49+
// so we are reusing an existing one to simplify the code, but you could build
50+
// only one to do all transformation (i.e. Collection <-> string).
51+
// Transformation flow: Collection <-> array <-> string
52+
->addModelTransformer(new CollectionToArrayTransformer(), true)
53+
->addModelTransformer(new TagArrayToStringTransformer($this->manager), true)
5054
;
5155
}
5256

0 commit comments

Comments
 (0)