1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\kw_rules\Rules\File; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use kalanis\kw_rules\Interfaces\IRuleFactory; |
7
|
|
|
use kalanis\kw_rules\Interfaces\IRules; |
8
|
|
|
use kalanis\kw_rules\Exceptions\RuleException; |
9
|
|
|
use ReflectionClass; |
10
|
|
|
use ReflectionException; |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class Factory |
15
|
|
|
* @package kalanis\kw_rules\Rules\File |
16
|
|
|
* Factory for getting rules for files |
17
|
|
|
*/ |
18
|
|
|
class Factory implements IRuleFactory |
19
|
|
|
{ |
20
|
|
|
/** @var array<string, class-string<AFileRule>> */ |
|
|
|
|
21
|
|
|
protected array $map = [ |
22
|
|
|
IRules::FILE_EXISTS => FileExists::class, |
23
|
|
|
IRules::FILE_SENT => FileSent::class, |
24
|
|
|
IRules::FILE_RECEIVED => FileReceived::class, |
25
|
|
|
IRules::FILE_MAX_SIZE => FileMaxSize::class, |
26
|
|
|
IRules::FILE_MIMETYPE_EQUALS => FileMimeEquals::class, |
27
|
|
|
IRules::FILE_MIMETYPE_IN_LIST => FileMimeList::class, |
28
|
|
|
IRules::IS_IMAGE => ImageIs::class, |
29
|
|
|
IRules::IMAGE_DIMENSION_EQUALS => ImageSizeEquals::class, |
30
|
|
|
IRules::IMAGE_DIMENSION_IN_LIST => ImageSizeList::class, |
31
|
|
|
IRules::IMAGE_MAX_DIMENSION => ImageSizeMax::class, |
32
|
|
|
IRules::IMAGE_MIN_DIMENSION => ImageSizeMin::class, |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param string $ruleName |
37
|
|
|
* @throws RuleException |
38
|
|
|
* @return AFileRule |
39
|
|
|
*/ |
40
|
54 |
|
public function getRule(string $ruleName): AFileRule |
41
|
|
|
{ |
42
|
54 |
|
if (isset($this->map[$ruleName])) { |
43
|
15 |
|
$rule = $this->map[$ruleName]; |
44
|
|
|
try { |
45
|
15 |
|
$ref = new ReflectionClass($rule); |
46
|
14 |
|
$class = $ref->newInstance(); |
47
|
14 |
|
if ($class instanceof AFileRule) { |
48
|
14 |
|
return $class; |
49
|
|
|
} |
50
|
1 |
|
} catch (ReflectionException $ex) { |
51
|
1 |
|
throw new RuleException($ex->getMessage(), $ex->getCode(), $ex); |
52
|
|
|
} |
53
|
|
|
} |
54
|
41 |
|
throw new RuleException(sprintf('Unknown rule %s', $ruleName)); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|