phprs\ezsql\impls\WhereImpl::condition PHP Method

condition() public static method

public static condition ( $context, $prefix, $expr, $args )
    public static function condition($context, $prefix, $expr, $args)
    {
        if (!empty($expr)) {
            if ($args) {
                //因为PDO不支持绑定数组变量, 这里需要手动展开数组
                //也就是说把 where("id IN(?)", [1,2])  展开成 where("id IN(?,?)", 1,2)
                $cutted = null;
                $cut = null;
                $toReplace = array();
                $newArgs = array();
                //找到所有数组对应的?符位置
                foreach ($args as $k => $arg) {
                    if (is_array($arg) || is_a($arg, 'phprs\\ezsql\\Native')) {
                        if (!$cutted) {
                            $cut = new NestedStringCut($expr);
                            $cutted = $cut->getText();
                        }
                        //找到第$k个?符
                        $pos = self::findQ($cutted, 0, $k);
                        $pos = $cut->mapPos($pos);
                        Verify::isTrue($pos !== false, new \InvalidArgumentException("unmatched params and ? @ {$expr}"));
                        if (is_array($arg)) {
                            $stubs = [];
                            foreach ($arg as $i) {
                                if (is_a($i, 'phprs\\ezsql\\Native')) {
                                    $stubs[] = strval($i);
                                } else {
                                    $stubs[] = '?';
                                    $newArgs[] = $i;
                                }
                            }
                            $stubs = implode(',', $stubs);
                        } else {
                            $stubs = strval($arg);
                        }
                        $toReplace[] = [$pos, $stubs];
                    } else {
                        $newArgs[] = $arg;
                    }
                }
                if (count($toReplace)) {
                    $toReplace = array_reverse($toReplace);
                    foreach ($toReplace as $i) {
                        list($pos, $v) = $i;
                        $expr = substr($expr, 0, $pos) . $v . substr($expr, $pos + 1);
                    }
                    $args = $newArgs;
                }
            }
            $context->appendSql($prefix . ' ' . $expr);
            if ($args) {
                $context->appendParams($args);
            }
        }
    }