-
Notifications
You must be signed in to change notification settings - Fork 4
Message filtering support #14
Changes from 16 commits
fe7fc8f
252137d
d0bac36
9a9f44c
d4ec81a
9b5b8f8
05af0f0
0c30d2e
637af13
06a3cb9
feccdf5
aa1a8a5
ef1897b
5880cc5
1d66cd1
8e3c51d
d2109d2
862ed45
2aed4c3
30ee474
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| <?php | ||
|
|
||
| namespace Phergie\Irc\Plugin\React\Url\Filter; | ||
|
|
||
| use Phergie\Irc\ConnectionInterface; | ||
| use Phergie\Irc\Event\EventInterface; | ||
|
|
||
| class UrlEvent implements EventInterface | ||
| { | ||
| /** | ||
| * @var string | ||
| */ | ||
| protected $url; | ||
|
|
||
| /** | ||
| * @var array|mixed|false | ||
| */ | ||
| protected $parsedUrl; | ||
|
|
||
| /** | ||
| * @var EventInterface | ||
| */ | ||
| protected $event; | ||
|
|
||
| /** | ||
| * UrlEvent constructor. | ||
| * @param string $url | ||
| * @param EventInterface $event | ||
| */ | ||
| public function __construct($url, EventInterface $event) | ||
| { | ||
| $this->url = $url; | ||
| $this->parsedUrl = parse_url($url); | ||
| $this->event = $event; | ||
| } | ||
|
|
||
| public function setMessage($message) | ||
| { | ||
| return $this->event->setMessage($message); | ||
| } | ||
|
|
||
| public function getMessage() | ||
| { | ||
| return $this->event->getMessage(); | ||
| } | ||
|
|
||
| public function setConnection(ConnectionInterface $connection) | ||
| { | ||
| return $this->event->setConnection($connection); | ||
| } | ||
|
|
||
| public function getConnection() | ||
| { | ||
| return $this->event->getConnection(); | ||
| } | ||
|
|
||
| public function setParams(array $params) | ||
| { | ||
| return $this->event->setParams($params); | ||
| } | ||
|
|
||
| public function getParams() | ||
| { | ||
| return $this->event->getParams(); | ||
| } | ||
|
|
||
| public function setCommand($command) | ||
| { | ||
| return $this->event->setCommand($command); | ||
| } | ||
|
|
||
| public function getCommand() | ||
| { | ||
| return $this->event->getCommand(); | ||
| } | ||
|
|
||
| public function getUrlSection($section) | ||
| { | ||
| if (isset($this->parsedUrl[$section])) { | ||
| return $this->parsedUrl[$section]; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| <?php | ||
|
|
||
| namespace Phergie\Irc\Plugin\React\Url\Filter; | ||
|
|
||
| use Phergie\Irc\Event\EventInterface; | ||
| use Phergie\Irc\Plugin\React\EventFilter\FilterInterface; | ||
|
|
||
| class UrlSectionFilter implements FilterInterface | ||
| { | ||
| /** | ||
| * @var string | ||
| */ | ||
| protected $value; | ||
|
|
||
| /** | ||
| * @var string | ||
| */ | ||
| protected $section; | ||
|
|
||
| /** | ||
| * | ||
| * @param string $section | ||
| * @param string $value | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need a |
||
| */ | ||
| public function __construct($section, $value) | ||
| { | ||
| $this->section = $section; | ||
| $this->value = $value; | ||
| } | ||
|
|
||
| /** | ||
| * @param EventInterface $event | ||
| * @return bool|null | ||
| */ | ||
| public function filter(EventInterface $event) | ||
| { | ||
| if (!($event instanceof UrlEvent)) { | ||
| return null; | ||
| } | ||
|
|
||
| $section = $event->getUrlSection($this->section); | ||
| if ($section === false) { | ||
| return null; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This implies that the event should be allowed through if it doesn't contain the part of the URL that's being filtered on. Is that the desired behavior?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes if the part of the URL is missing it doesn't fall within scope of the filter. How ever in some cases it does make sense to return false if it is missing so I've added a strict bool in 2aed4c3. |
||
| } | ||
|
|
||
| $pattern = '/^' . str_replace('*', '.*', $this->value) . '$/'; | ||
| if (preg_match($pattern, $section)) { | ||
| return true; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would returning
nullhere would make more sense semantically (i.e. to imply the lack of a value)?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reasoning for using
falseis the failure of finding a value. If your preference is usingnullthat can be arranged 😄There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I think
nullmakes more sense here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done