Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
93.33% covered (success)
93.33%
14 / 15
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
Action
93.33% covered (success)
93.33%
14 / 15
50.00% covered (danger)
50.00%
1 / 2
5.01
0.00% covered (danger)
0.00%
0 / 1
 add
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
3
 get
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
2.15
1<?php
2
3declare(strict_types=1);
4
5namespace PeServer\Core\Mvc;
6
7use PeServer\Core\Collection\Arr;
8use PeServer\Core\Http\HttpMethod;
9use PeServer\Core\Mvc\ActionSetting;
10use PeServer\Core\Mvc\Middleware\IMiddleware;
11use PeServer\Core\Mvc\Middleware\IShutdownMiddleware;
12
13/**
14 * HTTPメソッドとコントローラメソッドを紐づける。
15 * リクエストパスとコントローラクラス自体の紐づけは Route を参照のこと。
16 */
17class Action
18{
19    #region variable
20
21    /**
22     * 紐づけ。
23     *
24     * HTTPメソッドとコントローラメソッドがペアになる。
25     * 後入れ優先。
26     *
27     * @var array<string,ActionSetting>
28     * @phpstan-var array<HttpMethod::HTTP_METHOD_*|non-empty-string,ActionSetting>
29     */
30    private array $map = [];
31
32    #endregion
33
34    #region function
35
36    /**
37     * 追加。
38     *
39     * @param HttpMethod|HttpMethod[] $httpMethod HTTPメソッド
40     * @param non-empty-string $callMethod コントローラメソッド。
41     * @param array<IMiddleware|class-string<IMiddleware>> $middleware
42     * @param array<IShutdownMiddleware|class-string<IShutdownMiddleware>> $shutdownMiddleware
43     */
44    public function add(HttpMethod|array $httpMethod, string $callMethod, array $middleware, array $shutdownMiddleware): void
45    {
46        if (is_array($httpMethod)) {
47            foreach ($httpMethod as $method) {
48                $this->map[$method->name] = new ActionSetting(
49                    $callMethod,
50                    $middleware,
51                    $shutdownMiddleware
52                );
53            }
54        } else {
55            $this->map[$httpMethod->name] = new ActionSetting(
56                $callMethod,
57                $middleware,
58                $shutdownMiddleware
59            );
60        }
61    }
62
63    /**
64     * 取得。
65     *
66     * @param HttpMethod $httpMethod HTTPメソッド
67     * @return ActionSetting|null あった場合はクラスメソッド紐づけ情報、なければ null
68     */
69    public function get(HttpMethod $httpMethod): ?ActionSetting
70    {
71        if (Arr::tryGet($this->map, $httpMethod->name, $result)) {
72            return $result;
73        }
74
75        return null;
76    }
77
78    #endregion
79}