Search in sources :

Example 51 with ForeachIterator

use of php.runtime.lang.ForeachIterator in project jphp by jphp-compiler.

the class ArrayFunctions method prev.

public static Memory prev(Environment env, TraceInfo trace, @Reference Memory array) {
    if (expectingReference(env, trace, array, "prev")) {
        if (expecting(env, trace, 1, array, ARRAY)) {
            ArrayMemory memory = array.toValue(ArrayMemory.class);
            ForeachIterator iterator = memory.getCurrentIterator();
            if (iterator.prev())
                return iterator.getValue().toImmutable();
            else
                return Memory.FALSE;
        }
    }
    return Memory.FALSE;
}
Also used : ArrayMemory(php.runtime.memory.ArrayMemory) ForeachIterator(php.runtime.lang.ForeachIterator)

Example 52 with ForeachIterator

use of php.runtime.lang.ForeachIterator in project jphp by jphp-compiler.

the class ArrayFunctions method array_chunk.

public static Memory array_chunk(Environment env, TraceInfo trace, Memory input, int size, boolean saveKeys) {
    if (expecting(env, trace, 1, input, ARRAY)) {
        if (size < 1) {
            env.warning(trace, "array_chunk(): Size parameter expected to be greater than 0");
            return Memory.NULL;
        }
        ArrayMemory result = new ArrayMemory();
        ArrayMemory item = null;
        ForeachIterator iterator = input.getNewIterator(env, false, false);
        int i = 0;
        while (iterator.next()) {
            if (i == 0) {
                item = new ArrayMemory();
                result.add(item);
            }
            if (saveKeys) {
                item.put(iterator.getKey(), iterator.getValue().toImmutable());
            } else
                item.add(iterator.getValue().toImmutable());
            if (++i == size)
                i = 0;
        }
        return result.toConstant();
    } else
        return Memory.NULL;
}
Also used : ArrayMemory(php.runtime.memory.ArrayMemory) ForeachIterator(php.runtime.lang.ForeachIterator)

Example 53 with ForeachIterator

use of php.runtime.lang.ForeachIterator in project jphp by jphp-compiler.

the class PHttpClient method makeConnection.

@Signature
protected URLConnection makeConnection(PHttpRequest request, Environment env) throws Throwable {
    URL url = new URL(request.getUrl());
    URLConnection connection = proxy == null ? url.openConnection() : url.openConnection(proxy);
    if (connection instanceof HttpURLConnection) {
        HttpURLConnection httpURLConnection = (HttpURLConnection) connection;
        httpURLConnection.setReadTimeout(timeout);
        httpURLConnection.setConnectTimeout(timeout);
        httpURLConnection.setRequestMethod(request.getMethod());
        httpURLConnection.setDoInput(true);
        httpURLConnection.setDoOutput(true);
        // cookies.
        ArrayMemory cookies = request.getCookies();
        ForeachIterator iterator = cookies.foreachIterator(false, false);
        StringBuilder cookieHeader = new StringBuilder();
        while (iterator.next()) {
            String name = iterator.getStringKey();
            String value = iterator.getValue().toString();
            cookieHeader.append(URLEncoder.encode(name, "UTF-8")).append('=').append(URLEncoder.encode(value, "UTF-8")).append(';');
        }
        httpURLConnection.setRequestProperty("Cookie", cookieHeader.toString());
        ArrayMemory headers = request.getHeaders();
        iterator = headers.foreachIterator(false, false);
        while (iterator.next()) {
            String name = iterator.getStringKey();
            Memory value = iterator.getValue();
            if (value.isTraversable()) {
                ForeachIterator subIterator = value.getNewIterator(env);
                while (subIterator.next()) {
                    httpURLConnection.addRequestProperty(name, subIterator.getValue().toString());
                }
            } else {
                httpURLConnection.addRequestProperty(name, value.toString());
            }
        }
        Memory body = request.getBody(env);
        if (body.instanceOf(PHttpBody.class)) {
            body.toObject(PHttpBody.class).apply(env, ObjectMemory.valueOf(new WrapURLConnection(env, connection)));
        } else {
            OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
            writer.write(request.getBody(env).toBinaryString());
            writer.close();
        }
        return httpURLConnection;
    } else {
        throw new IllegalStateException("HttpClient::makeConnection() method must return instance of HttpURLConnection");
    }
}
Also used : ArrayMemory(php.runtime.memory.ArrayMemory) ForeachIterator(php.runtime.lang.ForeachIterator) Memory(php.runtime.Memory) ArrayMemory(php.runtime.memory.ArrayMemory) ObjectMemory(php.runtime.memory.ObjectMemory) StringMemory(php.runtime.memory.StringMemory) WrapURLConnection(php.runtime.ext.net.WrapURLConnection) WrapURLConnection(php.runtime.ext.net.WrapURLConnection)

Example 54 with ForeachIterator

use of php.runtime.lang.ForeachIterator in project jphp by jphp-compiler.

the class PHttpMultipartFormBody method apply.

