def v1_generate_simple_serialize_method(method_name: str, fields: List[SerializingField]) -> List[StatementBlock]: arr_elements = [] for field in fields: lval = '$this->{0}'.format(field.name()) substituted_lval = lval if field.convert_func() is not None: substituted_lval = field.convert_func().format(expr=substituted_lval) arr_elements.append(ArrayElementDeclaration( field.serialized_name(), AnyEvaluation("is_null({0}) ? null : {1}".format(lval, substituted_lval), Type.any()) )) class_fields = [ DocCommentStatement( None, [ Annotation.returns("array") ] ), MethodDeclaration( Identifier(method_name), Type.array(), AccessModifier.public(), False, ArgumentListDeclaration([]), MethodBody([ ReturnStatement(ArrayDeclaration(True, arr_elements)) ]) ) ] return class_fields
def v1_generate_members(spec: Spec) -> List[StatementBlock]: class_fields = [] for field in spec.fields(): class_fields.append(DocCommentStatement( field.comment(), [ Annotation.types(field.type()) ] )) class_fields.append(MemberDeclaration( Identifier(field.name()), Type(field.type()), AccessModifier.public(), False )) return class_fields
def v1_generate_simple_deserialize_method( method_name: str, clazz: str, n: int, fields: List[DeserializingField] ) -> List[StatementBlock]: parameters = [] for index in range(n): parameters.append(AnyEvaluation("null", Type.any())) for field in fields: lval = '$data["{0}"]'.format(field.serialized_name()) substituted_lval = lval if field.convert_func() is not None: substituted_lval = field.convert_func().format(expr=substituted_lval) parameters[field.position()] = AnyEvaluation( "isset({0}) ? {1} : null".format(lval, substituted_lval), Type(field.type()) ) class_fields = [ DocCommentStatement( None, [ Annotation.param("data", Type.dictionary().represent()) ] ), MethodDeclaration( Identifier(method_name), Type(clazz), AccessModifier.public(), True, ArgumentListDeclaration([ ArgumentDeclaration(Identifier("data"), Type.dictionary()), ]), MethodBody([ ReturnStatement( InvocationStatement( NamedCallableReference("new {0}".format(clazz)), Type(clazz), ParameterList(parameters) ) ) ]) ) ] return class_fields
def instanceof(): return UnaryOperator(Type.boolean(), "instanceof")
def not_double_equal(): return UnaryOperator(Type.boolean(), "!=")
def not_triple_equal(): return UnaryOperator(Type.boolean(), "!==")
def less_than_or_equal(): return UnaryOperator(Type.boolean(), "<=")
def less_than(): return UnaryOperator(Type.boolean(), "<")
def great_than_or_equal(): return UnaryOperator(Type.boolean(), ">=")
def great_than(): return UnaryOperator(Type.boolean(), ">")
def double_equal(): return UnaryOperator(Type.boolean(), "==")
def triple_equal(): return UnaryOperator(Type.boolean(), "===")
def type(self) -> Type: return Type.array()