diff --git a/src/RequiredFields.php b/src/RequiredFields.php index 903c404..7e6baaf 100644 --- a/src/RequiredFields.php +++ b/src/RequiredFields.php @@ -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())) @@ -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(); @@ -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'; } @@ -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)"); @@ -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(); @@ -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( @@ -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(); @@ -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(); @@ -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( @@ -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(); @@ -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 + ); + } } diff --git a/tests/RequiredFieldsTest.php b/tests/RequiredFieldsTest.php index 4ea9004..25768f7 100644 --- a/tests/RequiredFieldsTest.php +++ b/tests/RequiredFieldsTest.php @@ -10,10 +10,10 @@ class RequiredFieldsTest extends TestCase { use RefreshDatabase; - // todo: remove all getRequiredFieldsForOlderVersions() - public function test_get_required_fields_for_parent_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() // ); @@ -42,6 +42,157 @@ public function test_get_required_fields_in_order() ], Father::getRequiredFieldsForOlderVersions()); } + public function test_get_required_fields_with_nullables() + { + $expected = [ + 'name', + 'email', + 'username', + 'created_at', + 'updated_at', + 'deleted_at', + ]; + $this->assertEquals($expected, Father::getRequiredFields($withNullables = true)); + $this->assertEquals($expected, Father::getRequiredFieldsForOlderVersions($withNullables = true)); + $this->assertEquals($expected, Father::getRequiredFieldsWithNullables()); + } + + public function test_get_required_fields_with_defaults() + { + $expected = [ + 'active', + 'name', + 'email', + ]; + $this->assertEquals($expected, Father::getRequiredFields($withNullables = false, $withDefaults = true)); + $this->assertEquals($expected, Father::getRequiredFieldsForOlderVersions( + $withNullables = false, + $withDefaults = true + )); + $this->assertEquals($expected, Father::getRequiredFieldsWithDefaults()); + } + + public function test_get_required_with_primary_key() + { + $expected = [ + 'id', + 'name', + 'email', + ]; + + $this->assertEquals($expected, Father::getRequiredFields( + $withNullables = false, + $withDefaults = false, + $withPrimaryKey = true + )); + + $this->assertEquals($expected, Father::getRequiredFieldsForOlderVersions( + $withNullables = false, + $withDefaults = false, + $withPrimaryKey = true + )); + + $this->assertEquals($expected, Father::getRequiredFieldsWithPrimaryKey()); + } + + public function test_get_required_with_nullables_and_defaults() + { + $expected = [ + 'active', + 'name', + 'email', + 'username', + 'created_at', + 'updated_at', + 'deleted_at', + ]; + $this->assertEquals($expected, Father::getRequiredFields( + $withNullables = true, + $withDefaults = true, + $withPrimaryKey = false + )); + + $this->assertEquals($expected, Father::getRequiredFieldsForOlderVersions( + $withNullables = true, + $withDefaults = true, + $withPrimaryKey = false + )); + $this->assertEquals($expected, Father::getRequiredFieldsWithNullablesAndDefaults()); + } + public function test_get_required_with_nullables_and_primary_key() + { + $expected = [ + 'id', + 'name', + 'email', + 'username', + 'created_at', + 'updated_at', + 'deleted_at', + ]; + $this->assertEquals($expected, Father::getRequiredFields( + $withNullables = true, + $withDefaults = false, + $withPrimaryKey = true + )); + + $this->assertEquals($expected, Father::getRequiredFieldsForOlderVersions( + $withNullables = true, + $withDefaults = false, + $withPrimaryKey = true + )); + $this->assertEquals($expected, Father::getRequiredFieldsWithNullablesAndPrimaryKey()); + } + + public function test_get_required_with_defaults_and_primary_key() + { + $expected = [ + 'id', + 'active', + 'name', + 'email', + ]; + $this->assertEquals($expected, Father::getRequiredFields( + $withNullables = false, + $withDefaults = true, + $withPrimaryKey = true + )); + + $this->assertEquals($expected, Father::getRequiredFieldsForOlderVersions( + $withNullables = false, + $withDefaults = true, + $withPrimaryKey = true + )); + $this->assertEquals($expected, Father::getRequiredFieldsWithDefaultsAndPrimaryKey()); + } + + public function test_get_required_with_defaults_and_nullables_and_primary_key() + { + $expected = [ + 'id', + 'active', + 'name', + 'email', + 'username', + 'created_at', + 'updated_at', + 'deleted_at' + ]; + + $this->assertEquals($expected, Father::getRequiredFields( + $withNullables = true, + $withDefaults = true, + $withPrimaryKey = true + )); + + $this->assertEquals($expected, Father::getRequiredFieldsForOlderVersions( + $withNullables = true, + $withDefaults = true, + $withPrimaryKey = true + )); + $this->assertEquals($expected, Father::getAllFields()); + } + public function test_get_required_fields_for_another_parent_model() { $this->assertEquals([