3 namespace Wallabag\CoreBundle\Helper
;
5 use Wallabag\CoreBundle\Entity\Entry
;
6 use Wallabag\CoreBundle\Entity\Tag
;
7 use Wallabag\CoreBundle\Repository\TagRepository
;
14 protected $tagRepository;
16 public function __construct(TagRepository
$tagRepository)
18 $this->tagRepository
= $tagRepository;
22 * Assign some tags to an entry.
24 * @param array|string $tags An array of tag or a string coma separated of tag
25 * @param array $entitiesReady Entities from the EntityManager which are persisted but not yet flushed
26 * It is mostly to fix duplicate tag on import @see http://stackoverflow.com/a/7879164/569101
30 public function assignTagsToEntry(Entry
$entry, $tags, array $entitiesReady = [])
34 if (!\
is_array($tags)) {
35 $tags = explode(',', $tags);
38 // keeps only Tag entity from the "not yet flushed entities"
39 $tagsNotYetFlushed = [];
40 foreach ($entitiesReady as $entity) {
41 if ($entity instanceof Tag
) {
42 $tagsNotYetFlushed[$entity->getLabel()] = $entity;
46 foreach ($tags as $label) {
47 $label = trim(mb_convert_case($label, MB_CASE_LOWER
));
50 if (0 === \
strlen($label)) {
54 if (isset($tagsNotYetFlushed[$label])) {
55 $tagEntity = $tagsNotYetFlushed[$label];
57 $tagEntity = $this->tagRepository
->findOneByLabel($label);
59 if (null === $tagEntity) {
60 $tagEntity = new Tag();
61 $tagEntity->setLabel($label);
65 // only add the tag on the entry if the relation doesn't exist
66 if (false === $entry->getTags()->contains($tagEntity)) {
67 $entry->addTag($tagEntity);
68 $tagsEntities[] = $tagEntity;