Skip to content

Commit

Permalink
feat: add much more flexibility and options to the trait with test
Browse files Browse the repository at this point in the history
  • Loading branch information
WatheqAlshowaiter committed Jul 18, 2024
1 parent 84cca63 commit f721a84
Show file tree
Hide file tree
Showing 2 changed files with 268 additions and 41 deletions.
154 changes: 115 additions & 39 deletions src/RequiredFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,17 @@ trait RequiredFields
*
* @return array|string
*/
public static function getRequiredFields()
{
public static function getRequiredFields(
$withNullables = false,
$withDefaults = false,
$withPrimaryKey = false
) {
if ((float) App::version() < 10) {
return self::getRequiredFieldsForOlderVersions();
return self::getRequiredFieldsForOlderVersions(
$withNullables,
$withDefaults,
$withPrimaryKey
);
}

$primaryIndex = collect(Schema::getIndexes((new self())->getTable()))
Expand All @@ -30,11 +37,11 @@ public static function getRequiredFields()
->toArray();

return collect(Schema::getColumns((new self())->getTable()))
->reject(function ($column) use ($primaryIndex) {
return $column['auto_increment']
|| $column['nullable']
|| $column['default'] != null
|| in_array($column['name'], $primaryIndex);
->reject(function ($column) use ($primaryIndex, $withNullables, $withDefaults, $withPrimaryKey) {
return
$column['nullable'] && !$withNullables
|| $column['default'] != null && !$withDefaults
|| in_array($column['name'], $primaryIndex) && !$withPrimaryKey;
})
->pluck('name')
->toArray();
Expand All @@ -45,20 +52,39 @@ public static function getRequiredFields()
*
* @return array|string
*/
public static function getRequiredFieldsForOlderVersions()
{
public static function getRequiredFieldsForOlderVersions(
$withNullables = false,
$withDefaults = false,
$withPrimaryKey = false
) {
$databaseDriver = DB::connection()->getDriverName();

switch ($databaseDriver) {
case 'sqlite':
return self::getRequiredFieldsForSqlite();
return self::getRequiredFieldsForSqlite(
$withNullables,
$withDefaults,
$withPrimaryKey
);
case 'mysql':
case 'mariadb':
return self::getRequiredFieldsForMysqlAndMariaDb();
return self::getRequiredFieldsForMysqlAndMariaDb(
$withNullables,
$withDefaults,
$withPrimaryKey
);
case 'pgsql':
return self::getRequiredFieldsForPostgres();
return self::getRequiredFieldsForPostgres(
$withNullables,
$withDefaults,
$withPrimaryKey
);
case 'sqlsrv':
return self::getRequiredFieldsForSqlServer();
return self::getRequiredFieldsForSqlServer(
$withNullables,
$withDefaults,
$withPrimaryKey
);
default:
return 'NOT SUPPORTED DATABASE DRIVER';
}
Expand All @@ -67,8 +93,11 @@ public static function getRequiredFieldsForOlderVersions()
/**
* @return array
*/
private static function getRequiredFieldsForSqlite()
{
private static function getRequiredFieldsForSqlite(
$withNullables = false,
$withDefaults = false,
$withPrimaryKey = false
) {
$table = self::getTableFromThisModel();

$queryResult = DB::select("PRAGMA table_info($table)");
Expand All @@ -79,10 +108,10 @@ private static function getRequiredFieldsForSqlite()
}, $queryResult);

return collect($queryResult)
->reject(function ($column) {
return $column['pk']
|| $column['dflt_value'] != null
|| ! $column['notnull'];
->reject(function ($column) use ($withNullables, $withDefaults, $withPrimaryKey) {
return $column['pk'] && !$withPrimaryKey
|| $column['dflt_value'] != null && !$withDefaults
|| !$column['notnull'] && !$withNullables;
})
->pluck('name')
->toArray();
Expand All @@ -91,8 +120,11 @@ private static function getRequiredFieldsForSqlite()
/**
* @return array
*/
private static function getRequiredFieldsForMysqlAndMariaDb()
{
private static function getRequiredFieldsForMysqlAndMariaDb(
$withNullables = false,
$withDefaults = false,
$withPrimaryKey = false
) {
$table = self::getTableFromThisModel();

$queryResult = DB::select(
Expand All @@ -119,10 +151,10 @@ private static function getRequiredFieldsForMysqlAndMariaDb()
}, $queryResult);

return collect($queryResult)
->reject(function ($column) {
return $column['primary']
|| $column['default'] != null
|| $column['nullable'];
->reject(function ($column) use ($withNullables, $withDefaults, $withPrimaryKey) {
return $column['primary'] && !$withPrimaryKey
|| $column['default'] != null && !$withDefaults
|| $column['nullable'] && !$withNullables;
})
->pluck('name')
->toArray();
Expand All @@ -131,8 +163,11 @@ private static function getRequiredFieldsForMysqlAndMariaDb()
/**
* @return array
*/
private static function getRequiredFieldsForPostgres()
{
private static function getRequiredFieldsForPostgres(
$withNullables = false,
$withDefaults = false,
$withPrimaryKey = false
) {

$table = self::getTableFromThisModel();

Expand Down Expand Up @@ -200,22 +235,26 @@ private static function getRequiredFieldsForPostgres()
}, $queryResult);

return collect($queryResult)
->reject(function ($column) use ($primaryIndex) {
return $column['default']
|| $column['nullable'] == 'YES'
|| in_array($column['name'], $primaryIndex);
->reject(function ($column) use ($primaryIndex, $withPrimaryKey, $withDefaults, $withNullables) {
return
$column['default'] && !$withDefaults
|| $column['nullable'] == 'YES' && !$withNullables
|| in_array($column['name'], $primaryIndex) && !$withPrimaryKey;
})
->pluck('name')
->toArray();
}

/**
* Not tested yet in machine with SQLSERVER
* Not tested yet in machine with SQL SERVER
*
* @return array
*/
private static function getRequiredFieldsForSqlServer()
{
private static function getRequiredFieldsForSqlServer(
$withNullables = false,
$withDefaults = false,
$withPrimaryKey = false
) {
$table = self::getTableFromThisModel();

$queryResult = DB::select(
Expand Down Expand Up @@ -243,10 +282,10 @@ private static function getRequiredFieldsForSqlServer()
}, $queryResult);

return collect($queryResult)
->reject(function ($column) {
return $column['primary']
|| $column['default'] != null
|| $column['nullable'];
->reject(function ($column) use ($withDefaults, $withNullables, $withPrimaryKey) {
return $column['primary'] && !$withPrimaryKey
|| $column['default'] != null && !$withDefaults
|| $column['nullable'] && !$withNullables;
})
->pluck('name')
->toArray();
Expand All @@ -261,4 +300,41 @@ private static function getTableFromThisModel()

return str_replace('.', '__', $table);
}

public static function getRequiredFieldsWithNullables()
{
return self::getRequiredFields($withNullables = true, $withDefaults = false, $withPrimaryKey = false);
}

public static function getRequiredFieldsWithDefaults()
{
return self::getRequiredFields($withNullables = false, $withDefaults = true, $withPrimaryKey = false);
}

public static function getRequiredFieldsWithPrimaryKey()
{
return self::getRequiredFields($withNullables = false, $withDefaults = false, $withPrimaryKey = true);
}

public static function getRequiredFieldsWithDefaultsAndPrimaryKey()
{
return self::getRequiredFields($withNullables = false, $withDefaults = true, $withPrimaryKey = true);
}
public static function getRequiredFieldsWithNullablesAndDefaults()
{
return self::getRequiredFields($withNullables = true, $withDefaults = true, $withPrimaryKey = false);
}
public static function getRequiredFieldsWithNullablesAndPrimaryKey()
{
return self::getRequiredFields($withNullables = true, $withDefaults = false, $withPrimaryKey = true);
}

public static function getAllFields()
{
return self::getRequiredFields(
$withNullables = true,
$withDefaults = true,
$withPrimaryKey = true
);
}
}
Loading

0 comments on commit f721a84

Please sign in to comment.