Skip to content

Commit a4b33fe

Browse files
committed
Updated actions to use assoc array for params
1 parent c354d91 commit a4b33fe

21 files changed

+353
-276
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace PhpSieveManager\Exceptions;
4+
5+
class FilterActionParamException extends \Exception
6+
{
7+
8+
}

src/Filters/Actions/AddHeaderFilterAction.php

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,32 @@
55
/**
66
* Please refer to https://datatracker.ietf.org/doc/html/rfc5293#section-4
77
*/
8-
class AddHeaderFilterAction implements FilterAction
8+
class AddHeaderFilterAction extends BaseSieveAction
99
{
10-
private $last;
11-
private $fieldName;
12-
private $value;
13-
1410
public $require = ['editheader'];
1511

16-
/**
17-
* @param string $fieldName - The field name of the header to add
18-
* @param string $value - The value of the header to add
19-
* @param bool $last - Flag to indicate if the header should be added at the end
20-
*/
21-
public function __construct($fieldName, $value, $last = false) {
22-
$this->last = $last;
23-
$this->fieldName = $fieldName;
24-
$this->value = $value;
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+
];
2523
}
2624

2725
/**
2826
* @return string
2927
*/
3028
public function parse() {
3129
$script = "addheader";
32-
if ($this->last) {
30+
if (!empty($this->params['last'])) {
3331
$script .= " :last";
3432
}
35-
$script .= " \"{$this->fieldName}\" \"{$this->value}\";\n";
33+
$script .= " \"{$this->params['field-name']}\" \"{$this->params['value']}\";\n";
3634
return $script;
3735
}
3836
}
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+
}

src/Filters/Actions/BaseFlagFilterAction.php

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,34 @@
22

33
namespace PhpSieveManager\Filters\Actions;
44

