diff --git a/src/idl_gen_php.cpp b/src/idl_gen_php.cpp index 68ddc8171..31b05b505 100644 --- a/src/idl_gen_php.cpp +++ b/src/idl_gen_php.cpp @@ -25,14 +25,74 @@ namespace flatbuffers { namespace php { + // Hardcode spaces per indentation. + const std::string Indent = " "; + class PhpGenerator : public BaseGenerator { + public: + PhpGenerator(const Parser &parser, const std::string &path, + const std::string &file_name) + : BaseGenerator(parser, path, file_name){}; + bool generate() { + if (!generateEnums()) return false; + if (!generateStructs()) return false; + return true; + } - static std::string GenGetter(const Type &type); - static std::string GenDefaultValue(const Value &value); - static std::string GenMethod(const FieldDef &field); - static void GenStructBuilder(const StructDef &struct_def, - std::string *code_ptr); - static std::string GenTypeBasic(const Type &type); - static std::string GenTypeGet(const Type &type); + private: + bool generateEnums() { + for (auto it = parser_.enums_.vec.begin(); + it != parser_.enums_.vec.end(); ++it) { + auto &enum_def = **it; + std::string enumcode; + GenEnum(enum_def, &enumcode); + if (!SaveType(enum_def, enumcode, false)) return false; + } + return true; + } + + bool generateStructs() { + for (auto it = parser_.structs_.vec.begin(); + it != parser_.structs_.vec.end(); ++it) { + auto &struct_def = **it; + std::string declcode; + GenStruct(parser_, struct_def, &declcode); + if (!SaveType(struct_def, declcode, true)) return false; + } + return true; + } + + // Begin by declaring namespace and imports. + void BeginFile(const std::string name_space_name, + const bool needs_imports, std::string *code_ptr) { + std::string &code = *code_ptr; + code += "offset();\n"; code += Indent + "}\n"; } - - class PhpGenerator : public BaseGenerator { - public: - PhpGenerator(const Parser &parser, const std::string &path, - const std::string &file_name) - : BaseGenerator(parser, path, file_name){}; - bool generate() { - if (!generateEnums()) return false; - if (!generateStructs()) return false; - return true; - } - - private: - bool generateEnums() { - for (auto it = parser_.enums_.vec.begin(); - it != parser_.enums_.vec.end(); ++it) { - auto &enum_def = **it; - std::string enumcode; - GenEnum(enum_def, &enumcode); - if (!SaveType(enum_def, enumcode, false)) return false; - } - return true; - } - - bool generateStructs() { - for (auto it = parser_.structs_.vec.begin(); - it != parser_.structs_.vec.end(); ++it) { - auto &struct_def = **it; - std::string declcode; - GenStruct(parser_, struct_def, &declcode); - if (!SaveType(struct_def, declcode, true)) return false; - } - return true; - } - - // Begin by declaring namespace and imports. - void BeginFile(const std::string name_space_name, - const bool needs_imports, std::string *code_ptr) { - std::string &code = *code_ptr; - code += "