Pull merge.
[yaffs-website] / vendor / phpspec / prophecy / src / Prophecy / Util / StringUtil.php
1 <?php
2
3 /*
4  * This file is part of the Prophecy.
5  * (c) Konstantin Kudryashov <ever.zet@gmail.com>
6  *     Marcello Duarte <marcello.duarte@gmail.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Prophecy\Util;
13
14 use Prophecy\Call\Call;
15
16 /**
17  * String utility.
18  *
19  * @author Konstantin Kudryashov <ever.zet@gmail.com>
20  */
21 class StringUtil
22 {
23     private $verbose;
24
25     /**
26      * @param bool $verbose
27      */
28     public function __construct($verbose = true)
29     {
30         $this->verbose = $verbose;
31     }
32
33     /**
34      * Stringifies any provided value.
35      *
36      * @param mixed   $value
37      * @param boolean $exportObject
38      *
39      * @return string
40      */
41     public function stringify($value, $exportObject = true)
42     {
43         if (is_array($value)) {
44             if (range(0, count($value) - 1) === array_keys($value)) {
45                 return '['.implode(', ', array_map(array($this, __FUNCTION__), $value)).']';
46             }
47
48             $stringify = array($this, __FUNCTION__);
49
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))).']';
54         }
55         if (is_resource($value)) {
56             return get_resource_type($value).':'.$value;
57         }
58         if (is_object($value)) {
59             return $exportObject ? ExportUtil::export($value) : sprintf('%s:%s', get_class($value), spl_object_hash($value));
60         }
61         if (true === $value || false === $value) {
62             return $value ? 'true' : 'false';
63         }
64         if (is_string($value)) {
65             $str = sprintf('"%s"', str_replace("\n", '\\n', $value));
66
67             if (!$this->verbose && 50 <= strlen($str)) {
68                 return substr($str, 0, 50).'"...';
69             }
70
71             return $str;
72         }
73         if (null === $value) {
74             return 'null';
75         }
76
77         return (string) $value;
78     }
79
80     /**
81      * Stringifies provided array of calls.
82      *
83      * @param Call[] $calls Array of Call instances
84      *
85      * @return string
86      */
87     public function stringifyCalls(array $calls)
88     {
89         $self = $this;
90
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())
96             );
97         }, $calls));
98     }
99 }