5-
abstract class BaseFlagFilterAction implements FilterAction
5+
abstract class BaseFlagFilterAction extends BaseSieveAction
66
{
77
public $require = ['imap4flags'];
88

9-
private $variableName;
10-
private $listOfFlags;
11-
12-
/**
13-
* @param array $listOfFlags - List of flags
14-
* @param string $variableName - Variable name
15-
*/
16-
public function __construct($listOfFlags, $variableName = null) {
17-
$this->listOfFlags = $listOfFlags;
18-
$this->variableName = $variableName;
19-
if ($variableName) {
20-
$this->require[] = 'variables';
21-
}
9+
public function getRequiredParams()
10+
{
11+
return ['list-of-flags'];
2212
}
2313

24-
abstract public function getScriptName();
14+
protected function getParamTypes() {
15+
return [
16+
'variablename' => 'string',
17+
'list-of-flags' => 'string-list'
18+
];
19+
}
2520

2621
/**
2722
* @return string
2823
*/
2924
public function parse() {
3025
$script = $this->getScriptName();
31-
if ($this->variableName) {
32-
$script .= "\"{$this->variableName}\"";
26+
if (!empty($this->params['variablename'])) {
27+
$script .= "\"{$this->params['variablename']}\"";
3328
}
34-
$script .= " [" . implode(', ', array_map(function($flag) { return "\"$flag\""; }, $this->listOfFlags)) . "];\n";
29+
$script .= " [" . implode(', ', array_map(function($flag) { return "\"$flag\""; }, $this->params['list-of-flags'])) . "];\n";
3530

3631
return $script;
3732
}
33+
34+
abstract public function getScriptName();
3835
}
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: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,27 @@
55
/**
66
* Please refer to https://www.rfc-editor.org/rfc/rfc6558.html
77
*/
8-
class ConvertFilterAction implements FilterAction
8+
class ConvertFilterAction extends BaseSieveAction
99
{
10-
private $fromMediaType;
11-
private $toMediaType;
12-
private $transcodingParams;
13-
1410
public $require = ['convert'];
1511

16-
/**
17-
* @param string $fromMediaType - The source media type
18-
* @param string $toMediaType - The target media type
19-
* @param array $transcodingParams - Parameters for transcoding
20-
*/
21-
public function __construct($fromMediaType, $toMediaType, array $transcodingParams) {
22-
$this->fromMediaType = $fromMediaType;
23-
$this->toMediaType = $toMediaType;
24-
$this->transcodingParams = $transcodingParams;
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+
];
2523
}
2624

2725
/**
2826
* @return string
2927
*/
3028
public function parse() {
31-
return "convert \"{$this->fromMediaType}\" \"{$this->toMediaType}\" [" . implode(', ', array_map(function($param) { return "\"$param\""; }, $this->transcodingParams)) . "];\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";
3230
}
3331
}

src/Filters/Actions/DeleteHeaderFilterAction.php

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,54 +5,46 @@
55
/**
66
* Please refer to https://datatracker.ietf.org/doc/html/rfc5293#section-5
77
*/
8-
class DeleteHeaderFilterAction implements FilterAction
8+
class DeleteHeaderFilterAction extends BaseSieveAction
99
{
10-
private $index;
11-
private $last;
12-
private $comparator;
13-
private $matchType;
14-
private $fieldName;
15-
private $valuePatterns;
16-
1710
public $require = ['editheader'];
1811

19-
/**
20-
* @param string $fieldName - The field name of the header to delete
21-
* @param int|null $index - Index of the header to delete
22-
* @param bool $last - Flag to indicate if the last occurrence should be deleted
23-
* @param string|null $comparator - Comparator for matching
24-
* @param string|null $matchType - Type of match
25-
* @param array|null $valuePatterns - Patterns to match the header value
26-
*/
27-
public function __construct($fieldName, $index = null, $last = false, $comparator = null, $matchType = null, $valuePatterns = []) {
28-
$this->index = $index;
29-
$this->last = $last;
30-
$this->comparator = $comparator;
31-
$this->matchType = $matchType;
32-
$this->fieldName = $fieldName;
33-
$this->valuePatterns = $valuePatterns;
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+
];
3426
}
3527

3628
/**
3729
* @return string
3830
*/
3931
public function parse() {
4032
$script = "deleteheader";
41-
if ($this->index) {
42-
$script .= " :index {$this->index}";
43-
if ($this->last) {
33+
if (!empty($this->params['index'])) {
34+
$script .= " :index {$this->params['index']}";
35+
if (!empty($this->params['last'])) {
4436
$script .= " :last";
4537
}
4638
}
47-
if ($this->comparator) {
48-
$script .= " {$this->comparator}";
39+
if (!empty($this->params['comparator'])) {
40+
$script .= " {$this->params['comparator']}";
4941
}
50-
if ($this->matchType) {
51-
$script .= " {$this->matchType}";
42+
if (!empty($this->params['match-type'])) {
43+
$script .= " {$this->params['match-type']}";
5244
}
53-
$script .= " \"{$this->fieldName}\"";
54-
if ($this->valuePatterns) {
55-
$script .= " [\"" . implode('", "', $this->valuePatterns) . "\"]";
45+
$script .= " \"{$this->params['field-name']}\"";
46+
if (!empty($this->params['value-patterns'])) {
47+
$script .= " [\"" . implode('", "', $this->params['value-patterns']) . "\"]";
5648
}
5749
$script .= ";\n";
5850
return $script;

src/Filters/Actions/DiscardFilterAction.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,20 @@
22

33
namespace PhpSieveManager\Filters\Actions;
44

5-
class DiscardFilterAction implements FilterAction
5+
class DiscardFilterAction extends BaseSieveAction
66
{
77
/**
88
* @return string
99
*/
1010
public function parse() {
1111
return "discard;\n";
1212
}
13+
14+
public function getRequiredParams() {
15+
return [];
16+
}
17+
18+
protected function getParamTypes() {
19+
return [];
20+
}
1321
}

src/Filters/Actions/EncloseFilterAction.php

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,21 @@
55
/**
66
* Please refer to https://www.rfc-editor.org/rfc/rfc5703.html#section-6
77
*/
8-
class EncloseFilterAction implements FilterAction
8+
class EncloseFilterAction extends BaseSieveAction
99
{
10-
private $subject;
11-
private $headers;
12-
private $content;
13-
1410
public $require = ['enclose'];
1511

16-
/**
17-
* @param string $content - The content to enclose
18-
* @param string $subject - The subject of the enclosed message
19-
* @param array $headers - List of headers
20-
*/
21-
public function __construct($content, $subject, $headers) {
22-
$this->subject = $subject;
23-
$this->headers = $headers;
24-
$this->content = $content;
12+
protected function getRequiredParams()
13+
{
14+
return array_keys($this->getParamTypes());
15+
}
16+
17+
protected function getParamTypes() {
18+
return [
19+
'subject' => 'string',
20+
'headers' => 'string-list',
21+
'content' => 'string'
22+
];
2523
}
2624

2725
/**

0 commit comments

Comments
 (0)