aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Wallabag/CoreBundle/Entity/Config.php26
-rw-r--r--src/Wallabag/CoreBundle/Entity/TaggingRule.php128
-rw-r--r--src/Wallabag/CoreBundle/Helper/RuleBasedTagger.php26
3 files changed, 164 insertions, 16 deletions
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
76 */ 76 */
77 private $user; 77 private $user;
78 78
79 /**
80 * @ORM\OneToMany(targetEntity="Wallabag\CoreBundle\Entity\TaggingRule", mappedBy="config", cascade={"remove"})
81 */
82 private $taggingRules;
83
79 /* 84 /*
80 * @param User $user 85 * @param User $user
81 */ 86 */
82 public function __construct(\Wallabag\UserBundle\Entity\User $user) 87 public function __construct(\Wallabag\UserBundle\Entity\User $user)
83 { 88 {
84 $this->user = $user; 89 $this->user = $user;
90 $this->taggingRules = new ArrayCollection();
85 } 91 }
86 92
87 /** 93 /**
@@ -237,4 +243,24 @@ class Config
237 { 243 {
238 return $this->rssLimit; 244 return $this->rssLimit;
239 } 245 }
246
247 /**
248 * @param TaggingRule $rule
249 *
250 * @return Config
251 */
252 public function addTaggingRule(TaggingRule $rule)
253 {
254 $this->taggingRules[] = $rule;
255
256 return $this;
257 }
258
259 /**
260 * @return ArrayCollection<TaggingRule>
261 */
262 public function getTaggingRules()
263 {
264 return $this->taggingRules;
265 }
240} 266}
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 @@
1<?php
2
3namespace Wallabag\CoreBundle\Entity;
4
5use Doctrine\ORM\Mapping as ORM;
6use Symfony\Component\Validator\Constraints as Assert;
7
8/**
9 * Config.
10 *
11 * @ORM\Entity(repositoryClass="Wallabag\CoreBundle\Repository\TaggingRuleRepository")
12 * @ORM\Table
13 * @ORM\Entity
14 */
15class TaggingRule
16{
17 /**
18 * @var int
19 *
20 * @ORM\Column(name="id", type="integer")
21 * @ORM\Id
22 * @ORM\GeneratedValue(strategy="AUTO")
23 */
24 private $id;
25
26 /**
27 * @var string
28 *
29 * @Assert\NotBlank()
30 * @ORM\Column(name="rule", type="string", nullable=false)
31 */
32 private $rule;
33
34 /**
35 * @var array
36 *
37 * @Assert\NotBlank()
38 * @ORM\Column(name="tags", type="simple_array", nullable=false)
39 */
40 private $tags = [];
41
42 /**
43 * @ORM\ManyToOne(targetEntity="Wallabag\CoreBundle\Entity\Config", inversedBy="taggingRules")
44 */
45 private $config;
46
47 /**
48 * Get id.
49 *
50 * @return int
51 */
52 public function getId()
53 {
54 return $this->id;
55 }
56
57 /**
58 * Set rule.
59 *
60 * @param string $rule
61 *
62 * @return TaggingRule
63 */
64 public function setRule($rule)
65 {
66 $this->rule = $rule;
67
68 return $this;
69 }
70
71 /**
72 * Get rule.
73 *
74 * @return string
75 */
76 public function getRule()
77 {
78 return $this->rule;
79 }
80
81 /**
82 * Set tags.
83 *
84 * @param array<string> $tags
85 *
86 * @return TaggingRule
87 */
88 public function setTags(array $tags)
89 {
90 $this->tags = $tags;
91
92 return $this;
93 }
94
95 /**
96 * Get tags.
97 *
98 * @return array<string>
99 */
100 public function getTags()
101 {
102 return $this->tags;
103 }
104
105 /**
106 * Set config.
107 *
108 * @param Config $config
109 *
110 * @return TaggingRule
111 */
112 public function setConfig(Config $config)
113 {
114 $this->config = $config;
115
116 return $this;
117 }
118
119 /**
120 * Get config.
121 *
122 * @return Config
123 */
124 public function getConfig()
125 {
126 return $this->config;
127 }
128}
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
30 $rules = $this->getRulesForUser($entry->getUser()); 30 $rules = $this->getRulesForUser($entry->getUser());
31 31
32 foreach ($rules as $rule) { 32 foreach ($rules as $rule) {
33 if (!$this->rulerz->satisfies($entry, $rule['rule'])) { 33 if (!$this->rulerz->satisfies($entry, $rule->getRule())) {
34 continue; 34 continue;
35 } 35 }
36 36
37 foreach ($rule['tags'] as $label) { 37 foreach ($rule->getTags() as $label) {
38 $tag = $this->getTag($entry->getUser(), $label); 38 $tag = $this->getTag($entry->getUser(), $label);
39 39
40 $entry->addTag($tag); 40 $entry->addTag($tag);
@@ -62,21 +62,15 @@ class RuleBasedTagger
62 return $tag; 62 return $tag;
63 } 63 }
64 64
65 /**
66 * Retrieves the tagging rules for a given user.
67 *
68 * @param User $user
69 *
70 * @return array<TaggingRule>
71 */
65 private function getRulesForUser(User $user) 72 private function getRulesForUser(User $user)
66 { 73 {
67 return [ 74 return $user->getConfig()->getTaggingRules();
68 [
69 'rule' => 'domainName = "github.com"',
70 'tags' => ['github'],
71 ],
72 [
73 'rule' => 'readingTime >= 15',
74 'tags' => ['long reading'],
75 ],
76 [
77 'rule' => 'readingTime <= 3 ',
78 'tags' => ['short reading'],
79 ],
80 ];
81 } 75 }
82} 76}