4 * This file is part of the Prophecy.
5 * (c) Konstantin Kudryashov <ever.zet@gmail.com>
6 * Marcello Duarte <marcello.duarte@gmail.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Prophecy\Util;
14 use Prophecy\Call\Call;
19 * @author Konstantin Kudryashov <ever.zet@gmail.com>
24 * Stringifies any provided value.
27 * @param boolean $exportObject
31 public function stringify($value, $exportObject = true)
33 if (is_array($value)) {
34 if (range(0, count($value) - 1) === array_keys($value)) {
35 return '['.implode(', ', array_map(array($this, __FUNCTION__), $value)).']';
38 $stringify = array($this, __FUNCTION__);
40 return '['.implode(', ', array_map(function ($item, $key) use ($stringify) {
41 return (is_integer($key) ? $key : '"'.$key.'"').
42 ' => '.call_user_func($stringify, $item);
43 }, $value, array_keys($value))).']';
45 if (is_resource($value)) {
46 return get_resource_type($value).':'.$value;
48 if (is_object($value)) {
49 return $exportObject ? ExportUtil::export($value) : sprintf('%s:%s', get_class($value), spl_object_hash($value));
51 if (true === $value || false === $value) {
52 return $value ? 'true' : 'false';
54 if (is_string($value)) {
55 $str = sprintf('"%s"', str_replace("\n", '\\n', $value));
57 if (50 <= strlen($str)) {
58 return substr($str, 0, 50).'"...';
63 if (null === $value) {
67 return (string) $value;
71 * Stringifies provided array of calls.
73 * @param Call[] $calls Array of Call instances
77 public function stringifyCalls(array $calls)
81 return implode(PHP_EOL, array_map(function (Call $call) use ($self) {
82 return sprintf(' - %s(%s) @ %s',
83 $call->getMethodName(),
84 implode(', ', array_map(array($self, 'stringify'), $call->getArguments())),
85 str_replace(GETCWD().DIRECTORY_SEPARATOR, '', $call->getCallPlace())