Skip to content

Commit 3715c1c

Browse files
authored
Merge pull request #14 from josaphatim/revamp-actions
Revamp actions to support optional and required parameters
2 parents c978e03 + a4b33fe commit 3715c1c

24 files changed

+439
-248
lines changed

src/Filters/Actions/AddFlagFilterAction.php

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,10 @@
22

33
namespace PhpSieveManager\Filters\Actions;
44

5-
class AddFlagFilterAction implements FilterAction
5+
class AddFlagFilterAction extends BaseFlagFilterAction
66
{
7-
private $params;
8-
9-
/**
10-
* @param array $params
11-
* @throws FilterActionParamException
12-
*/
13-
public function __construct(array $params = []) {
14-
if (count($params) != 1) {
15-
throw new FilterActionParamException("AddFlagAction expect one parameter");
16-
}
17-
$this->params = $params;
18-
}
19-
20-
/**
21-
* @return string
22-
*/
23-
public function parse() {
24-
return 'addflag "'.$this->params[0].'";'."\n";
7+
public function getScriptName()
8+
{
9+
return 'addflag';
2510
}
2611
}

src/Filters/Actions/AddHeaderFilterAction.php

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,35 @@
22

33
namespace PhpSieveManager\Filters\Actions;
44

5-
use PhpSieveManager\Exceptions\FilterActionParamException;
6-
75
/**
86
* Please refer to https://datatracker.ietf.org/doc/html/rfc5293#section-4
97
*/
10-
class AddHeaderFilterAction implements FilterAction
8+
class AddHeaderFilterAction extends BaseSieveAction
119
{
12-
private $params;
10+
public $require = ['editheader'];
1311

14-
/**
15-
* @param array $params
16-
* @throws FilterActionParamException
17-
*/
18-
public function __construct(array $params = []) {
19-
if (count($params) != 2) {
20-
throw new FilterActionParamException("AddHeaderFilterAction expect two parameters");
21-
}
22-
$this->params = $params;
12+
public function getRequiredParams()
13+
{
14+
return ['field-name', 'value'];
15+
}
16+
17+
protected function getParamTypes() {
18+
return [
19+
'last' => 'bool',
20+
'field-name' => 'string',
21+
'value' => 'string'
22+
];
2323
}
2424

2525
/**
2626
* @return string
2727
*/
2828
public function parse() {
29-
return 'addheader "'.$this->params[0].'" "'.$this->params[1].'";'."\n";
29+
$script = "addheader";
30+
if (!empty($this->params['last'])) {
31+
$script .= " :last";
32+
}
33+
$script .= " \"{$this->params['field-name']}\" \"{$this->params['value']}\";\n";
34+
return $script;
3035
}
3136
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
namespace PhpSieveManager\Filters\Actions;
4+
5+
use PhpSieveManager\Exceptions\FilterActionParamException;
6+
7+
abstract class BaseSieveAction implements FilterAction {
8+
protected array $params;
9+
10+
public function __construct(array $params = []) {
11+
$this->params = $params;
12+
$this->validateParams();
13+
}
14+
15+
protected function validateParams() {
16+
foreach ($this->getRequiredParams() as $param) {
17+
if (!isset($this->params[$param])) {
18+
throw new FilterActionParamException("Missing required parameter: $param");
19+
}
20+
}
21+
$this->validateTypes();
22+
}
23+
24+
protected function validateTypes() {
25+
$paramTypes = $this->getParamTypes();
26+
foreach ($this->params as $key => $value) {
27+
if (isset($paramTypes[$key]) && !$this->isValidType($key, $value)) {
28+
throw new FilterActionParamException("Invalid type for parameter: $key. Expected " . $value);
29+
}
30+
}
31+
}
32+
33+
/**
34+
* @param mixed $value
35+
* @param string $type
36+
*
37+
* @return bool
38+
*/
39+
protected function isValidType($value, $type) {
40+
switch ($type) {
41+
case 'string':
42+
return is_string($value);
43+
case 'int':
44+
return is_int($value);
45+
case 'float':
46+
return is_float($value);
47+
case 'bool':
48+
return is_bool($value);
49+
case 'array':
50+
return is_array($value);
51+
case 'string-list':
52+
return is_array($value) && array_reduce($value, function($carry, $item) { return $carry && is_string($item); }, true);
53+
default:
54+
return false;
55+
}
56+
}
57+
58+
/**
59+
* @return array
60+
*/
61+
abstract protected function getRequiredParams();
62+
63+
/**
64+
* @return array
65+
*/
66+
abstract protected function getParamTypes();
67+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace PhpSieveManager\Filters\Actions;
4+
5+
abstract class BaseFlagFilterAction extends BaseSieveAction
6+
{
7+
public $require = ['imap4flags'];
8+
9+
public function getRequiredParams()
10+
{
11+
return ['list-of-flags'];
12+
}
13+
14+
protected function getParamTypes() {
15+
return [
16+
'variablename' => 'string',
17+
'list-of-flags' => 'string-list'
18+
];
19+
}
20+
21+
/**
22+
* @return string
23+
*/
24+
public function parse() {
25+
$script = $this->getScriptName();
26+
if (!empty($this->params['variablename'])) {
27+
$script .= "\"{$this->params['variablename']}\"";
28+
}
29+
$script .= " [" . implode(', ', array_map(function($flag) { return "\"$flag\""; }, $this->params['list-of-flags'])) . "];\n";
30+
31+
return $script;
32+
}
33+
34+
abstract public function getScriptName();
35+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace PhpSieveManager\Filters\Actions;
4+
5+
abstract class BaseRejectFilterAction extends BaseSieveAction
6+
{
7+
public $require = [];
8+
9+
protected function getRequiredParams()
10+
{
11+
return ['reason'];
12+
}
13+
14+
protected function getParamTypes() {
15+
return ['reason' => 'string'];
16+
}
17+
18+
/**
19+
* @return string
20+
*/
21+
public function parse() {
22+
$type = $this->getType();
23+
$this->require[] = $type;
24+
return "{$type} \"{$type}\";\n";
25+
}
26+
27+
abstract protected function getType();
28+
}

src/Filters/Actions/ConvertFilterAction.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,30 @@
22

33
namespace PhpSieveManager\Filters\Actions;
44

5-
use PhpSieveManager\Exceptions\FilterActionParamException;
6-
75
/**
86
* Please refer to https://www.rfc-editor.org/rfc/rfc6558.html
97
*/
10-
class ConvertFilterAction implements FilterAction
8+
class ConvertFilterAction extends BaseSieveAction
119
{
12-
private $params;
10+
public $require = ['convert'];
1311

14-
/**
15-
* @param array $params
16-
* @throws FilterActionParamException
17-
*/
18-
public function __construct(array $params = []) {
19-
if (count($params) != 3) {
20-
throw new FilterActionParamException("ConvertFilterAction expect three parameters");
21-
}
22-
$this->params = $params;
12+
protected function getRequiredParams()
13+
{
14+
return array_keys($this->getParamTypes());
15+
}
16+
17+
protected function getParamTypes() {
18+
return [
19+
'quoted-from-media-type' => 'string',
20+
'quoted-to-media-type' => 'string',
21+
'transcoding-params' => 'string-list'
22+
];
2323
}
2424

2525
/**
2626
* @return string
2727
*/
2828
public function parse() {
29-
return 'convert "'.$this->params[0].'" "'.$this->params[1].'" ["'.implode('","', $this->params[2]).'"];'."\n";
29+
return "convert \"{$this->params['quoted-from-media-type']}\" \"{$this->params['quoted-to-media-type']}\" [" . implode(', ', array_map(function($param) { return "\"$param\""; }, $this->params['transcoding-params'])) . "];\n";
3030
}
3131
}

src/Filters/Actions/DeleteHeaderFilterAction.php

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,51 @@
22

33
namespace PhpSieveManager\Filters\Actions;
44

5-
use PhpSieveManager\Exceptions\FilterActionParamException;
6-
75
/**
86
* Please refer to https://datatracker.ietf.org/doc/html/rfc5293#section-5
97
*/
10-
class DeleteHeaderFilterAction implements FilterAction
8+
class DeleteHeaderFilterAction extends BaseSieveAction
119
{
12-
private $params;
10+
public $require = ['editheader'];
1311

14-
/**
15-
* @param array $params
16-
* @throws FilterActionParamException
17-
*/
18-
public function __construct(array $params = []) {
19-
if (count($params) != 1) {
20-
throw new FilterActionParamException("DeleteHeaderFilterAction expect one parameters");
21-
}
22-
$this->params = $params;
12+
protected function getRequiredParams()
13+
{
14+
return ['field-name'];
15+
}
16+
17+
protected function getParamTypes() {
18+
return [
19+
'index' => 'bool',
20+
'last' => 'bool',
21+
'comparator' => 'string',
22+
'match-type' => 'string',
23+
'field-name' => 'string',
24+
'value-patterns' => 'string-list'
25+
];
2326
}
2427

2528
/**
2629
* @return string
2730
*/
2831
public function parse() {
29-
return 'deleteheader "'.$this->params[0].'";'."\n";
32+
$script = "deleteheader";
33+
if (!empty($this->params['index'])) {
34+
$script .= " :index {$this->params['index']}";
35+
if (!empty($this->params['last'])) {
36+
$script .= " :last";
37+
}
38+
}
39+
if (!empty($this->params['comparator'])) {
40+
$script .= " {$this->params['comparator']}";
41+
}
42+
if (!empty($this->params['match-type'])) {
43+
$script .= " {$this->params['match-type']}";
44+
}
45+
$script .= " \"{$this->params['field-name']}\"";
46+
if (!empty($this->params['value-patterns'])) {
47+
$script .= " [\"" . implode('", "', $this->params['value-patterns']) . "\"]";
48+
}
49+
$script .= ";\n";
50+
return $script;
3051
}
3152
}

src/Filters/Actions/DiscardFilterAction.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,20 @@
22

33
namespace PhpSieveManager\Filters\Actions;
44

5-
class DiscardFilterAction implements FilterAction
5+
class DiscardFilterAction extends BaseSieveAction
66
{
7-
/**
8-
* @param array $params
9-
*/
10-
public function __construct(array $params = []) {}
11-
127
/**
138
* @return string
149
*/
1510
public function parse() {
16-
return "discard;"."\n";
11+
return "discard;\n";
12+
}
13+
14+
public function getRequiredParams() {
15+
return [];
16+
}
17+
18+
protected function getParamTypes() {
19+
return [];
1720
}
1821
}

0 commit comments

Comments
 (0)