From ba34c26eca817bd4818cf8a57656f859b4a31ea4 Mon Sep 17 00:00:00 2001 From: Yonel Ceruto Date: Mon, 6 Feb 2017 13:33:16 -0500 Subject: [PATCH 1/5] Cleanup and comments about Tags feature implementation --- src/AppBundle/Entity/Post.php | 8 -------- src/AppBundle/Entity/Tag.php | 3 +++ .../DataTransformer/TagArrayToStringTransformer.php | 11 ++++++++++- src/AppBundle/Form/Type/DateTimePickerType.php | 3 --- src/AppBundle/Form/Type/TagsInputType.php | 8 ++++++-- 5 files changed, 19 insertions(+), 14 deletions(-) diff --git a/src/AppBundle/Entity/Post.php b/src/AppBundle/Entity/Post.php index 8f7ccaea1..a00a3356d 100644 --- a/src/AppBundle/Entity/Post.php +++ b/src/AppBundle/Entity/Post.php @@ -186,14 +186,6 @@ public function setAuthor(User $author) $this->author = $author; } - /** - * Is the given User the author of this Post? - */ - public function isAuthor(User $user = null) - { - return $user === $this->author; - } - public function getComments() { return $this->comments; diff --git a/src/AppBundle/Entity/Tag.php b/src/AppBundle/Entity/Tag.php index 0ba291f92..f1e014c4a 100644 --- a/src/AppBundle/Entity/Tag.php +++ b/src/AppBundle/Entity/Tag.php @@ -55,6 +55,9 @@ public function getName() */ public function jsonSerialize() { + // This method is called when a Tag instance needs be serialized to JSON + // See usage in app/Resources/views/form/fields.html.twig: tags|json_encode + return $this->name; } diff --git a/src/AppBundle/Form/DataTransformer/TagArrayToStringTransformer.php b/src/AppBundle/Form/DataTransformer/TagArrayToStringTransformer.php index 19c22f741..249f9df30 100644 --- a/src/AppBundle/Form/DataTransformer/TagArrayToStringTransformer.php +++ b/src/AppBundle/Form/DataTransformer/TagArrayToStringTransformer.php @@ -37,6 +37,10 @@ public function __construct(ObjectManager $manager) */ public function transform($array) { + // This transformation expects an array of Tag always thanks to + // Symfony\Bridge\Doctrine\Form\DataTransformer\CollectionToArrayTransformer::transform() + // Just remains transform this array into a comma separated string format. + /* @var Tag[] $array */ return implode(',', $array); } @@ -52,17 +56,22 @@ public function reverseTransform($string) $names = explode(',', $string); + // Now we need to get the current tags and to find the new ones that should be created. $tags = $this->manager->getRepository(Tag::class)->findBy([ 'name' => $names, ]); - $newNames = array_diff($names, $tags); foreach ($newNames as $name) { $tag = new Tag(); $tag->setName($name); $tags[] = $tag; + + // Don't worry about persisting these new tags, Doctrine is able to do it automatically + // thanks to cascade={"persist"} configuration in AppBundle\Entity\Post::$tags property. } + // This one should return an array always to transform these tags into Collection instance again. + // See Symfony\Bridge\Doctrine\Form\DataTransformer\CollectionToArrayTransformer::reverseTransform() return $tags; } } diff --git a/src/AppBundle/Form/Type/DateTimePickerType.php b/src/AppBundle/Form/Type/DateTimePickerType.php index 7ef3e13d1..d0d4b3040 100644 --- a/src/AppBundle/Form/Type/DateTimePickerType.php +++ b/src/AppBundle/Form/Type/DateTimePickerType.php @@ -28,9 +28,6 @@ */ class DateTimePickerType extends AbstractType { - /** - * @var MomentFormatConverter - */ private $formatConverter; public function __construct() diff --git a/src/AppBundle/Form/Type/TagsInputType.php b/src/AppBundle/Form/Type/TagsInputType.php index 666757f08..35c75c59a 100644 --- a/src/AppBundle/Form/Type/TagsInputType.php +++ b/src/AppBundle/Form/Type/TagsInputType.php @@ -45,8 +45,12 @@ public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->setByReference(false) - ->addModelTransformer(new TagArrayToStringTransformer($this->manager)) - ->addModelTransformer(new CollectionToArrayTransformer()) + // We need to transform the tags collection into a comma separated string format + // so we are reusing an existing one to simplify the code, but you could build + // only one to do all transformation (i.e. Collection <-> string). + // Transformation flow: Collection <-> array <-> string + ->addModelTransformer(new CollectionToArrayTransformer(), true) + ->addModelTransformer(new TagArrayToStringTransformer($this->manager), true) ; } From a574d24d11b40bb8fe82390bc333c8a1bb61b2ed Mon Sep 17 00:00:00 2001 From: Yonel Ceruto Date: Mon, 6 Feb 2017 18:49:29 -0500 Subject: [PATCH 2/5] Remove byReference option which is unnecessary now --- src/AppBundle/Form/Type/TagsInputType.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/AppBundle/Form/Type/TagsInputType.php b/src/AppBundle/Form/Type/TagsInputType.php index 35c75c59a..d39d96849 100644 --- a/src/AppBundle/Form/Type/TagsInputType.php +++ b/src/AppBundle/Form/Type/TagsInputType.php @@ -44,7 +44,6 @@ public function __construct(ObjectManager $manager) public function buildForm(FormBuilderInterface $builder, array $options) { $builder - ->setByReference(false) // We need to transform the tags collection into a comma separated string format // so we are reusing an existing one to simplify the code, but you could build // only one to do all transformation (i.e. Collection <-> string). From 4d71179cf974512df8df563f6592e1a85d4f998a Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Tue, 7 Feb 2017 08:45:35 +0100 Subject: [PATCH 3/5] Updated a help note --- src/AppBundle/Entity/Tag.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/AppBundle/Entity/Tag.php b/src/AppBundle/Entity/Tag.php index f1e014c4a..69bbe93a3 100644 --- a/src/AppBundle/Entity/Tag.php +++ b/src/AppBundle/Entity/Tag.php @@ -55,8 +55,9 @@ public function getName() */ public function jsonSerialize() { - // This method is called when a Tag instance needs be serialized to JSON - // See usage in app/Resources/views/form/fields.html.twig: tags|json_encode + // This entity implements JsonSerializable (http://php.net/manual/en/class.jsonserializable.php) + // so this method is used to customize its JSON representation when json_encode() + // is called, for example in tags|json_encode (app/Resources/views/form/fields.html.twig) return $this->name; } From a62876fa416e0611aa05103ad8200ce71bc08f77 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Tue, 7 Feb 2017 08:50:55 +0100 Subject: [PATCH 4/5] Updated some help notes --- .../DataTransformer/TagArrayToStringTransformer.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/AppBundle/Form/DataTransformer/TagArrayToStringTransformer.php b/src/AppBundle/Form/DataTransformer/TagArrayToStringTransformer.php index 249f9df30..835a61907 100644 --- a/src/AppBundle/Form/DataTransformer/TagArrayToStringTransformer.php +++ b/src/AppBundle/Form/DataTransformer/TagArrayToStringTransformer.php @@ -37,9 +37,9 @@ public function __construct(ObjectManager $manager) */ public function transform($array) { - // This transformation expects an array of Tag always thanks to + // The value received is an array of Tag objects generated with // Symfony\Bridge\Doctrine\Form\DataTransformer\CollectionToArrayTransformer::transform() - // Just remains transform this array into a comma separated string format. + // The value returned is a string that concatenates the string representation of those objects /* @var Tag[] $array */ return implode(',', $array); @@ -56,7 +56,7 @@ public function reverseTransform($string) $names = explode(',', $string); - // Now we need to get the current tags and to find the new ones that should be created. + // Get the current tags and find the new ones that should be created. $tags = $this->manager->getRepository(Tag::class)->findBy([ 'name' => $names, ]); @@ -66,11 +66,11 @@ public function reverseTransform($string) $tag->setName($name); $tags[] = $tag; - // Don't worry about persisting these new tags, Doctrine is able to do it automatically - // thanks to cascade={"persist"} configuration in AppBundle\Entity\Post::$tags property. + // There's no need to persist these new tags because Doctrine does that automatically + // thanks to the cascade={"persist"} option in the AppBundle\Entity\Post::$tags property. } - // This one should return an array always to transform these tags into Collection instance again. + // Return an array of tags to transform them back into a Doctrine Collection. // See Symfony\Bridge\Doctrine\Form\DataTransformer\CollectionToArrayTransformer::reverseTransform() return $tags; } From 6147bbd3eb2b87c7829cbe816327cdfc5c397458 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Tue, 7 Feb 2017 08:55:58 +0100 Subject: [PATCH 5/5] Updated the last help note --- src/AppBundle/Form/Type/TagsInputType.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/AppBundle/Form/Type/TagsInputType.php b/src/AppBundle/Form/Type/TagsInputType.php index d39d96849..2af056bdb 100644 --- a/src/AppBundle/Form/Type/TagsInputType.php +++ b/src/AppBundle/Form/Type/TagsInputType.php @@ -44,10 +44,10 @@ public function __construct(ObjectManager $manager) public function buildForm(FormBuilderInterface $builder, array $options) { $builder - // We need to transform the tags collection into a comma separated string format - // so we are reusing an existing one to simplify the code, but you could build - // only one to do all transformation (i.e. Collection <-> string). - // Transformation flow: Collection <-> array <-> string + // The Tag collection must be transformed into a comma separated string. + // We could create a custom transformer to do Collection <-> string in one step, + // but here we're doing the transformation in two steps (Collection <-> array <-> string) + // and reuse the existing CollectionToArrayTransformer. ->addModelTransformer(new CollectionToArrayTransformer(), true) ->addModelTransformer(new TagArrayToStringTransformer($this->manager), true) ;