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>
26 * @param bool $verbose
28 public function __construct($verbose = true)
30 $this->verbose = $verbose;
34 * Stringifies any provided value.
37 * @param boolean $exportObject
41 public function stringify($value, $exportObject = true)
43 if (is_array($value)) {
44 if (range(0, count($value) - 1) === array_keys($value)) {
45 return '['.implode(', ', array_map(array($this, __FUNCTION__), $value)).']';
48 $stringify = array($this, __FUNCTION__);
50 return '['.implode(', ', array_map(function ($item, $key) use ($stringify) {
51 return (is_integer($key) ? $key : '"'.$key.'"').
52 ' => '.call_user_func($stringify, $item);
53 }, $value, array_keys($value))).']';
55 if (is_resource($value)) {
56 return get_resource_type($value).':'.$value;
58 if (is_object($value)) {
59 return $exportObject ? ExportUtil::export($value) : sprintf('%s:%s', get_class($value), spl_object_hash($value));
61 if (true === $value || false === $value) {
62 return $value ? 'true' : 'false';
64 if (is_string($value)) {
65 $str = sprintf('"%s"', str_replace("\n", '\\n', $value));
67 if (!$this->verbose && 50 <= strlen($str)) {
68 return substr($str, 0, 50).'"...';
73 if (null === $value) {
77 return (string) $value;
81 * Stringifies provided array of calls.
83 * @param Call[] $calls Array of Call instances
87 public function stringifyCalls(array $calls)
91 return implode(PHP_EOL, array_map(function (Call $call) use ($self) {
92 return sprintf(' - %s(%s) @ %s',
93 $call->getMethodName(),
94 implode(', ', array_map(array($self, 'stringify'), $call->getArguments())),
95 str_replace(GETCWD().DIRECTORY_SEPARATOR, '', $call->getCallPlace())