• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

aplus-framework / mvc / 16357225248

17 Jul 2025 10:10PM UTC coverage: 100.0%. Remained the same
16357225248

push

github

natanfelles
Adds links to service pages

1833 of 1833 relevant lines covered (100.0%)

10.63 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/Controller.php
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 ReflectionClass;
16
use ReflectionNamedType;
17

18
/**
19
 * Class Controller.
20
 *
21
 * @package mvc
22
 */
23
abstract class Controller extends RouteActions
24
{
25
    /**
26
     * The matched route Request.
27
     *
28
     * @var Request
29
     */
30
    protected Request $request;
31
    /**
32
     * The matched route Response.
33
     *
34
     * @var Response
35
     */
36
    protected Response $response;
37
    /**
38
     * Set true to load models in properties.
39
     *
40
     * @see Controller::prepareModels()
41
     *
42
     * @var bool
43
     */
44
    protected bool $loadModels = true;
45

46
    /**
47
     * Controller constructor.
48
     *
49
     * @param Request $request
50
     * @param Response $response
51
     */
52
    public function __construct(Request $request, Response $response)
53
    {
54
        $this->request = $request;
4✔
55
        $this->response = $response;
4✔
56
        if ($this->loadModels) {
4✔
57
            $this->prepareModels();
4✔
58
        }
59
    }
60

61
    /**
62
     * Initialize models in properties.
63
     *
64
     * @since 4
65
     *
66
     * @return static
67
     */
68
    protected function prepareModels() : static
69
    {
70
        $reflection = new ReflectionClass($this);
4✔
71
        foreach ($reflection->getProperties() as $property) {
4✔
72
            $type = $property->getType();
4✔
73
            if (!$type instanceof ReflectionNamedType) {
4✔
74
                continue;
4✔
75
            }
76
            $class = $type->getName();
4✔
77
            if (!\is_subclass_of($class, Model::class)) {
4✔
78
                continue;
4✔
79
            }
80
            $name = $property->name;
4✔
81
            if (isset($this->{$name})) {
4✔
82
                continue;
4✔
83
            }
84
            $this->{$name} = Model::get($class);
4✔
85
        }
86
        return $this;
4✔
87
    }
88

89
    /**
90
     * Render a view.
91
     *
92
     * @param string $view The view file
93
     * @param array<string,mixed> $variables The variables passed to the view
94
     * @param string $instance The View service instance name
95
     *
96
     * @return string The rendered view contents
97
     */
98
    protected function render(
99
        string $view,
100
        array $variables = [],
101
        string $instance = 'default'
102
    ) : string {
103
        return App::view($instance)->render($view, $variables);
1✔
104
    }
105

106
    /**
107
     * Validate data.
108
     *
109
     * @param array<string,mixed> $data The data to be validated
110
     * @param array<string,array<string>|string> $rules An associative array with field
111
     * as keys and values as rules
112
     * @param array<string,string> $labels An associative array with fields as
113
     * keys and label as values
114
     * @param array<string,array<string,string>> $messages A multi-dimensional
115
     * array with field names as keys and values as arrays where the keys are
116
     * rule names and values are the custom error message strings
117
     * @param string $instance The Validation service instance name
118
     *
119
     * @return array<string,string> An empty array if validation pass or an
120
     * associative array with field names as keys and error messages as values
121
     */
122
    protected function validate(
123
        array $data,
124
        array $rules,
125
        array $labels = [],
126
        array $messages = [],
127
        string $instance = 'default'
128
    ) : array {
129
        $validation = App::validation($instance);
1✔
130
        return $validation->setRules($rules)->setLabels($labels)
1✔
131
            ->setMessages($messages)->validate($data)
1✔
132
            ? []
1✔
133
            : $validation->getErrors();
1✔
134
    }
135
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc