Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
14 / 14
CRAP
100.00% covered (success)
100.00%
1 / 1
RouteGen
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
14 / 14
22
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getRouteName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getParentRouteName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getRouteUrl
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getRouteUrlWithoutPrefix
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getRouteUrlWithPlaceholders
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getParentRouteUrlWithPlaceholders
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getUrlWithPlaceholders
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 getRouteUrlWithId
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 getParentRouteUrl
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getRouteVars
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getRouteVarsWithId
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 getAsRoute
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 withRoutePrefix
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace San\Crud\Generators;
4
5use San\Crud\Utils\NameUtils;
6
7class RouteGen extends BaseGen {
8    public function __construct(public array $tables, public ?string $routePrefix = null) {
9        parent::__construct($tables);
10    }
11
12    public function getRouteName() {
13        return NameUtils::getRouteName($this->tables);
14    }
15
16    public function getParentRouteName() {
17        return NameUtils::getRouteName($this->parentTables());
18    }
19
20    public function getRouteUrl() {
21        return $this->withRoutePrefix(str_replace('.', '/', $this->getRouteName()));
22    }
23
24    public function getRouteUrlWithoutPrefix() {
25        return str_replace('.', '/', $this->getRouteName());
26    }
27
28    public function getRouteUrlWithPlaceholders() {
29        return $this->getUrlWithPlaceholders($this->tables);
30    }
31
32    public function getParentRouteUrlWithPlaceholders() {
33        return $this->getUrlWithPlaceholders($this->parentTables(), $this->mainTable());
34    }
35
36    public function getUrlWithPlaceholders($tables, $suffix = '') {
37        foreach ((array) $tables as $table) {
38            $parts[] = sprintf('%s/{%s}', $table, NameUtils::getVariableName($table));
39        }
40
41        if (!empty($suffix)) {
42            $parts[] = $suffix;
43        }
44
45        return $this->withRoutePrefix(implode('/', $parts ?? []));
46    }
47
48    public function getRouteUrlWithId() {
49        foreach ((array) $this->tables as $table) {
50            if ($table == $this->mainTable()) {
51                $parts[] = sprintf('%s/{%s_id}', $table, NameUtils::getVariableName($table));
52            } else {
53                $parts[] = sprintf('%s/{%s}', $table, NameUtils::getVariableName($table));
54            }
55        }
56
57        return $this->withRoutePrefix(implode('/', $parts));
58    }
59
60    public function getParentRouteUrl() {
61        return $this->withRoutePrefix(str_replace('.', '/', $this->getParentRouteName()));
62    }
63
64    public function getRouteVars() {
65        return sprintf('[%s]', join(', ', array_map(fn($table) => sprintf("'%s' => $%s", NameUtils::getVariableName($table), NameUtils::getVariableName($table)), $this->tables)));
66    }
67
68    public function getRouteVarsWithId() {
69        foreach ($this->tables as $table) {
70            if ($table == $this->mainTable()) {
71                $vars[] = sprintf("'%s_id' => $%s->id", NameUtils::getVariableName($table), NameUtils::getVariableName($table));
72            } else {
73                $vars[] = sprintf("'%s' => $%s", NameUtils::getVariableName($table), NameUtils::getVariableName($table));
74            }
75        }
76
77        return sprintf('[%s]', join(', ', $vars ?? []));
78    }
79
80    public function getAsRoute() {
81        return $this->hasParentTable() ? sprintf('"as" => "%s",', $this->getParentRouteName()) : '';
82    }
83
84    public function withRoutePrefix(string $route) {
85        return empty($this->routePrefix) ? $route : sprintf('%s/%s', trim($this->routePrefix, '/'), $route);
86    }
87
88}