1
|
|
|
<?php |
2
|
|
|
|
|
|
|
|
3
|
|
|
namespace BristolSU\ControlDB\Export\Formatter; |
4
|
|
|
|
5
|
|
|
use BristolSU\ControlDB\Contracts\Models\Group; |
6
|
|
|
use BristolSU\ControlDB\Contracts\Models\Position; |
7
|
|
|
use BristolSU\ControlDB\Contracts\Models\Role; |
8
|
|
|
use BristolSU\ControlDB\Contracts\Models\Tags\GroupTag; |
9
|
|
|
use BristolSU\ControlDB\Contracts\Models\Tags\GroupTagCategory; |
10
|
|
|
use BristolSU\ControlDB\Contracts\Models\Tags\PositionTag; |
11
|
|
|
use BristolSU\ControlDB\Contracts\Models\Tags\PositionTagCategory; |
12
|
|
|
use BristolSU\ControlDB\Contracts\Models\Tags\RoleTag; |
13
|
|
|
use BristolSU\ControlDB\Contracts\Models\Tags\RoleTagCategory; |
14
|
|
|
use BristolSU\ControlDB\Contracts\Models\Tags\UserTag; |
15
|
|
|
use BristolSU\ControlDB\Contracts\Models\Tags\UserTagCategory; |
16
|
|
|
use BristolSU\ControlDB\Contracts\Models\User; |
17
|
|
|
use BristolSU\ControlDB\Export\FormattedItem; |
18
|
|
|
|
19
|
|
|
abstract class Formatter |
|
|
|
|
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
public const USERS = User::class; |
23
|
|
|
|
24
|
|
|
public const GROUPS = Group::class; |
25
|
|
|
|
26
|
|
|
public const ROLES = Role::class; |
27
|
|
|
|
28
|
|
|
public const POSITIONS = Position::class; |
29
|
|
|
|
30
|
|
|
public const USERTAGS = UserTag::class; |
31
|
|
|
|
32
|
|
|
public const GROUPTAGS = GroupTag::class; |
33
|
|
|
|
34
|
|
|
public const ROLETAGS = RoleTag::class; |
35
|
|
|
|
36
|
|
|
public const POSITIONTAGS = PositionTag::class; |
37
|
|
|
|
38
|
|
|
public const USERTAGCATEGORIES = UserTagCategory::class; |
39
|
|
|
|
40
|
|
|
public const GROUPTAGCATEGORIES = GroupTagCategory::class; |
41
|
|
|
|
42
|
|
|
public const ROLETAGCATEGORIES = RoleTagCategory::class; |
43
|
|
|
|
44
|
|
|
public const POSITIONTAGCATEGORIES = PositionTagCategory::class; |
45
|
|
|
|
46
|
|
|
public const ALL = 'all'; |
47
|
|
|
/** |
|
|
|
|
48
|
|
|
* @var array |
49
|
|
|
*/ |
50
|
|
|
protected $config; |
51
|
|
|
|
52
|
20 |
|
public function __construct(array $config) |
|
|
|
|
53
|
|
|
{ |
54
|
20 |
|
$this->config = $config; |
55
|
20 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
|
|
|
|
58
|
|
|
* @param string $key |
|
|
|
|
59
|
|
|
* @param null $default |
|
|
|
|
60
|
|
|
* |
61
|
|
|
* @return mixed|null |
62
|
|
|
*/ |
63
|
8 |
|
protected function config(string $key, $default = null) |
64
|
|
|
{ |
65
|
8 |
|
if(array_key_exists($key, $this->config)) { |
|
|
|
|
66
|
7 |
|
return $this->config[$key]; |
67
|
|
|
} |
68
|
1 |
|
return $default; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
|
|
|
|
72
|
|
|
* @param array|FormattedItem[] $items Items ready to format |
|
|
|
|
73
|
|
|
* @return array |
|
|
|
|
74
|
|
|
*/ |
75
|
11 |
|
public function format($items) |
76
|
|
|
{ |
77
|
|
|
return array_map(function($item) { |
|
|
|
|
78
|
11 |
|
return $this->canHandle($item) ? $this->formatItem($item) : $item; |
79
|
11 |
|
}, $items); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
abstract public function formatItem(FormattedItem $formattedItem): FormattedItem; |
|
|
|
|
83
|
|
|
|
84
|
|
|
abstract public function handles(): string; |
|
|
|
|
85
|
|
|
|
86
|
11 |
|
protected function canHandle(FormattedItem $item): bool |
|
|
|
|
87
|
|
|
{ |
88
|
11 |
|
if($this->handles() === self::ALL) { |
|
|
|
|
89
|
3 |
|
return true; |
90
|
|
|
} |
91
|
8 |
|
return $item->isType($this->handles()); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
} |