Skip to content

Commit

Permalink
refactor: change array_map to map in the collection
Browse files Browse the repository at this point in the history
  • Loading branch information
WatheqAlshowaiter committed Jul 21, 2024
1 parent b536091 commit 3ce7007
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 180 deletions.
50 changes: 0 additions & 50 deletions .github/workflows/mariadb.yml

This file was deleted.

130 changes: 7 additions & 123 deletions src/RequiredFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,6 @@ public static function getRequiredFields(
);
}

// if (DB::connection()->getDriverName() == 'mariadb') { // mariadb has special case for nullables
// // dump(
// // Schema::getColumns((new self())->getTable()) // todo remove it later
// // );
// return self::getRequiredFieldsForOlderVersions(
// $withNullables,
// $withDefaults,
// $withPrimaryKey
// );
// }

$primaryIndex = collect(Schema::getIndexes((new self())->getTable()))
->filter(function ($index) {
return $index['primary'];
Expand Down Expand Up @@ -170,20 +159,14 @@ private static function getRequiredFieldsForMysqlAndMariaDb(
return (array) $column;
}, $queryResult);

// convert each column with "default" => "NULL" to "default" => null (specific to mariadb)
$queryResult = array_map(function ($column) {
if ($column['default'] == 'NULL') {
$column['default'] = null;
}

return $column;
}, $queryResult);

if ($withNullables) {
dump($queryResult); // todo remove it later
}

return collect($queryResult)
->map(function ($column) { // specific to mariadb
if ($column['default'] == 'NULL') {
$column['default'] = null;
}

return $column;
})
->reject(function ($column) use ($withNullables, $withDefaults, $withPrimaryKey) {
return $column['primary'] && !$withPrimaryKey
|| $column['default'] != null && !$withDefaults
Expand Down Expand Up @@ -421,103 +404,4 @@ public static function getAllFields()
$withPrimaryKey = true
);
}

// test method todo remove it later
public static function testMethod(
$withNullables = false,
$withDefaults = false,
$withPrimaryKey = false
) {


$queryResult = [
[
"name" => "id",
"type" => "bigint(20) unsigned",
"nullable" => 0,
"default" => null,
"primary" => 1
],
[
"name" => "active",
"type" => "tinyint(1)",
"nullable" => 0,
"default" => "0",
"primary" => 0
],
[
"name" => "name",
"type" => "varchar(255)",
"nullable" => 0,
"default" => null,
"primary" => 0
],
[
"name" => "email",
"type" => "varchar(255)",
"nullable" => 0,
"default" => null,
"primary" => 0
],
[
"name" => "username",
"type" => "varchar(255)",
"nullable" => 1,
"default" => "NULL",
"primary" => 0
],
[
"name" => "created_at",
"type" => "timestamp",
"nullable" => 1,
"default" => "NULL",
"primary" => 0
],
[
"name" => "updated_at",
"type" => "timestamp",
"nullable" => 1,
"default" => "NULL",
"primary" => 0
],
[
"name" => "deleted_at",
"type" => "timestamp",
"nullable" => 1,
"default" => "NULL",
"primary" => 0
]
];

$queryResult = array_map(function ($column) {
if ($column['default'] == 'NULL') {
$column['default'] = null;
}

return $column;
}, $queryResult);


dd($queryResult);

// ignore primary unless $withPrimaryKey is true
// ignore default unless $withDefaults is true
// ignore nullable unless $withNullables is
$result = collect($queryResult)
->reject(function ($column) use ($withNullables, $withDefaults, $withPrimaryKey) {
return $column['primary'] && !$withPrimaryKey
|| $column['default'] != null && !$withDefaults
|| $column['nullable'] && !$withNullables;
})
->pluck('name')
->toArray();

// Add primary key to the result if $withPrimaryKey is true
// if ($withPrimaryKey) {
// $result = array_unique(array_merge($primaryIndex, $result));
// }

dd($result);
return $result;
}
}
8 changes: 1 addition & 7 deletions tests/RequiredFieldsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,8 @@ class RequiredFieldsTest extends TestCase
{
use RefreshDatabase;

public function test_get_required_fields_for_parent_model()
public function test_get_required_fields_for_father_model()
{
// todo after knowing how to change the database connection in the tests
// and test it in github action, then delete this comments
dump(
Illuminate\Support\Facades\DB::connection()->getDriverName()
);

$this->assertEquals([
'name',
'email',
Expand Down

0 comments on commit 3ce7007

Please sign in to comment.