Add TaggingRule entity

This commit is contained in:
Kévin Gomez 2015-10-11 17:14:50 +02:00
parent c3510620ad
commit ac9fec610a
3 changed files with 164 additions and 16 deletions

View File

@ -76,12 +76,18 @@ class Config
*/ */
private $user; private $user;
/**
* @ORM\OneToMany(targetEntity="Wallabag\CoreBundle\Entity\TaggingRule", mappedBy="config", cascade={"remove"})
*/
private $taggingRules;
/* /*
* @param User $user * @param User $user
*/ */
public function __construct(\Wallabag\UserBundle\Entity\User $user) public function __construct(\Wallabag\UserBundle\Entity\User $user)
{ {
$this->user = $user; $this->user = $user;
$this->taggingRules = new ArrayCollection();
} }
/** /**
@ -237,4 +243,24 @@ class Config
{ {
return $this->rssLimit; return $this->rssLimit;
} }
/**
* @param TaggingRule $rule
*
* @return Config
*/
public function addTaggingRule(TaggingRule $rule)
{
$this->taggingRules[] = $rule;
return $this;
}
/**
* @return ArrayCollection<TaggingRule>
*/
public function getTaggingRules()
{
return $this->taggingRules;
}
} }

View File

@ -0,0 +1,128 @@
<?php
namespace Wallabag\CoreBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Config.
*
* @ORM\Entity(repositoryClass="Wallabag\CoreBundle\Repository\TaggingRuleRepository")
* @ORM\Table
* @ORM\Entity
*/
class TaggingRule
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @Assert\NotBlank()
* @ORM\Column(name="rule", type="string", nullable=false)
*/
private $rule;
/**
* @var array
*
* @Assert\NotBlank()
* @ORM\Column(name="tags", type="simple_array", nullable=false)
*/
private $tags = [];
/**
* @ORM\ManyToOne(targetEntity="Wallabag\CoreBundle\Entity\Config", inversedBy="taggingRules")
*/
private $config;
/**
* Get id.
*
* @return int
*/
public function getId()
{
return $this->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<string> $tags
*
* @return TaggingRule
*/
public function setTags(array $tags)
{
$this->tags = $tags;
return $this;
}
/**
* Get tags.
*
* @return array<string>
*/
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;
}
}

View File

@ -30,11 +30,11 @@ class RuleBasedTagger
$rules = $this->getRulesForUser($entry->getUser()); $rules = $this->getRulesForUser($entry->getUser());
foreach ($rules as $rule) { foreach ($rules as $rule) {
if (!$this->rulerz->satisfies($entry, $rule['rule'])) { if (!$this->rulerz->satisfies($entry, $rule->getRule())) {
continue; continue;
} }
foreach ($rule['tags'] as $label) { foreach ($rule->getTags() as $label) {
$tag = $this->getTag($entry->getUser(), $label); $tag = $this->getTag($entry->getUser(), $label);
$entry->addTag($tag); $entry->addTag($tag);
@ -62,21 +62,15 @@ class RuleBasedTagger
return $tag; return $tag;
} }
/**
* Retrieves the tagging rules for a given user.
*
* @param User $user
*
* @return array<TaggingRule>
*/
private function getRulesForUser(User $user) private function getRulesForUser(User $user)
{ {
return [ return $user->getConfig()->getTaggingRules();
[
'rule' => 'domainName = "github.com"',
'tags' => ['github'],
],
[
'rule' => 'readingTime >= 15',
'tags' => ['long reading'],
],
[
'rule' => 'readingTime <= 3 ',
'tags' => ['short reading'],
],
];
} }
} }