From 0df423e84bcdb63c639a35375f6ed0039c17e2e9 Mon Sep 17 00:00:00 2001 From: Hugo Sales Date: Sun, 26 Dec 2021 22:08:39 +0000 Subject: [PATCH] [TOOLS] Update bin/generate_entity_fields so it defaults nullable variables to null and handles null in varchars --- bin/generate_entity_fields | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/bin/generate_entity_fields b/bin/generate_entity_fields index e8012b7321..adcc34fd82 100755 --- a/bin/generate_entity_fields +++ b/bin/generate_entity_fields @@ -47,18 +47,29 @@ foreach ($files as $file) { $fields_code = []; $methods_code = []; foreach ($fields as $field) { - $nullable = !@$schema['fields'][$field]['not null'] ? '?' : ''; - $type = types[$schema['fields'][$field]['type']]; + $field_schema = $schema['fields'][$field]; + $nullable = ($field_schema['not null'] ?? false) ? '' : '?'; + $type = types[$field_schema['type']]; $type = $type !== '' ? $nullable . $type : $type; $method_name = str_replace([' ', 'actor'], ['', 'Actor'], ucwords(str_replace('_', ' ', $field))); - $default = $schema['fields'][$field]['default'] ?? null; - $length = $schema['fields'][$field]['length'] ?? null; - $field_setter = \is_int($length) ? "\mb_substr(\${$field}, 0, $length)" : "\${$field}"; + $length = $field_schema['length'] ?? null; - if (isset($default) && $nullable != '?' && $type != '\DateTimeInterface') { - if (is_string($default)) { + $field_setter = "\${$field}"; + if (\is_int($length)) { + if ($nullable === '?') { + $field_setter = "\is_null(\${$field}) ? null : \mb_substr(\${$field}, 0, $length)"; + } else { + $field_setter = "\mb_substr(\${$field}, 0, $length)"; + } + } + + if (($nullable === '?' || \array_key_exists('default', $field_schema)) && $type != '\DateTimeInterface') { + $default = $field_schema['default'] ?? null; + if (\is_string($default)) { $default = "'{$default}'"; - } elseif ($type == 'bool') { + } elseif (\is_null($default)) { + $default = "null"; + } elseif ($type === 'bool' || $type === '?bool') { $default = $default ? 'true' : 'false'; }