Enclose values if needed
- only used by unparse()

@param string $value     Cell value to process
@param string $delimiter Character to put between cells on the same row

@return string Processed value
327020
protected function _enclose_value($value = null, $delimiter) {
        if ($value !== null && $value != '') {
            $delimiter_quoted = $delimiter ?
                preg_quote($delimiter, '/') . "|"
                : '';
            $enclosure_quoted = preg_quote($this->enclosure, '/');
            $pattern = "/" . $delimiter_quoted . $enclosure_quoted . "|\n|\r/i";
            if ($this->enclose_all || preg_match($pattern, $value) || ($value{0} == ' ' || substr($value, -1) == ' ')) {
                $value = str_replace($this->enclosure, $this->enclosure . $this->enclosure, $value);
                $value = $this->enclosure . $value . $this->enclosure;
            }
        }

        return $value;
    }
581042
public function consumeString($delimiter, $type)
    {
        $close = Utils::balanceDelimiter($delimiter);

        $balanced = $close !== $delimiter;
        $patterns = array('/(?<!\\\\)((?:\\\\\\\\)*)(' . preg_quote($close, '/') . ')/');

        if ($balanced) {
            $patterns[] = '/(?<!\\\\)((?:\\\\\\\\)*)(' . preg_quote($delimiter, '/') . ')/';
        }

        $stack = 1; // we're already inside the string
        $start = $this->pos();
        $closeDelimiterMatch = null;
        while ($stack) {
            $next = $this->getNext($patterns);
            if ($next[0] === -1) {
                $this->terminate();
                $finish = $this->pos();
                break;
            } elseif ($balanced && $next[1][2] === $delimiter) {
                $stack++;
                $finish = $next[0] + strlen($next[1][0]);
            } elseif ($next[1][2] === $close) {
                $stack--;
                if (!$stack) {
                    $closeDelimiterMatch = $next[1][2];
                }
                $finish = $next[0] + strlen($next[1][1]);
            } else {
                assert(0);
            }
            $this->pos($next[0] + strlen($next[1][0]));
        }
        $substr = substr($this->string(), $start, $finish - $start);
        // special case for qw, the string is not a 'STRING', it is actually
        // a whitespace separated list of strings. So we need to split it and
        // record them separately
        if ($type === 'SPLIT_STRING') {
            foreach (preg_split('/(\s+)/', $substr, -1, PREG_SPLIT_DELIM_CAPTURE) as $token) {
                if (preg_match('/^\s/', $token)) {
                    $this->record($token, null);
                } else {
                    $this->record($token, 'STRING');
                }
            }
        } else {
            $this->record($substr, $type);
        }
        if ($closeDelimiterMatch !== null) {
            $this->record($closeDelimiterMatch, 'DELIMITER');
        }
    }
1234171
private function _doLiteralBlock($value,$indent) {
    if ($value === "\n") return '\n';
    if (strpos($value, "\n") === false && strpos($value, "'") === false) {
      return sprintf ("'%s'", $value);
    }
    if (strpos($value, "\n") === false && strpos($value, '"') === false) {
      return sprintf ('"%s"', $value);
    }
    $exploded = explode("\n",$value);
    $newValue = '|';
    if (isset($exploded[0]) && ($exploded[0] == "|" || $exploded[0] == "|-" || $exploded[0] == ">")) {
        $newValue = $exploded[0];
        unset($exploded[0]);
    }
    $indent += $this->_dumpIndent;
    $spaces   = str_repeat(' ',$indent);
    foreach ($exploded as $line) {
      $line = trim($line);
      if (strpos($line, '"') === 0 && strrpos($line, '"') == (strlen($line)-1) || strpos($line, "'") === 0 && strrpos($line, "'") == (strlen($line)-1)) {
        $line = substr($line, 1, -1);
      }
      $newValue .= "\n" . $spaces . ($line);
    }
    return $newValue;
  }
710479
protected function splitQueryValue($value)
    {
        $data = explode('=', $value, 2);
        if (1 === count($data)) {
            $data[] = null;
        }
        return $data;
    }
354424
public function append($key, $value, $delimiter = ',')
    {
        $newValue = $value;
        if (isset($this->properties[$key]) && !empty($this->properties[$key])) {
            $newValue = $this->properties[$key] . $delimiter . $value;
        }
        $this->properties[$key] = $newValue;
    }
1005634
public function concat(string $delimiter = Text::BLANK_TEXT): Text
    {
        $imploded = implode($delimiter, $this->toArrayOfStrings());

        return Text::make($imploded);
    }
356319
public static function case2camel($string, $delimiter = '_')
    {
        $reg = '/' . $delimiter . '([a-z])/';
        return preg_replace_callback($reg, function ($mathes) use ($delimiter) {
            return trim(strtoupper($mathes[0]), $delimiter);
        }, $string);
    }
639279
public function set($name, $value, $delimiter = self::DELIMITER) {
		if ($this->_locked) {
			throw new ReadOnly('Config is read only');
		}
		
		if (strpos($name, $delimiter) === false) {
			$this->_data[$name] = is_array($value)
				? new static($value, false)
				: $value;
			
			return $this;
		}
		
		list ($first, $rest) = explode($delimiter, $name, 2);
		
		if (! isset($this->_data[$first]) || ! $this->_data[$first] instanceof self) {
			$this->_data[$first] = new static();
		}
		
		$this->_data[$first]->set($rest, $value, $delimiter);
		
		return $this;
	}
