Skip to content

Commit 2430600

Browse files
committed
Refactor exists() and get()
1 parent 0708cbd commit 2430600

18 files changed

+78
-77
lines changed

api.php

Lines changed: 39 additions & 39 deletions
Large diffs are not rendered by default.

src/Tqdev/PhpCrudApi/Column/DefinitionService.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function updateTable(String $tableName, /* object */ $changes): bool
3131
public function updateColumn(String $tableName, String $columnName, /* object */ $changes): bool
3232
{
3333
$table = $this->reflection->getTable($tableName);
34-
$column = $table->get($columnName);
34+
$column = $table->getColumn($columnName);
3535

3636
// remove constraints on other column
3737
$newColumn = ReflectedColumn::fromJson((object) array_merge((array) $column->jsonSerialize(), (array) $changes));
@@ -136,7 +136,7 @@ public function removeTable(String $tableName)
136136
public function removeColumn(String $tableName, String $columnName)
137137
{
138138
$table = $this->reflection->getTable($tableName);
139-
$newColumn = $table->get($columnName);
139+
$newColumn = $table->getColumn($columnName);
140140
if ($newColumn->getPk()) {
141141
$newColumn->setPk(false);
142142
if (!$this->db->definition()->removeColumnPrimaryKey($table->getName(), $newColumn->getName(), $newColumn)) {

src/Tqdev/PhpCrudApi/Column/Reflection/ReflectedDatabase.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class ReflectedDatabase implements \JsonSerializable
77
{
88
private $name;
99
private $tableNames;
10+
private $viewNames;
1011

1112
public function __construct(String $name, array $tableNames)
1213
{
@@ -43,7 +44,7 @@ public function getName(): String
4344
return $this->name;
4445
}
4546

46-
public function exists(String $tableName): bool
47+
public function hasTable(String $tableName): bool
4748
{
4849
return isset($this->tableNames[$tableName]);
4950
}

src/Tqdev/PhpCrudApi/Column/Reflection/ReflectedTable.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public static function fromJson( /* object */$json): ReflectedTable
7474
return new ReflectedTable($name, $columns);
7575
}
7676

77-
public function exists(String $columnName): bool
77+
public function hasColumn(String $columnName): bool
7878
{
7979
return isset($this->columns[$columnName]);
8080
}
@@ -99,7 +99,7 @@ public function columnNames(): array
9999
return array_keys($this->columns);
100100
}
101101

102-
public function get($columnName): ReflectedColumn
102+
public function getColumn($columnName): ReflectedColumn
103103
{
104104
return $this->columns[$columnName];
105105
}

src/Tqdev/PhpCrudApi/Column/ReflectionService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function refreshTable(String $tableName)
6161

6262
public function hasTable(String $tableName): bool
6363
{
64-
return $this->database->exists($tableName);
64+
return $this->database->hasTable($tableName);
6565
}
6666

6767
public function getTable(String $tableName): ReflectedTable

src/Tqdev/PhpCrudApi/Controller/ColumnController.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ public function getColumn(Request $request): Response
5959
return $this->responder->error(ErrorCode::TABLE_NOT_FOUND, $tableName);
6060
}
6161
$table = $this->reflection->getTable($tableName);
62-
if (!$table->exists($columnName)) {
62+
if (!$table->hasColumn($columnName)) {
6363
return $this->responder->error(ErrorCode::COLUMN_NOT_FOUND, $columnName);
6464
}
65-
$column = $table->get($columnName);
65+
$column = $table->getColumn($columnName);
6666
return $this->responder->success($column);
6767
}
6868

@@ -87,7 +87,7 @@ public function updateColumn(Request $request): Response
8787
return $this->responder->error(ErrorCode::TABLE_NOT_FOUND, $tableName);
8888
}
8989
$table = $this->reflection->getTable($tableName);
90-
if (!$table->exists($columnName)) {
90+
if (!$table->hasColumn($columnName)) {
9191
return $this->responder->error(ErrorCode::COLUMN_NOT_FOUND, $columnName);
9292
}
9393
$success = $this->definition->updateColumn($tableName, $columnName, $request->getBody());
@@ -118,7 +118,7 @@ public function addColumn(Request $request): Response
118118
}
119119
$columnName = $request->getBody()->name;
120120
$table = $this->reflection->getTable($tableName);
121-
if ($table->exists($columnName)) {
121+
if ($table->hasColumn($columnName)) {
122122
return $this->responder->error(ErrorCode::COLUMN_ALREADY_EXISTS, $columnName);
123123
}
124124
$success = $this->definition->addColumn($tableName, $request->getBody());
@@ -149,7 +149,7 @@ public function removeColumn(Request $request): Response
149149
return $this->responder->error(ErrorCode::TABLE_NOT_FOUND, $tableName);
150150
}
151151
$table = $this->reflection->getTable($tableName);
152-
if (!$table->exists($columnName)) {
152+
if (!$table->hasColumn($columnName)) {
153153
return $this->responder->error(ErrorCode::COLUMN_NOT_FOUND, $columnName);
154154
}
155155
$success = $this->definition->removeColumn($tableName, $columnName);

src/Tqdev/PhpCrudApi/Controller/RecordController.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function _list(Request $request): Response
2828
{
2929
$table = $request->getPathSegment(2);
3030
$params = $request->getParams();
31-
if (!$this->service->exists($table)) {
31+
if (!$this->service->hasTable($table)) {
3232
return $this->responder->error(ErrorCode::TABLE_NOT_FOUND, $table);
3333
}
3434
return $this->responder->success($this->service->_list($table, $params));
@@ -39,7 +39,7 @@ public function read(Request $request): Response
3939
$table = $request->getPathSegment(2);
4040
$id = $request->getPathSegment(3);
4141
$params = $request->getParams();
42-
if (!$this->service->exists($table)) {
42+
if (!$this->service->hasTable($table)) {
4343
return $this->responder->error(ErrorCode::TABLE_NOT_FOUND, $table);
4444
}
4545
if (strpos($id, ',') !== false) {
@@ -66,7 +66,7 @@ public function create(Request $request): Response
6666
return $this->responder->error(ErrorCode::HTTP_MESSAGE_NOT_READABLE, '');
6767
}
6868
$params = $request->getParams();
69-
if (!$this->service->exists($table)) {
69+
if (!$this->service->hasTable($table)) {
7070
return $this->responder->error(ErrorCode::TABLE_NOT_FOUND, $table);
7171
}
7272
if (is_array($record)) {
@@ -89,7 +89,7 @@ public function update(Request $request): Response
8989
return $this->responder->error(ErrorCode::HTTP_MESSAGE_NOT_READABLE, '');
9090
}
9191
$params = $request->getParams();
92-
if (!$this->service->exists($table)) {
92+
if (!$this->service->hasTable($table)) {
9393
return $this->responder->error(ErrorCode::TABLE_NOT_FOUND, $table);
9494
}
9595
$ids = explode(',', $id);
@@ -115,7 +115,7 @@ public function delete(Request $request): Response
115115
$table = $request->getPathSegment(2);
116116
$id = $request->getPathSegment(3);
117117
$params = $request->getParams();
118-
if (!$this->service->exists($table)) {
118+
if (!$this->service->hasTable($table)) {
119119
return $this->responder->error(ErrorCode::TABLE_NOT_FOUND, $table);
120120
}
121121
$ids = explode(',', $id);
@@ -139,7 +139,7 @@ public function increment(Request $request): Response
139139
return $this->responder->error(ErrorCode::HTTP_MESSAGE_NOT_READABLE, '');
140140
}
141141
$params = $request->getParams();
142-
if (!$this->service->exists($table)) {
142+
if (!$this->service->hasTable($table)) {
143143
return $this->responder->error(ErrorCode::TABLE_NOT_FOUND, $table);
144144
}
145145
$ids = explode(',', $id);

src/Tqdev/PhpCrudApi/Database/ColumnsBuilder.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function getOrderBy(ReflectedTable $table, array $columnOrdering): String
3636
{
3737
$results = array();
3838
foreach ($columnOrdering as $i => list($columnName, $ordering)) {
39-
$column = $table->get($columnName);
39+
$column = $table->getColumn($columnName);
4040
$quotedColumnName = $this->quoteColumnName($column);
4141
$results[] = $quotedColumnName . ' ' . $ordering;
4242
}
@@ -47,7 +47,7 @@ public function getSelect(ReflectedTable $table, array $columnNames): String
4747
{
4848
$results = array();
4949
foreach ($columnNames as $columnName) {
50-
$column = $table->get($columnName);
50+
$column = $table->getColumn($columnName);
5151
$quotedColumnName = $this->quoteColumnName($column);
5252
$quotedColumnName = $this->converter->convertColumnName($column, $quotedColumnName);
5353
$results[] = $quotedColumnName;
@@ -60,7 +60,7 @@ public function getInsert(ReflectedTable $table, array $columnValues): String
6060
$columns = array();
6161
$values = array();
6262
foreach ($columnValues as $columnName => $columnValue) {
63-
$column = $table->get($columnName);
63+
$column = $table->getColumn($columnName);
6464
$quotedColumnName = $this->quoteColumnName($column);
6565
$columns[] = $quotedColumnName;
6666
$columnValue = $this->converter->convertColumnValue($column);
@@ -80,7 +80,7 @@ public function getUpdate(ReflectedTable $table, array $columnValues): String
8080
{
8181
$results = array();
8282
foreach ($columnValues as $columnName => $columnValue) {
83-
$column = $table->get($columnName);
83+
$column = $table->getColumn($columnName);
8484
$quotedColumnName = $this->quoteColumnName($column);
8585
$columnValue = $this->converter->convertColumnValue($column);
8686
$results[] = $quotedColumnName . '=' . $columnValue;
@@ -95,7 +95,7 @@ public function getIncrement(ReflectedTable $table, array $columnValues): String
9595
if (!is_numeric($columnValue)) {
9696
continue;
9797
}
98-
$column = $table->get($columnName);
98+
$column = $table->getColumn($columnName);
9999
$quotedColumnName = $this->quoteColumnName($column);
100100
$columnValue = $this->converter->convertColumnValue($column);
101101
$results[] = $quotedColumnName . '=' . $quotedColumnName . '+' . $columnValue;

src/Tqdev/PhpCrudApi/Database/DataConverter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ private function getRecordValueConversion(ReflectedColumn $column): String
3333
public function convertRecords(ReflectedTable $table, array $columnNames, array &$records) /*: void*/
3434
{
3535
foreach ($columnNames as $columnName) {
36-
$column = $table->get($columnName);
36+
$column = $table->getColumn($columnName);
3737
$conversion = $this->getRecordValueConversion($column);
3838
if ($conversion != 'none') {
3939
foreach ($records as $i => $record) {
@@ -68,7 +68,7 @@ public function convertColumnValues(ReflectedTable $table, array &$columnValues)
6868
{
6969
$columnNames = array_keys($columnValues);
7070
foreach ($columnNames as $columnName) {
71-
$column = $table->get($columnName);
71+
$column = $table->getColumn($columnName);
7272
$conversion = $this->getInputValueConversion($column);
7373
if ($conversion != 'none') {
7474
$value = $columnValues[$columnName];

src/Tqdev/PhpCrudApi/Database/GenericDefinition.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ private function getAddTableSQL(ReflectedTable $newTable): String
261261
$fields = [];
262262
$constraints = [];
263263
foreach ($newTable->columnNames() as $columnName) {
264-
$newColumn = $newTable->get($columnName);
264+
$newColumn = $newTable->getColumn($columnName);
265265
$f1 = $this->quote($columnName);
266266
$f2 = $this->getColumnType($newColumn, false);
267267
$f3 = $this->quote($tableName . '_' . $columnName . '_fkey');

0 commit comments

Comments
 (0)