1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* webtrees-lib: MyArtJaub library for webtrees |
5
|
|
|
* Helper functions to be used in the core webtrees application. |
6
|
|
|
* |
7
|
|
|
* @package MyArtJaub\Webtrees |
8
|
|
|
* @subpackage Helpers |
9
|
|
|
* @author Jonathan Jaubart <[email protected]> |
10
|
|
|
* @copyright Copyright (c) 2021-2022, Jonathan Jaubart |
11
|
|
|
* @license http://www.gnu.org/licenses/gpl.html GNU General Public License, version 3 |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
use Illuminate\Contracts\Container\BindingResolutionException; |
15
|
|
|
use Illuminate\Support\Collection; |
16
|
|
|
use MyArtJaub\Webtrees\Contracts\Hooks\HookServiceInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Invoke a hook interface and get hold of its result. |
20
|
|
|
* |
21
|
|
|
* @template THook of \MyArtJaub\Webtrees\Contracts\Hooks\HookInterface |
22
|
|
|
* @template TCollector of \MyArtJaub\Webtrees\Contracts\Hooks\HookCollectorInterface |
23
|
|
|
* @template TReturn |
24
|
|
|
* |
25
|
|
|
* @param class-string<THook> $hook_interface |
|
|
|
|
26
|
|
|
* @param callable(TCollector): TReturn $apply |
27
|
|
|
* @param TReturn|null $default |
28
|
|
|
* @return TReturn|null |
29
|
|
|
*/ |
30
|
|
|
function hook(string $hook_interface, callable $apply, $default = null) |
31
|
|
|
{ |
32
|
|
|
try { |
33
|
|
|
$hook_collector = app(HookServiceInterface::class)->use($hook_interface); |
34
|
|
|
if ($hook_collector !== null) { |
35
|
|
|
return $apply($hook_collector); |
36
|
|
|
} |
37
|
|
|
} catch (BindingResolutionException $ex) { |
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
return $default; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Get the updated column index after insertion of new columns. |
45
|
|
|
* |
46
|
|
|
* @param int $initial_index |
47
|
|
|
* @param Collection<int> $new_column_indexes |
48
|
|
|
* @return int |
49
|
|
|
*/ |
50
|
|
|
function columnIndex(int $initial_index, Collection $new_column_indexes): int |
51
|
|
|
{ |
52
|
|
|
return $initial_index + $new_column_indexes->filter(fn(int $i) => $i <= $initial_index)->count(); |
53
|
|
|
} |
54
|
|
|
|