@Override
@Signature
public Memory apply(Environment env, Memory... args) throws IOException {
    WrapURLConnection connection = args[0].toObject(WrapURLConnection.class);
    URLConnection conn = connection.getWrappedObject();
    OutputStream out = connection.getOutputStream();
    conn.setRequestProperty("Cache-Control", "no-cache");
    conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + this.boundary);
    ForeachIterator iterator = fields.foreachIterator(false, false);
    while (iterator.next()) {
        StringBuilder sb = new StringBuilder();
        String name = iterator.getStringKey();
        Memory value = iterator.getValue();
        sb.append("--").append(boundary).append(CRLF);
        sb.append("Content-Disposition: form-data; name=\"").append(name).append("\"").append(CRLF);
        sb.append("Content-Type: text/plain; charset=").append(encoding).append(CRLF);
        sb.append(CRLF).append(value).append(CRLF);
        out.write(sb.toString().getBytes());
    }
    iterator = files.foreachIterator(false, false);
    while (iterator.next()) {
        StringBuilder sb = new StringBuilder();
        String name = iterator.getStringKey();
        String fileName = name;
        Memory value = iterator.getValue();
        InputStream inputStream = Stream.getInputStream(env, value);
        if (inputStream == null) {
            throw new IllegalStateException("File '" + name + "' is not valid file or stream");
        }
        if (value.instanceOf(Stream.class)) {
            Stream stream = value.toObject(Stream.class);
            fileName = new File(stream.getPath()).getName();
        } else {
            fileName = new File(value.toString()).getName();
        }
        sb.append("--").append(boundary).append(CRLF).append("Content-Disposition: form-data; name=\"").append(URLEncoder.encode(name, encoding)).append("\"; filename=\"").append(URLEncoder.encode(fileName, encoding)).append("\"").append(CRLF);
        sb.append("Content-Type: ").append(URLConnection.guessContentTypeFromName(fileName)).append(CRLF);
        sb.append("Content-Transfer-Encoding: binary").append(CRLF).append(CRLF);
        out.write(sb.toString().getBytes());
        byte[] buff = new byte[4096];
        int len;
        while ((len = inputStream.read(buff)) > 0) {
            out.write(buff, 0, len);
        }
        Stream.closeStream(env, inputStream);
        sb.append(CRLF);
    }
    out.write(("--" + boundary + "--").getBytes());
    out.write(CRLF.getBytes());
    return Memory.NULL;
}
Also used : Memory(php.runtime.Memory) ArrayMemory(php.runtime.memory.ArrayMemory) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) WrapURLConnection(php.runtime.ext.net.WrapURLConnection) URLConnection(java.net.URLConnection) ForeachIterator(php.runtime.lang.ForeachIterator) OutputStream(java.io.OutputStream) Stream(php.runtime.ext.core.classes.stream.Stream) InputStream(java.io.InputStream) WrapURLConnection(php.runtime.ext.net.WrapURLConnection) File(java.io.File) Signature(php.runtime.annotation.Reflection.Signature)

Example 55 with ForeachIterator

use of php.runtime.lang.ForeachIterator in project jphp by jphp-compiler.

the class StringFunctions method substr_replace.

@Immutable
public static Memory substr_replace(Environment env, TraceInfo trace, Memory string, Memory replacementM, Memory startM, Memory lengthM) {
    int start = 0;
    int length = Integer.MAX_VALUE / 2;
    String replacement = "";
    ForeachIterator replacementIterator = null;
    if (replacementM.isArray())
        replacementIterator = replacementM.getNewIterator(env, false, false);
    else
        replacement = replacementM.toString();
    ForeachIterator startIterator = null;
    if (startM.isArray())
        startIterator = startM.getNewIterator(env, false, false);
    else
        start = startM.toInteger();
    ForeachIterator lengthIterator = null;
    if (lengthM.isArray())
        lengthIterator = lengthM.getNewIterator(env, false, false);
    else
        length = lengthM.toInteger();
    if (string.isArray()) {
        ArrayMemory resultArray = new ArrayMemory();
        ForeachIterator iterator = string.getNewIterator(env, false, false);
        while (iterator.next()) {
            String value = iterator.getValue().toString();
            if (replacementIterator != null && replacementIterator.next())
                replacement = replacementIterator.getValue().toString();
            if (lengthIterator != null && lengthIterator.next())
                length = lengthIterator.getValue().toInteger();
            if (startIterator != null && startIterator.next())
                start = startIterator.getValue().toInteger();
            String result = _substr_replace(value, replacement, start, length);
            resultArray.add(new StringMemory(result));
        }
        return resultArray.toConstant();
    } else {
        if (replacementIterator != null && replacementIterator.next())
            replacement = replacementIterator.getValue().toString();
        if (lengthIterator != null && lengthIterator.next())
            length = lengthIterator.getValue().toInteger();
        if (startIterator != null && startIterator.next())
            start = startIterator.getValue().toInteger();
        return new StringMemory(_substr_replace(string.toString(), replacement, start, length));
    }
}
Also used : ForeachIterator(php.runtime.lang.ForeachIterator) Immutable(php.runtime.annotation.Runtime.Immutable)

Aggregations

ForeachIterator (php.runtime.lang.ForeachIterator)100 ArrayMemory (php.runtime.memory.ArrayMemory)49 Memory (php.runtime.Memory)47 LongMemory (php.runtime.memory.LongMemory)22 KeyValueMemory (php.runtime.memory.KeyValueMemory)18 ReferenceMemory (php.runtime.memory.ReferenceMemory)16 Invoker (php.runtime.invoke.Invoker)12 ObjectMemory (php.runtime.memory.ObjectMemory)12 IObject (php.runtime.lang.IObject)9 ArrayKeyMemory (php.runtime.memory.helper.ArrayKeyMemory)6 ArrayValueMemory (php.runtime.memory.helper.ArrayValueMemory)6 ShortcutMemory (php.runtime.memory.helper.ShortcutMemory)5 Signature (php.runtime.annotation.Reflection.Signature)4 StringMemory (php.runtime.memory.StringMemory)4 File (java.io.File)3 HashSet (java.util.HashSet)3 Element (org.w3c.dom.Element)3 FastMethod (php.runtime.annotation.Runtime.FastMethod)3 ClassEntity (php.runtime.reflection.ClassEntity)3 PropertyEntity (php.runtime.reflection.PropertyEntity)3