From ac9fec610a6485b39c856d9cb7d263ce8c5f1223 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Gomez?= Date: Sun, 11 Oct 2015 17:14:50 +0200 Subject: Add TaggingRule entity --- src/Wallabag/CoreBundle/Entity/Config.php | 26 +++++ src/Wallabag/CoreBundle/Entity/TaggingRule.php | 128 +++++++++++++++++++++ src/Wallabag/CoreBundle/Helper/RuleBasedTagger.php | 26 ++--- 3 files changed, 164 insertions(+), 16 deletions(-) create mode 100644 src/Wallabag/CoreBundle/Entity/TaggingRule.php diff --git a/src/Wallabag/CoreBundle/Entity/Config.php b/src/Wallabag/CoreBundle/Entity/Config.php index b2a1915a..496fadb4 100644 --- a/src/Wallabag/CoreBundle/Entity/Config.php +++ b/src/Wallabag/CoreBundle/Entity/Config.php @@ -76,12 +76,18 @@ class Config */ private $user; + /** + * @ORM\OneToMany(targetEntity="Wallabag\CoreBundle\Entity\TaggingRule", mappedBy="config", cascade={"remove"}) + */ + private $taggingRules; + /* * @param User $user */ public function __construct(\Wallabag\UserBundle\Entity\User $user) { $this->user = $user; + $this->taggingRules = new ArrayCollection(); } /** @@ -237,4 +243,24 @@ class Config { return $this->rssLimit; } + + /** + * @param TaggingRule $rule + * + * @return Config + */ + public function addTaggingRule(TaggingRule $rule) + { + $this->taggingRules[] = $rule; + + return $this; + } + + /** + * @return ArrayCollection + */ + public function getTaggingRules() + { + return $this->taggingRules; + } } diff --git a/src/Wallabag/CoreBundle/Entity/TaggingRule.php b/src/Wallabag/CoreBundle/Entity/TaggingRule.php new file mode 100644 index 00000000..6d03a34d --- /dev/null +++ b/src/Wallabag/CoreBundle/Entity/TaggingRule.php @@ -0,0 +1,128 @@ +id; + } + + /** + * Set rule. + * + * @param string $rule + * + * @return TaggingRule + */ + public function setRule($rule) + { + $this->rule = $rule; + + return $this; + } + + /** + * Get rule. + * + * @return string + */ + public function getRule() + { + return $this->rule; + } + + /** + * Set tags. + * + * @param array $tags + * + * @return TaggingRule + */ + public function setTags(array $tags) + { + $this->tags = $tags; + + return $this; + } + + /** + * Get tags. + * + * @return array + */ + public function getTags() + { + return $this->tags; + } + + /** + * Set config. + * + * @param Config $config + * + * @return TaggingRule + */ + public function setConfig(Config $config) + { + $this->config = $config; + + return $this; + } + + /** + * Get config. + * + * @return Config + */ + public function getConfig() + { + return $this->config; + } +} diff --git a/src/Wallabag/CoreBundle/Helper/RuleBasedTagger.php b/src/Wallabag/CoreBundle/Helper/RuleBasedTagger.php index 012450b6..bb933779 100644 --- a/src/Wallabag/CoreBundle/Helper/RuleBasedTagger.php +++ b/src/Wallabag/CoreBundle/Helper/RuleBasedTagger.php @@ -30,11 +30,11 @@ class RuleBasedTagger $rules = $this->getRulesForUser($entry->getUser()); foreach ($rules as $rule) { - if (!$this->rulerz->satisfies($entry, $rule['rule'])) { + if (!$this->rulerz->satisfies($entry, $rule->getRule())) { continue; } - foreach ($rule['tags'] as $label) { + foreach ($rule->getTags() as $label) { $tag = $this->getTag($entry->getUser(), $label); $entry->addTag($tag); @@ -62,21 +62,15 @@ class RuleBasedTagger return $tag; } + /** + * Retrieves the tagging rules for a given user. + * + * @param User $user + * + * @return array + */ private function getRulesForUser(User $user) { - return [ - [ - 'rule' => 'domainName = "github.com"', - 'tags' => ['github'], - ], - [ - 'rule' => 'readingTime >= 15', - 'tags' => ['long reading'], - ], - [ - 'rule' => 'readingTime <= 3 ', - 'tags' => ['short reading'], - ], - ]; + return $user->getConfig()->getTaggingRules(); } } -- cgit v1.2.3