495950
private function processEnclosedField($value, array $option)
    {
        // then, remove enclosure and line feed
        $cell = $this->trimEnclosure($value, $option['enclosure']);
        // replace the escape sequence "" to "
        $cell = $this->unescapeEnclosure($cell, $option['enclosure']);

        $this->setCell($cell);
        $this->col++;
        $this->continue = false;
    }
818889
protected function _prepareValue(&$value)
        {
            switch ($value[0]) {
                case pdf_parser::TYPE_STRING:
                    if ($this->encrypted) {
                        $value[1] = $this->_unescape($value[1]);
                        $value[1] = $this->_encrypt_data($this->_currentObjId, $value[1]);
                        $value[1] = TCPDF_STATIC::_escape($value[1]);
                    }
                    break;

                case pdf_parser::TYPE_STREAM:
                    if ($this->encrypted) {
                        $value[2][1] = $this->_encrypt_data($this->_currentObjId, $value[2][1]);
                        $value[1][1]['/Length'] = array(
                            pdf_parser::TYPE_NUMERIC,
                            strlen($value[2][1])
                        );
                    }
                    break;

                case pdf_parser::TYPE_HEX:
                    if ($this->encrypted) {
                        $value[1] = $this->hex2str($value[1]);
                        $value[1] = $this->_encrypt_data($this->_currentObjId, $value[1]);

                        // remake hexstring of encrypted string
                        $value[1] = $this->str2hex($value[1]);
                    }
                    break;
            }
        }
1195383
public function wrap($value, $prefixAlias = false)
    {
        if ($this->isExpression($value)) {
            return $this->getValue($value);
        }

        // If the value being wrapped has a column alias we will need to separate out
        // the pieces so we can wrap each of the segments of the expression on it
        // own, and then joins them both back together with the "as" connector.
        if (strpos(strtolower($value), ' as ') !== false) {
            return $this->wrapAliasedValue($value, $prefixAlias);
        }

        return $this->wrapSegments(explode('.', $value));
    }
444492
public static function clearTextConcat($value)
    {
        if (is_string($value)) {
            return trim(preg_replace('/\s+/s', ' ', $value));
        }
        if (is_array($value)) {
            $result = [];
            foreach ($value as $val) {
                $result[] = self::clearTextConcat($val);
            }
            return implode(' ', array_filter($result, 'strlen'));
        }
        return null;
    }
467382
public function split(string $delimiter = null): StreamInterface
    {
        if (\is_null($delimiter) || $delimiter === '') {
            return $this->chunk();
        }

        $parts = \explode($delimiter, $this->value);
        $stream = new Stream(self::class);

        foreach ($parts as $part) {
            $stream = $stream->add(new self($part, $this->encoding));
        }

        return $stream;
    }
1305698
public function wrapSanitizer($value)
    {
        // Its a raw query, just cast as string, object has __toString()
        if ($value instanceof Raw) {
            return (string)$value;
        }

        if ($value instanceof \Closure) {
            return $value;
        }

        // Separate our table and fields which are joined with a ".", like my_table.id
        $valueArr = explode('.', $value, 2);

        foreach ($valueArr as $key => $subValue) {
            // Don't wrap if we have *, which is not a usual field
            $valueArr[$key] = trim($subValue) === '*' ? $subValue : static::SANITIZER . $subValue . static::SANITIZER;
        }

        // Join these back with "." and return
        return implode('.', $valueArr);
    }
217035
protected static function generateRow(array $row, $delimiter, $enclose)
    {
        array_walk($row, function (&$item) use ($enclose) {
            if (is_bool($item)) {
                $item = (int) $item;
            } elseif (is_null($item)) {
                $item = '';
            } elseif (!is_scalar($item)) {
                $item = "[array]";
            }
            $item = $enclose.Html::encode($item).$enclose;
        });

        return implode($delimiter, $row) . PHP_EOL;
    }
852739
protected function parseAsMultiple($value, $separator = ',')
    {
        if (is_array($value) || $value instanceof Traversable) {
            $parsed = [];
            foreach ($value as $val) {
                if (empty($val) && !is_numeric($val)) {
                    continue;
                }

                $parsed[] = $val;
            }

            return $parsed;
        }

        if ($separator instanceof PropertyInterface) {
            $separator = $separator->multipleSeparator();
        }

        if (is_object($value) && method_exists($value, '__toString')) {
            $value = strval($value);
        }

        if (empty($value) && !is_numeric($value)) {
            return [];
        }

        /**
         * This property is marked as "multiple".
         * Manually handling the resolution to array
         * until the property itself manages this.
         */
        if (is_string($value)) {
            return explode($separator, $value);
        }

        /**
         * If the parameter isn't an array yet,
         * means we might be dealing with an integer,
         * an empty string, or an object.
         */
        if (!is_array($value)) {
            return (array)$value;
        }

        return $value;
    }
189016
protected function splitCommaSeparatedValues($value)
    {
        if (!is_array($value) || (count($value) == 1) && is_string(current($value))) {
            $value = (array) $value;
            $value = explode(',', $value[0]);
        }

        return $value;
    }