Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
20 / 20 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| Controller | |
100.00% |
20 / 20 |
|
100.00% |
4 / 4 |
8 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| prepareModel | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
4 | |||
| render | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| validate | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php declare(strict_types=1); |
| 2 | /* |
| 3 | * This file is part of Aplus Framework MVC Library. |
| 4 | * |
| 5 | * (c) Natan Felles <natanfelles@gmail.com> |
| 6 | * |
| 7 | * For the full copyright and license information, please view the LICENSE |
| 8 | * file that was distributed with this source code. |
| 9 | */ |
| 10 | namespace Framework\MVC; |
| 11 | |
| 12 | use Framework\HTTP\Request; |
| 13 | use Framework\HTTP\Response; |
| 14 | use Framework\Routing\RouteActions; |
| 15 | use LogicException; |
| 16 | use ReflectionNamedType; |
| 17 | use ReflectionProperty; |
| 18 | |
| 19 | /** |
| 20 | * Class Controller. |
| 21 | * |
| 22 | * @package mvc |
| 23 | */ |
| 24 | abstract class Controller extends RouteActions |
| 25 | { |
| 26 | /** |
| 27 | * The matched route Request. |
| 28 | * |
| 29 | * @var Request |
| 30 | */ |
| 31 | protected Request $request; |
| 32 | /** |
| 33 | * The matched route Response. |
| 34 | * |
| 35 | * @var Response |
| 36 | */ |
| 37 | protected Response $response; |
| 38 | |
| 39 | /** |
| 40 | * Controller constructor. |
| 41 | * |
| 42 | * @param Request $request |
| 43 | * @param Response $response |
| 44 | */ |
| 45 | public function __construct(Request $request, Response $response) |
| 46 | { |
| 47 | $this->request = $request; |
| 48 | $this->response = $response; |
| 49 | $this->prepareModel(); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Initialize $model with property type name. |
| 54 | * |
| 55 | * @since 3.6 |
| 56 | * |
| 57 | * @return static |
| 58 | */ |
| 59 | protected function prepareModel() : static |
| 60 | { |
| 61 | if (\property_exists($this, 'model')) { |
| 62 | $property = new ReflectionProperty($this, 'model'); |
| 63 | $type = $property->getType(); |
| 64 | if ( ! $type instanceof ReflectionNamedType || $type->isBuiltin()) { |
| 65 | throw new LogicException( |
| 66 | 'Property ' . static::class |
| 67 | . '::$model must have a valid named type' |
| 68 | ); |
| 69 | } |
| 70 | $name = $type->getName(); |
| 71 | $this->model = new $name(); |
| 72 | } |
| 73 | return $this; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Render a view. |
| 78 | * |
| 79 | * @param string $view The view file |
| 80 | * @param array<string,mixed> $variables The variables passed to the view |
| 81 | * @param string $instance The View service instance name |
| 82 | * |
| 83 | * @return string The rendered view contents |
| 84 | */ |
| 85 | protected function render( |
| 86 | string $view, |
| 87 | array $variables = [], |
| 88 | string $instance = 'default' |
| 89 | ) : string { |
| 90 | return App::view($instance)->render($view, $variables); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Validate data. |
| 95 | * |
| 96 | * @param array<string,mixed> $data The data to be validated |
| 97 | * @param array<string,array<string>|string> $rules An associative array with field |
| 98 | * as keys and values as rules |
| 99 | * @param array<string,string> $labels An associative array with fields as |
| 100 | * keys and label as values |
| 101 | * @param array<string,array<string,string>> $messages A multi-dimensional |
| 102 | * array with field names as keys and values as arrays where the keys are |
| 103 | * rule names and values are the custom error message strings |
| 104 | * @param string $instance The Validation service instance name |
| 105 | * |
| 106 | * @return array<string,string> An empty array if validation pass or an |
| 107 | * associative array with field names as keys and error messages as values |
| 108 | */ |
| 109 | protected function validate( |
| 110 | array $data, |
| 111 | array $rules, |
| 112 | array $labels = [], |
| 113 | array $messages = [], |
| 114 | string $instance = 'default' |
| 115 | ) : array { |
| 116 | $validation = App::validation($instance); |
| 117 | return $validation->setRules($rules)->setLabels($labels) |
| 118 | ->setMessages($messages)->validate($data) |
| 119 | ? [] |
| 120 | : $validation->getErrors(); |
| 121 | } |
| 122 | } |