Mojopollo\Schema\MakeMigrationJson::validateSchema PHP Method

validateSchema() public method

Validates the schema from the json array and returns an error of syntax errors if any are found
See also: https://laravel.com/docs/5.2/migrations#creating-columns
See also: https://laravel.com/docs/5.2/migrations#creating-indexes
public validateSchema ( array $data ) : array
$data array The array containing the json output from file
return array Any errors identified for every field schema
    public function validateSchema(array $data)
    {
        // Error array
        $errors = [];
        // Get allowed column modifiers and indexes
        $validModifiersAndIndexes = array_merge($this->getColumnIndexes(), $this->getColumnModifiers());
        // For every table
        foreach ($data as $tableName => $fields) {
            // If there is no fields here do not continue
            if (empty($fields)) {
                // Go to next
                continue 1;
            }
            // For every field
            foreach ($fields as $fieldName => $fieldSchema) {
                // Split field schema
                $fieldSchema = explode(':', $fieldSchema);
                // Assign column type
                $columnType = $this->parseProperty($fieldSchema[0]);
                // Assign all modifiers and indexes
                // only if there is a count of 2 or more
                $columnModifiersAndIndexes = count($fieldSchema) > 1 ? array_slice($fieldSchema, 1) : [];
                // Check for valid column type
                if ($this->isValidColumnType($columnType) === false) {
                    // Keep the json array structure and report error
                    $errors[$tableName][$fieldName]['columnType'] = "'{$columnType}' is not a valid column type";
                }
                // Check for valid column modifiers
                foreach ($columnModifiersAndIndexes as $modifierOrIndex) {
                    // If this $modifierOrIndex is not in our $validModifiersAndIndexes array
                    // report error
                    if (!in_array($this->parseProperty($modifierOrIndex), $validModifiersAndIndexes)) {
                        // Keep the json array structure and report error
                        $errors[$tableName][$fieldName]['columnModifier'] = "'{$modifierOrIndex}' is not a valid column modifier";
                    }
                }
            }
        }
        // Return the errors array
        return $errors;
    }

Usage Example

 /**
  * Test validateSchema()
  *
  * @return void
  */
 public function testValidateSchema()
 {
     // Set the json array schema
     $schemaArray = ['dogs' => ['name' => 'string:unique', 'paws' => 'yesTheyHaveThemSometimes:index', 'canines' => 'boolean', 'hair' => 'string(50):index', 'ears_invalid' => 'string:thisIsMyInvalidModifier', 'ears_valid' => "string:after('id')"], 'create_cats_table' => ['hair' => 'boolean'], 'posts_tags_pivot' => null];
     // Validate schema
     $errors = $this->makeMigrationJson->validateSchema($schemaArray);
     // The 'paws' section should come back with invalid column type error
     $this->assertTrue(isset($errors['dogs']['paws']['columnType']), 'columnType: "paws" was supposed to come back with a column type error, instead we got: ' . json_encode($errors));
     // The 'ears_invalid' section should come back with invalid column type error
     $this->assertTrue(isset($errors['dogs']['ears_invalid']['columnModifier']), 'columnModifier: "ears_invalid" was supposed to come back with a column modifier error, instead we got: ' . json_encode($errors));
     // The 'hair' section should not come back with errors because of its optional column type parameters
     $this->assertFalse(isset($errors['dogs']['hair']), 'columnType: "hair:string(50):index" should be allowed to be validated as a "string", not as "string(50)": ');
     // The 'ears_valid' section should not come back with errors because it is a valid column modifier or index
     $this->assertFalse(isset($errors['dogs']['ears_valid']), 'columnType: "ears_valid" should not come back with errors because it is a valid column modifier or index');
 }
All Usage Examples Of Mojopollo\Schema\MakeMigrationJson::validateSchema