|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* PHP: Nelson Martell Library file |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright © 2016-2021 Nelson Martell (http://nelson6e65.github.io) |
|
7
|
|
|
* |
|
8
|
|
|
* Licensed under The MIT License (MIT) |
|
9
|
|
|
* For full copyright and license information, please see the LICENSE |
|
10
|
|
|
* Redistributions of files must retain the above copyright notice. |
|
11
|
|
|
* |
|
12
|
|
|
* @copyright 2016-2021 Nelson Martell |
|
13
|
|
|
* @link http://nelson6e65.github.io/php_nml/ |
|
14
|
|
|
* @since 0.6.0 |
|
15
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License (MIT) |
|
16
|
|
|
* */ |
|
17
|
|
|
|
|
18
|
|
|
declare(strict_types=1); |
|
19
|
|
|
|
|
20
|
|
|
namespace NelsonMartell\Test\Helpers; |
|
21
|
|
|
|
|
22
|
|
|
use InvalidArgumentException; |
|
23
|
|
|
use NelsonMartell\Extensions\Text; |
|
24
|
|
|
use SebastianBergmann\Exporter\Exporter; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Plugin to export variables in test clases. |
|
28
|
|
|
* |
|
29
|
|
|
* @author Nelson Martell <[email protected]> |
|
30
|
|
|
* @since 0.6.0 |
|
31
|
|
|
* @internal |
|
32
|
|
|
* */ |
|
33
|
|
|
trait ExporterPlugin |
|
34
|
|
|
{ |
|
35
|
|
|
/** |
|
36
|
|
|
* @var Exporter |
|
37
|
|
|
*/ |
|
38
|
|
|
protected static $exporter = null; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Extract the string representation of specified object in one line. |
|
42
|
|
|
* |
|
43
|
|
|
* @param mixed $obj Object to export. |
|
44
|
|
|
* @param int $depth How many levels of depth will export in arrays and objects. If $depth <= 0, |
|
45
|
|
|
* only '...' will be returned for content of object or array. Default: ``2``. |
|
46
|
|
|
* @param bool $short If class names are returned without namespace. Default: ``false``. |
|
47
|
|
|
* |
|
48
|
|
|
* @return string |
|
49
|
|
|
* |
|
50
|
|
|
* @see Exporter::shortenedRecursiveExport() |
|
51
|
|
|
* @see Exporter::export() |
|
52
|
|
|
*/ |
|
53
|
|
|
public static function export($obj, int $depth = 2, bool $short = false): string |
|
54
|
|
|
{ |
|
55
|
|
|
if (static::$exporter === null) { |
|
56
|
|
|
static::$exporter = new Exporter(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$depth = ((int) $depth < 0) ? 0 : (int) $depth; |
|
60
|
|
|
$short = (bool) $short; |
|
61
|
|
|
$str = null; |
|
62
|
|
|
|
|
63
|
|
|
if (is_object($obj)) { |
|
64
|
|
|
$className = static::getClass($obj, $short); |
|
65
|
|
|
|
|
66
|
|
|
if ($depth <= 0) { |
|
67
|
|
|
// Do not show properties |
|
68
|
|
|
$content = '{...}'; |
|
69
|
|
|
} else { |
|
70
|
|
|
$arrayObject = static::$exporter->toArray($obj); |
|
71
|
|
|
// Initial content without deep |
|
72
|
|
|
$content = static::export($arrayObject, 1, $short); |
|
73
|
|
|
// Replace array braces and separator |
|
74
|
|
|
$content = str_replace([' => ', '[ ', ' ]'], [': ', '{ ', ' }'], $content); |
|
75
|
|
|
|
|
76
|
|
|
if ($depth > 1) { |
|
77
|
|
|
foreach ($arrayObject as $key => $value) { |
|
78
|
|
|
$format = '{0}: {1}'; |
|
79
|
|
|
|
|
80
|
|
|
if (is_object($value)) { |
|
81
|
|
|
$sclass = static::getClass($value, $short); |
|
82
|
|
|
|
|
83
|
|
|
$sVal = $sclass . '({...})'; |
|
84
|
|
|
} elseif (is_array($value)) { |
|
85
|
|
|
$sVal = '[...]'; |
|
86
|
|
|
} else { |
|
87
|
|
|
continue; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
$search = Text::format($format, $key, $sVal); |
|
91
|
|
|
$replacement = Text::format($format, $key, static::export($value, $depth - 1, $short)); |
|
92
|
|
|
|
|
93
|
|
|
$content = str_replace($search, $replacement, $content); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
$str = Text::format('{0}({1})', $className, $content); |
|
99
|
|
|
$str = str_replace(', }', ' }', $str); |
|
100
|
|
|
} elseif (is_array($obj)) { |
|
101
|
|
|
if ($depth <= 0) { |
|
102
|
|
|
$str = count($obj) > 0 ? '[...]' : ''; |
|
103
|
|
|
} else { |
|
104
|
|
|
$str = ''; |
|
105
|
|
|
foreach ($obj as $key => $value) { |
|
106
|
|
|
// Export all items recursively |
|
107
|
|
|
$str .= Text::format('{0} => {1}, ', $key, static::export($value, $depth - 1, $short)); |
|
108
|
|
|
} |
|
109
|
|
|
$str = Text::format('[ {0} ]', $str); |
|
110
|
|
|
$str = str_replace(', ]', ' ]', $str); |
|
111
|
|
|
} |
|
112
|
|
|
} else { |
|
113
|
|
|
$str = static::$exporter->export($obj); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
return $str; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Exports class name of an object instance. |
|
122
|
|
|
* |
|
123
|
|
|
* @param object $obj Object to get the class name. |
|
124
|
|
|
* @param bool $short Return only class name without namespace. |
|
125
|
|
|
* |
|
126
|
|
|
* @return string |
|
127
|
|
|
*/ |
|
128
|
|
|
public static function getClass($obj, bool $short = false): string |
|
129
|
|
|
{ |
|
130
|
|
|
if (!is_object($obj)) { |
|
131
|
|
|
throw new InvalidArgumentException('Object argument is not an object instance.'); |
|
132
|
|
|
} |
|
133
|
|
|
$className = get_class($obj); |
|
134
|
|
|
if ($short) { |
|
135
|
|
|
$pos = strrpos($className, '\\'); |
|
136
|
|
|
if ($pos !== false) { |
|
137
|
|
|
// Get class name without namespace |
|
138
|
|
|
$className = substr($className, $pos + 1); |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
return $className; |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|