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.
25 * @param array|string $tags An array of tag or a string coma separated of tag
26 * @param array $entitiesReady Entities from the EntityManager which are persisted but not yet flushed
27 * It is mostly to fix duplicate tag on import @see http://stackoverflow.com/a/7879164/569101
31 public function assignTagsToEntry(Entry
$entry, $tags, array $entitiesReady = [])
35 if (!\
is_array($tags)) {
36 $tags = explode(',', $tags);
39 // keeps only Tag entity from the "not yet flushed entities"
40 $tagsNotYetFlushed = [];
41 foreach ($entitiesReady as $entity) {
42 if ($entity instanceof Tag
) {
43 $tagsNotYetFlushed[$entity->getLabel()] = $entity;
47 foreach ($tags as $label) {
48 $label = trim(mb_convert_case($label, MB_CASE_LOWER
));
51 if (0 === \
strlen($label)) {
55 if (isset($tagsNotYetFlushed[$label])) {
56 $tagEntity = $tagsNotYetFlushed[$label];
58 $tagEntity = $this->tagRepository
->findOneByLabel($label);
60 if (null === $tagEntity) {
61 $tagEntity = new Tag();
62 $tagEntity->setLabel($label);
66 // only add the tag on the entry if the relation doesn't exist
67 if (false === $entry->getTags()->contains($tagEntity)) {
68 $entry->addTag($tagEntity);
69 $tagsEntities[] = $tagEntity;