1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Models; |
4
|
|
|
|
5
|
|
|
use Core\Model; |
6
|
|
|
use Core\Traits\StringFunctions; |
7
|
|
|
|
8
|
|
|
class SlugModel extends Model |
9
|
|
|
{ |
10
|
|
|
use StringFunctions; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* is the slug unique, used when updating the slug |
14
|
|
|
* @param string $slug the slug to search for |
15
|
|
|
* @param string $table the table to search in |
16
|
|
|
* @param string $columnName the name of the slug column |
17
|
|
|
* @return bool |
18
|
|
|
* @throws \ErrorException |
19
|
|
|
*/ |
20
|
|
|
public function isUnique(string $slug, string $table, string $columnName): bool |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
if (!$this->isAlphaNum($table)) { |
24
|
|
|
throw new \ErrorException("Invalid table name " . $table); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
if (!$this->isAlphaNum($columnName)) { |
28
|
|
|
throw new \ErrorException("Invalid Column name " . $columnName); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
$slugTbl = $this->getTablePrefix($table); |
32
|
|
|
|
33
|
|
|
$sql = "SELECT * FROM $slugTbl WHERE $columnName = :slug"; |
34
|
|
|
$this->query($sql); |
35
|
|
|
$this->bind(':slug', $slug); |
36
|
|
|
$this->execute(); |
37
|
|
|
return !$this->stmt->rowCount() > 0; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* get the ID of the row from the slug |
42
|
|
|
* @param string $slug the slug to search |
43
|
|
|
* @param string $table the table to search in |
44
|
|
|
* @param string $columnName the slug column name |
45
|
|
|
* @param string $idColumn the id column name |
46
|
|
|
* @return int the id of the row |
47
|
|
|
* @throws \ErrorException |
48
|
|
|
*/ |
49
|
|
|
public function getIdFromSlug(string $slug, string $table, string $columnName, string $idColumn): int |
50
|
|
|
{ |
51
|
|
|
if (!$this->isAlphaNum($table)) { |
52
|
|
|
throw new \ErrorException("Invalid table name " . $table); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if (!$this->isAlphaNum($columnName)) { |
56
|
|
|
throw new \ErrorException("Invalid Slug Column name " . $columnName); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if (!$this->isAlphaNum($idColumn)) { |
60
|
|
|
throw new \ErrorException("Invalid ID Column name " . $columnName); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$slugTbl = $this->getTablePrefix($table); |
64
|
|
|
|
65
|
|
|
$sql = "SELECT $idColumn FROM $slugTbl WHERE $columnName = :slug"; |
66
|
|
|
$this->query($sql); |
67
|
|
|
$this->bind(":slug", $slug); |
68
|
|
|
$this->execute(); |
69
|
|
|
if (!$this->stmt->rowCount() > 0) { |
70
|
|
|
return 0; |
71
|
|
|
} |
72
|
|
|
return $this->stmt->fetchColumn(); |
73
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* get the slug from an Id |
78
|
|
|
* @param int $searchId |
79
|
|
|
* @param string $table |
80
|
|
|
* @param string $columnName |
81
|
|
|
* @param string $idColumn |
82
|
|
|
* @return string |
83
|
|
|
* @throws \ErrorException |
84
|
|
|
*/ |
85
|
|
|
public function getSlugFromId(int $searchId, string $table, string $columnName, string $slugColumn): string |
86
|
|
|
{ |
87
|
|
|
if (!$this->isAlphaNum($table)) { |
88
|
|
|
throw new \ErrorException("Invalid table name " . $table); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
if (!$this->isAlphaNum($columnName)) { |
92
|
|
|
throw new \ErrorException("Invalid Slug Column name " . $columnName); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if (!$this->isAlphaNum($slugColumn)) { |
96
|
|
|
throw new \ErrorException("Invalid ID Column name " . $columnName); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$slugTbl = $this->getTablePrefix($table); |
100
|
|
|
|
101
|
|
|
$sql = "SELECT $slugColumn FROM $slugTbl WHERE $columnName = :searchId"; |
102
|
|
|
$this->query($sql); |
103
|
|
|
$this->bind(":searchId", $searchId); |
104
|
|
|
$this->execute(); |
105
|
|
|
if (!$this->stmt->rowCount() > 0) { |
106
|
|
|
return 0; |
107
|
|
|
} |
108
|
|
|
return $this->stmt->fetchColumn(); |
109
|
|
|
} |
110
|
|
|
} |