public function testSurround() { $cases = ['normal' => ['value' => 'Allen Linatoc', 'expect' => '%sAllen Linatoc%s'], 'redundant' => ['value' => 'Allen Linatoc"', 'expect' => '%sAllen Linatoc"%s'], 'escape' => ['value' => 'Allen \'Linatoc', 'expect' => '%sAllen \\\'Linatoc%s']]; foreach ($this->fences as $left => $right) { foreach ($cases as $casename => $casedata) { $value = $casedata['value']; $expect = sprintf($casedata['expect'], $left, $right); $result = \Utils\String::Surround($value, $left, $casename == 'redundant', $casename == 'escape'); $this->assertEqual($expect, $result, sprintf("%s != %s, casename='{$casename}', fence='{$left}{$right}'", $expect, $result)); } } }
/** * Convert a runtime value into a virtual runtime value. Returns FALSE if value can't be determined. * * @param string $value Any scalar value, EXCEPT if it's an array. * @param boolean $throw [optional] If an exception will be thrown once a virtual runtime value can't be determined. This is useful when determining complex array values. * @return string|boolean */ public static function GetVirtual($value, $throw = true) { if (is_string($value)) { return String::Surround($value, "'"); } else { if (is_array($value)) { $closure_for_assoc = function ($current) { $newresult = []; foreach ($current as $key => $value) { array_push($newresult, sprintf("%s => %s", self::GetVirtual($key), $value)); // note that we didn't get Virtual value of $value since this has been previously virualized using array_map() } return $newresult; }; $result = array_map(function ($current) { return self::GetVirtual($current); }, $value); $result = str("[ {0} ]", implode(", ", Arrays::IsIndexed($result) ? $result : $closure_for_assoc($result))); return $result; } else { if (is_bool($value)) { return $value ? 'true' : 'false'; } else { if (is_null($value)) { return "null"; } else { if (is_scalar($value)) { return (string) $value; } } } } } if ($throw) { throw new ScalarValueException($value); } return false; }