Package be.selckin.ws.util.java2php

Source Code of be.selckin.ws.util.java2php.PhpWriter

package be.selckin.ws.util.java2php;

import be.selckin.ws.util.java2php.php.Argument;
import be.selckin.ws.util.java2php.php.Operation;
import be.selckin.ws.util.java2php.php.PhpClass;
import be.selckin.ws.util.java2php.php.PhpEnum;
import be.selckin.ws.util.java2php.php.PhpProperty;
import be.selckin.ws.util.java2php.php.PhpService;
import be.selckin.ws.util.java2php.php.PhpType;
import com.google.common.collect.ComparisonChain;
import com.google.common.collect.Iterables;
import com.google.common.collect.Ordering;
import com.google.common.collect.TreeMultimap;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;

public class PhpWriter {

    public void write(Php php, EndPoint endPoint) throws IOException {
        for (PhpService service : endPoint.getServices()) {
            writeService(php, service, endPoint.getTypes());
        }
    }

    private void writeService(Php php, PhpService service, List<PhpType> phpTypes) throws IOException {
        php.appendLine("");
        php.appendLine("");
        php.appendLine("// Service: " + service.getQName());

        php.startNamespace(service.getNamespace());
        php.startClass(service.getName());

        php.appendLine("");
        php.appendField("client", "null");
        php.appendLine("");

        php.startFunction("__construct", Arrays.asList(new Argument("url"), new Argument("extraConfig")));
        php.appendLine("$this->client = new \\SoapClient($url, array_merge(array(");
        php.appendLine("'style' => SOAP_DOCUMENT,");
        php.appendLine("'features' => SOAP_SINGLE_ELEMENT_ARRAYS,");
        php.appendLine("'compression' => SOAP_COMPRESSION_ACCEPT | SOAP_COMPRESSION_GZIP,");
        php.appendLine("'exceptions' => TRUE,");
        php.appendLine("'classmap' => array(");
        php.indentMore();
        writeClassMap(php, phpTypes);
        php.indentLess();
        php.appendLine("),");


        php.appendLine("), $extraConfig));");
        php.endFunction();

        for (Operation operation : service.getOperations()) {
            List<Argument> arguments = operation.getArguments();
            php.appendLine("");
            php.appendPhpDocReturn(operation.getTypeHint());
            php.startFunction(operation.getName(), arguments);

            StringBuilder call = new StringBuilder("$this->client->" + operation.getName() + "(");
            Iterator<Argument> it = arguments.iterator();
            if (it.hasNext()) {
                call.append("array(");
                String name = it.next().getName();
                call.append("\"").append(name).append("\" => ").append("$").append(name);
                while (it.hasNext()) {
                    call.append(", ");
                    name = it.next().getName();
                    call.append("\"").append(name).append(" => ").append("$").append(name);
                }

                call.append(")");
            }
            call.append(");");
            php.appendLine("return " + call.toString());
            php.endFunction();
        }


        php.endClass();
        php.endNamespace();
    }

    private void writeClassMap(Php php, List<PhpType> phpTypes) throws IOException {
        for (PhpClass phpClass : Iterables.filter(phpTypes, PhpClass.class)) {
            php.appendLine(String.format("\"%s\" => \"%s\",", phpClass.getQName().getLocalPart(), phpClass.getAbsolutePhpName().replace("\\", "\\\\")));
        }
    }

    public void write(Php code, Set<PhpType> types) throws IOException {
        for (Entry<String, Collection<PhpType>> entry : groupNs(types).asMap().entrySet()) {
            code.startNamespace(entry.getKey());
            {
                for (PhpType phpType : entry.getValue()) {
                    code.appendLine("//");
                    code.appendLine("// XML Schema item " + phpType.getQName().toString());
                    code.appendLine("//");

                    if (phpType instanceof PhpClass) {
                        write(code, (PhpClass) phpType);
                    } else if (phpType instanceof PhpEnum) {
                        write(code, (PhpEnum) phpType);
                    } else
                        throw new RuntimeException("Can't handle: " + phpType);

                }

            }
            code.endNamespace();
        }
    }

    private void write(Php code, PhpEnum anEnum) throws IOException {
        code.startInterface(anEnum.getName());
        {
            for (String value : anEnum.getValues()) {
                code.appendConst(value, value);
            }
        }
        code.endInterface();
    }

    private void write(Php code, PhpClass phpClass) throws IOException {
        code.startClass(phpClass.getName());
        {
            for (PhpProperty phpProperty : phpClass.getProperties()) {
                for (String comment : phpProperty.getComments()) {
                    code.appendLine("//" + comment);
                }
                code.appendPhpDocVar(phpProperty);
                code.appendField(phpProperty.getName(), phpProperty.getInitialValue());
            }
            code.appendLine("");

            List<PhpProperty> required = phpClass.getRequiredProperties();
            if (!required.isEmpty()) {
                List<Argument> args = new ArrayList<>();
                for (PhpProperty property : required) {
                    args.add(new Argument(property.getTypeHint(), property.getName()));
                }

                code.appendPhpDocVars(required);
                code.startFunction("__construct", args);
                {
                    for (PhpProperty property : required) {
                        code.appendLine("$this->" + property.getSetterName() + "($" + property.getName() + ");");
                    }
                }

                code.endFunction();
            }

            code.appendLine("");
            for (PhpProperty phpProperty : phpClass.getProperties()) {
                code.appendPhpDocReturn(phpProperty.getTypeHint());
                code.startFunction(phpProperty.getGetterName(), Collections.<Argument>emptyList());
                code.appendLine("return $this->" + phpProperty.getName() + ";");
                code.endFunction();

                code.appendPhpDocParam("value", phpProperty);
                if (phpProperty.getExplicitNamespace() == null) {
                    code.startFunction(phpProperty.getSetterName(), Arrays.asList(new Argument(phpProperty.getTypeHint(), "value")));
                    code.appendLine("$this->" + phpProperty.getName() + " = $value;");
                    code.endFunction();
                } else {
                    code.startFunction(phpProperty.getSetterName(), Arrays.asList(new Argument(phpProperty.getTypeHint(), "value")));
                    code.appendLine("$this->" + phpProperty.getName() + " = array_map(function($item) {");
                    code.appendLine("    if ($item instanceof \\SoapVar) {");
                    code.appendLine("        return $item;");
                    code.appendLine("    } else {");
                    code.appendLine("        return new \\SoapVar($item, SOAP_ENC_OBJECT, \"" + phpProperty.getName() + "\", \"" + phpProperty.getExplicitNamespace() + "\", null, \"" + phpProperty.getExplicitNamespace() + "\");");
                    code.appendLine("    }");
                    code.appendLine("}, $value);");
                    code.endFunction();
                }
            }
        }
        code.endClass();
    }

    private TreeMultimap<String, PhpType> groupNs(Set<PhpType> types) {
        TreeMultimap<String, PhpType> perNamespace = TreeMultimap.create(Ordering.<String>natural(), new Comparator<PhpType>() {
            @Override
            public int compare(PhpType o1, PhpType o2) {
                return ComparisonChain.start().compare(o1.getName(), o2.getName()).result();
            }
        });
        for (PhpType type : types) {
            perNamespace.put(type.getNamespace(), type);
        }
        return perNamespace;
    }
}
TOP

Related Classes of be.selckin.ws.util.java2php.PhpWriter

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.