Skip to content

Commit

Permalink
new branch (with increased requirements)
Browse files Browse the repository at this point in the history
  • Loading branch information
garak committed Jun 11, 2018
1 parent 4305750 commit 35a5711
Show file tree
Hide file tree
Showing 15 changed files with 67 additions and 191 deletions.
7 changes: 2 additions & 5 deletions DependencyInjection/BeelabTagExtension.php
Expand Up @@ -8,16 +8,13 @@
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

/**
* This is the class that loads and manages your bundle configuration.
* This is the class that loads and manages bundle configuration.
*
* To learn more see {@link http://symfony.com/doc/current/bundles/extension.html}
*/
class BeelabTagExtension extends Extension
{
/**
* {@inheritdoc}
*/
public function load(array $configs, ContainerBuilder $container)
public function load(array $configs, ContainerBuilder $container): void
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
Expand Down
7 changes: 2 additions & 5 deletions DependencyInjection/Configuration.php
Expand Up @@ -6,16 +6,13 @@
use Symfony\Component\Config\Definition\ConfigurationInterface;

/**
* This is the class that validates and merges configuration from your app/config files.
* This is the class that validates and merges configuration from configuration files.
*
* To learn more see {@link http://symfony.com/doc/current/bundles/extension.html}
*/
class Configuration implements ConfigurationInterface
{
/**
* {@inheritdoc}
*/
public function getConfigTreeBuilder()
public function getConfigTreeBuilder(): TreeBuilder
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('beelab_tag');
Expand Down
46 changes: 8 additions & 38 deletions Entity/AbstractTaggable.php
Expand Up @@ -15,7 +15,7 @@ abstract class AbstractTaggable implements TaggableInterface
/**
* @var ArrayCollection
*
* Override this property in your Entity with definition of ManyToMany relation
* Override this property in your Entity with definition of ManyToMany relation.
*/
protected $tags;

Expand All @@ -24,77 +24,47 @@ abstract class AbstractTaggable implements TaggableInterface
*/
protected $tagsText;

/**
* Constructor.
*/
public function __construct()
{
$this->tags = new ArrayCollection();
}

/**
* {@inheritdoc}
*/
public function addTag(TagInterface $tag)
public function addTag(TagInterface $tag): void
{
$this->tags[] = $tag;

return $this;
}

/**
* {@inheritdoc}
*/
public function removeTag(TagInterface $tag)
public function removeTag(TagInterface $tag): void
{
$this->tags->removeElement($tag);
}

/**
* {@inheritdoc}
*/
public function hasTag(TagInterface $tag)
public function hasTag(TagInterface $tag): bool
{
return $this->tags->contains($tag);
}

/**
* {@inheritdoc}
*/
public function getTags()
public function getTags(): iterable
{
return $this->tags;
}

/**
* {@inheritdoc}
*/
public function getTagNames()
public function getTagNames(): array
{
return empty($this->tagsText) ? [] : array_map('trim', explode(',', $this->tagsText));
}

/**
* Set tags text
* Override this method in your Entity and update a field here.
*
* @param string $tagsText
*
* @return TaggableInterface
*/
public function setTagsText($tagsText)
public function setTagsText(?string $tagsText): void
{
$this->tagsText = $tagsText;

return $this;
}

/**
* Get tags text.
*
* @return string
*/
public function getTagsText()
public function getTagsText(): ?string
{
$this->tagsText = implode(', ', $this->tags->toArray());

Expand Down
15 changes: 5 additions & 10 deletions Listener/TagSubscriber.php
Expand Up @@ -34,15 +34,13 @@ class TagSubscriber implements EventSubscriber
protected $purge;

/**
* Constructor.
*
* @param string $tagClassName
* @param bool $purge whether to delete tags when entity is deleted
*
* @throws MappingException
* @throws \InvalidArgumentException
*/
public function __construct(string $tagClassName, $purge = false)
public function __construct(string $tagClassName, bool $purge = false)
{
if (!class_exists($tagClassName)) {
throw MappingException::nonExistingClass($tagClassName);
Expand All @@ -54,10 +52,7 @@ public function __construct(string $tagClassName, $purge = false)
$this->purge = $purge;
}

/**
* {@inheritdoc}
*/
public function getSubscribedEvents()
public function getSubscribedEvents(): array
{
return ['onFlush'];
}
Expand All @@ -68,7 +63,7 @@ public function getSubscribedEvents()
*
* @param OnFlushEventArgs $args
*/
public function onFlush(OnFlushEventArgs $args)
public function onFlush(OnFlushEventArgs $args): void
{
$this->manager = $args->getEntityManager();
$this->uow = $this->manager->getUnitOfWork();
Expand Down Expand Up @@ -97,7 +92,7 @@ public function onFlush(OnFlushEventArgs $args)
* @param TaggableInterface $entity
* @param bool $update true if entity is being updated, false otherwise
*/
protected function setTags(TaggableInterface $entity, bool $update = false)
protected function setTags(TaggableInterface $entity, bool $update = false): void
{
$tagNames = $entity->getTagNames();
if (empty($tagNames) && !$update) {
Expand Down Expand Up @@ -142,7 +137,7 @@ protected function setTags(TaggableInterface $entity, bool $update = false)
*
* @param TaggableInterface $entity
*/
protected function purgeTags(TaggableInterface $entity)
protected function purgeTags(TaggableInterface $entity): void
{
foreach ($entity->getTags() as $oldTag) {
$this->manager->remove($oldTag);
Expand Down
7 changes: 3 additions & 4 deletions Resources/doc/index.md
Expand Up @@ -108,7 +108,6 @@ Following configuration is added by recipe:
```yaml
# config/packages/beelab_tag.yaml

# BeelabTag Configuration
beelab_tag:
tag_class: App\Entity\Tag
purge: false
Expand Down Expand Up @@ -147,8 +146,8 @@ class Article implements TaggableInterface
*/
private $tags;

// note: if you generated code with SensioGeneratorBundle, you need
// to replace "Tag" with "TagInterface" where appropriate
// note: if you generated code, you need to
// replace "Tag" with "TagInterface" where appropriate

/**
* Constructor
Expand Down Expand Up @@ -193,7 +192,7 @@ class Article implements TaggableInterface
/**
* {@inheritdoc}
*/
public function getTagNames()
public function getTagNames(): array
{
return empty($this->tagsText) ? [] : array_map('trim', explode(',', $this->tagsText));
}
Expand Down
14 changes: 2 additions & 12 deletions Tag/TagInterface.php
Expand Up @@ -4,17 +4,7 @@

interface TagInterface
{
/**
* Set name.
*
* @param string $name
*/
public function setName($name);
public function setName(?string $name): void;

/**
* Get name.
*
* @return string
*/
public function getName();
public function getName(): ?string;
}
37 changes: 5 additions & 32 deletions Tag/TaggableInterface.php
Expand Up @@ -4,40 +4,13 @@

interface TaggableInterface
{
/**
* Add tag.
*
* @param TagInterface $tag
*/
public function addTag(TagInterface $tag);
public function addTag(TagInterface $tag): void;

/**
* Get names of tags.
*
* @return array
*/
public function getTagNames();
public function getTagNames(): array;

/**
* Get tags.
*
* @return array|ArrayCollection
*/
public function getTags();
public function getTags(): iterable;

/**
* Has tag.
*
* @param TagInterface $tag
*
* @return bool
*/
public function hasTag(TagInterface $tag);
public function hasTag(TagInterface $tag): bool;

/**
* Remove tag.
*
* @param TagInterface $tag
*/
public function removeTag(TagInterface $tag);
public function removeTag(TagInterface $tag): void;
}
21 changes: 3 additions & 18 deletions Test/TagStub.php
Expand Up @@ -9,31 +9,16 @@
*/
class TagStub implements TagInterface
{
/**
* Set name.
*
* @param string $name
*/
public function setName($name)
public function setName(?string $name): void
{
}

/**
* Get name.
*
* @return string
*/
public function getName()
public function getName(): ?string
{
return 'a name';
}

/**
* Get name.
*
* @return string
*/
public function getTags()
public function getTags(): ?string
{
return 'a name';
}
Expand Down
38 changes: 6 additions & 32 deletions Test/TaggableStub.php
Expand Up @@ -10,52 +10,26 @@
*/
class TaggableStub implements TaggableInterface
{
/**
* Add tag.
*
* @param TagInterface $tag
*/
public function addTag(TagInterface $tag)
public function addTag(TagInterface $tag): void
{
}

/**
* Get names of tags.
*
* @return array
*/
public function getTagNames()
public function getTagNames(): array
{
return ['foo', 'bar'];
}

/**
* Get tags.
*
* @return array|ArrayCollection
*/
public function getTags()
public function getTags(): iterable
{
return [new TagStub()];
}

/**
* Has tag.
*
* @param TagInterface $tag
*
* @return bool
*/
public function hasTag(TagInterface $tag)
public function hasTag(TagInterface $tag): bool
{
return true;
}

/**
* Remove tag.
*
* @param TagInterface $tag
*/
public function removeTag(TagInterface $tag)
public function removeTag(TagInterface $tag): void
{
}
}
7 changes: 1 addition & 6 deletions Test/TaggableStub3.php
Expand Up @@ -7,12 +7,7 @@
*/
class TaggableStub3 extends TaggableStub
{
/**
* Get names of tags.
*
* @return array
*/
public function getTagNames()
public function getTagNames(): array
{
return [];
}
Expand Down

0 comments on commit 35a5711

Please sign in to comment.