src/CriteriaLexer/ParsedCriteria.php
protected function _buildStringFromParsed(array $parts, $params = [])
    {
        if ($params instanceof \Traversable)
            $params = StdTravers::of($params)->toArray();

        if (! is_array($params) )
            throw new \InvalidArgumentException(sprintf(
                'Parameters must be an array or Traversable; given: (%s).'
                , \Poirot\Std\flatten($params)
            ));


        // regard to recursive function call
        $isOptional = false;
        $args = func_get_args();
        if ($args && isset($args[2]))
            $isOptional = $args[2];

        # Check For Presented Values in Optional Segment
        // consider this "/[@:username{{\w+}}][-:userid{{\w+}}]"
        // if username not presented as params injected then first part include @ as literal
        // not rendered.
        // optional part only render when all parameter is present
        if ($isOptional) {
            foreach ($parts as $parsed) {
                if (! isset($parsed['_parameter_']) )
                    continue;

                ## need parameter
                $neededParameterName = $parsed['_parameter_'];
                if (! (isset($params[$neededParameterName]) && $params[$neededParameterName] !== '') )
                    return '';
            }
        }


        $return    = '';
        // [0 => ['_literal_' => 'localhost'], 1=>['_optional' => ..] ..]
        foreach ($parts as $parsed) {
            $definition_name  = key($parsed);
            $definition_value = $parsed[$definition_name];
            // $parsed can also have extra parsed data options
            // _parameter_ String(3) => tld \
            // tld String(4)         => .com
            switch ($definition_name)
            {
                case '_literal_':
                    $return .= $definition_value;
                    break;

                case '_parameter_':
                    if (! isset($params[$definition_value]) ) {
                        if ($isOptional)  return '';

                        throw new \InvalidArgumentException(sprintf(
                            'Missing parameter (%s).'
                            , $definition_value
                        ));
                    }

                    if (! \Poirot\Std\isStringify($params[$definition_value]) )
                        throw new \Exception(sprintf(
                            'Parameter %s is not stringify; given: (%s).'
                            , $definition_value
                            , \Poirot\Std\flatten($params[$definition_value])
                        ));

                    $return .= $params[$definition_value];
                    break;

                case '_optional_':
                    $optionalPart = $this->_buildStringFromParsed($definition_value, $params, true);
                    if ($optionalPart !== '')
                        $return .= $optionalPart;

                    break;
            }
        }

        return $return;
    }
protected function _buildStringFromParsed(array $parts, $params = [])
    {
        if ($params instanceof \Traversable)
            $params = StdTravers::of($params)->toArray();

        if (! is_array($params) )
            throw new \InvalidArgumentException(sprintf(
                'Parameters must be an array or Traversable; given: (%s).'
                , \Poirot\Std\flatten($params)
            ));


        // regard to recursive function call
        $isOptional = false;
        $args = func_get_args();
        if ($args && isset($args[2]))
            $isOptional = $args[2];

        # Check For Presented Values in Optional Segment
        // consider this "/[@:username{{\w+}}][-:userid{{\w+}}]"
        // if username not presented as params injected then first part include @ as literal
        // not rendered.
        // optional part only render when all parameter is present
        if ($isOptional) {
            foreach ($parts as $parsed) {
                if (! isset($parsed['_parameter_']) )
                    continue;

                ## need parameter
                $neededParameterName = $parsed['_parameter_'];
                if (! (isset($params[$neededParameterName]) && $params[$neededParameterName] !== '') )
                    return '';
            }
        }


        $return    = '';
        // [0 => ['_literal_' => 'localhost'], 1=>['_optional' => ..] ..]
        foreach ($parts as $parsed) {
            $definition_name  = key($parsed);
            $definition_value = $parsed[$definition_name];
            // $parsed can also have extra parsed data options
            // _parameter_ String(3) => tld \
            // tld String(4)         => .com
            switch ($definition_name)
            {
                case '_literal_':
                    $return .= $definition_value;
                    break;

                case '_parameter_':
                    if (! isset($params[$definition_value]) ) {
                        if ($isOptional)  return '';

                        throw new \InvalidArgumentException(sprintf(
                            'Missing parameter (%s).'
                            , $definition_value
                        ));
                    }

                    if (! \Poirot\Std\isStringify($params[$definition_value]) )
                        throw new \Exception(sprintf(
                            'Parameter %s is not stringify; given: (%s).'
                            , $definition_value
                            , \Poirot\Std\flatten($params[$definition_value])
                        ));

                    $return .= $params[$definition_value];
                    break;

                case '_optional_':
                    $optionalPart = $this->_buildStringFromParsed($definition_value, $params, true);
                    if ($optionalPart !== '')
                        $return .= $optionalPart;

                    break;
            }
        }

        return $return;
    }
protected function _buildStringFromParsed ( array $ parts , $ params = [ ] ) { if ( $ params instanceof \ Traversable ) $ params = StdTravers :: of ( $ params ) -> toArray ( ) ; if ( ! is_array ( $ params ) ) throw new \ InvalidArgumentException ( sprintf ( 'Parameters must be an array or Traversable; given: (%s).' , \ Poirot \ Std \ flatten ( $ params ) ) ) ; // regard to recursive function call $ isOptional = false ; $ args = func_get_args ( ) ; if ( $ args && isset ( $ args [ 2 ] ) ) $ isOptional = $ args [ 2 ] ; # Check For Presented Values in Optional Segment // consider this "/[@:username{{\w+}}][-:userid{{\w+}}]" // if username not presented as params injected then first part include @ as literal // not rendered. // optional part only render when all parameter is present if ( $ isOptional ) { foreach ( $ parts as $ parsed ) { if ( ! isset ( $ parsed [ '_parameter_' ] ) ) continue ; ## need parameter $ neededParameterName = $ parsed [ '_parameter_' ] ; if ( ! ( isset ( $ params [ $ neededParameterName ] ) && $ params [ $ neededParameterName ] !== '' ) ) return '' ; } } $ return = '' ; // [0 => ['_literal_' => 'localhost'], 1=>['_optional' => ..] ..] foreach ( $ parts as $ parsed ) { $ definition_name = key ( $ parsed ) ; $ definition_value = $ parsed [ $ definition_name ] ; // $parsed can also have extra parsed data options // _parameter_ String(3) => tld \ // tld String(4)         => .com switch ( $ definition_name ) { case '_literal_' : $ return .= $ definition_value ; break ; case '_parameter_' : if ( ! isset ( $ params [ $ definition_value ] ) ) { if ( $ isOptional ) return '' ; throw new \ InvalidArgumentException ( sprintf ( 'Missing parameter (%s).' , $ definition_value ) ) ; } if ( ! \ Poirot \ Std \ isStringify ( $ params [ $ definition_value ] ) ) throw new \ Exception ( sprintf ( 'Parameter %s is not stringify; given: (%s).' , $ definition_value , \ Poirot \ Std \ flatten ( $ params [ $ definition_value ] ) ) ) ; $ return .= $ params [ $ definition_value ] ; break ; case '_optional_' : $ optionalPart = $ this -> _buildStringFromParsed ( $ definition_value , $ params , true ) ; if ( $ optionalPart !== '' ) $ return .= $ optionalPart ; break ; } } return $ return ; }
Build String Representation From Given Parts and Params

@param array              $parts
@param array|\Traversable $params

@return string
Build String Representation From Given Parts and Params