| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
namespace Liuch\DmarcSrg\Sources; |
| 33 |
|
| 34 |
use Liuch\DmarcSrg\Exception\LogicException; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
class SourceAction |
| 40 |
{ |
| 41 |
public const ACTION_SEEN = 1; |
| 42 |
public const ACTION_MOVE = 2; |
| 43 |
public const ACTION_DELETE = 3; |
| 44 |
public const FLAG_BASENAME = 1; |
| 45 |
|
| 46 |
private $valid = false; |
| 47 |
private $type = 0; |
| 48 |
private $param = null; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
private function __construct(string $action) |
| 59 |
{ |
| 60 |
if (($delim_offset = mb_strpos($action, ':')) === false) { |
| 61 |
$name = $action; |
| 62 |
$param = null; |
| 63 |
} else { |
| 64 |
$name = mb_substr($action, 0, $delim_offset); |
| 65 |
$param = mb_substr($action, $delim_offset + 1); |
| 66 |
} |
| 67 |
switch ($name) { |
| 68 |
case 'mark_seen': |
| 69 |
$this->type = self::ACTION_SEEN; |
| 70 |
if (!empty($param)) { |
| 71 |
return; |
| 72 |
} |
| 73 |
break; |
| 74 |
case 'move_to': |
| 75 |
$this->type = self::ACTION_MOVE; |
| 76 |
if (empty($param)) { |
| 77 |
return; |
| 78 |
} |
| 79 |
break; |
| 80 |
case 'delete': |
| 81 |
$this->type = self::ACTION_DELETE; |
| 82 |
if (!empty($param)) { |
| 83 |
return; |
| 84 |
} |
| 85 |
break; |
| 86 |
default: |
| 87 |
return; |
| 88 |
} |
| 89 |
$this->param = $param; |
| 90 |
$this->valid = true; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
public function __get(string $name) |
| 101 |
{ |
| 102 |
if (in_array($name, [ 'type', 'param' ])) { |
| 103 |
return $this->$name; |
| 104 |
} |
| 105 |
throw new LogicException('Undefined property: ' . $name); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
public static function fromSetting($setting, int $flags, string $default): array |
| 118 |
{ |
| 119 |
if (gettype($setting) !== 'array') { |
| 120 |
$setting = [ $setting ]; |
| 121 |
} |
| 122 |
$tmap = []; |
| 123 |
$list = []; |
| 124 |
foreach ($setting as $it) { |
| 125 |
if (gettype($it) === 'string') { |
| 126 |
$sa = new self($it); |
| 127 |
if ($sa->valid && !isset($tmap[$sa->type])) { |
| 128 |
if (($flags & self::FLAG_BASENAME) && !self::checkBasename($sa)) { |
| 129 |
continue; |
| 130 |
} |
| 131 |
$list[] = $sa; |
| 132 |
$tmap[$sa->type] = true; |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
if (count($list) === 0) { |
| 137 |
$sa = new self($default); |
| 138 |
if ($sa->valid) { |
| 139 |
$list[] = $sa; |
| 140 |
} |
| 141 |
} |
| 142 |
return $list; |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
private static function checkBasename($sa): bool |
| 153 |
{ |
| 154 |
return ($sa->type !== self::ACTION_MOVE || basename($sa->param) === $sa->param); |
| 155 |
} |
| 156 |
} |