1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright (c) 2015-present Ganbaro Digital Ltd |
5
|
|
|
* All rights reserved. |
6
|
|
|
* |
7
|
|
|
* Redistribution and use in source and binary forms, with or without |
8
|
|
|
* modification, are permitted provided that the following conditions |
9
|
|
|
* are met: |
10
|
|
|
* |
11
|
|
|
* * Redistributions of source code must retain the above copyright |
12
|
|
|
* notice, this list of conditions and the following disclaimer. |
13
|
|
|
* |
14
|
|
|
* * Redistributions in binary form must reproduce the above copyright |
15
|
|
|
* notice, this list of conditions and the following disclaimer in |
16
|
|
|
* the documentation and/or other materials provided with the |
17
|
|
|
* distribution. |
18
|
|
|
* |
19
|
|
|
* * Neither the names of the copyright holders nor the names of his |
20
|
|
|
* contributors may be used to endorse or promote products derived |
21
|
|
|
* from this software without specific prior written permission. |
22
|
|
|
* |
23
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
24
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
25
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
26
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
27
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
28
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
29
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
30
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
31
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
32
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
33
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
34
|
|
|
* POSSIBILITY OF SUCH DAMAGE. |
35
|
|
|
* |
36
|
|
|
* @category Libraries |
37
|
|
|
* @package Reflection/ValueBuilders |
38
|
|
|
* @author Stuart Herbert <[email protected]> |
39
|
|
|
* @copyright 2015-present Ganbaro Digital Ltd www.ganbarodigital.com |
40
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
41
|
|
|
* @link http://code.ganbarodigital.com/php-reflection |
42
|
|
|
*/ |
43
|
|
|
|
44
|
|
|
namespace GanbaroDigital\Reflection\ValueBuilders; |
45
|
|
|
|
46
|
|
|
class ConvertToString |
47
|
|
|
{ |
48
|
|
|
use LookupMethodByType; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* convert any input type into a real string |
52
|
|
|
* |
53
|
|
|
* @param mixed $item |
54
|
|
|
* the data to convert |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
|
|
public function __invoke($item) |
58
|
|
|
{ |
59
|
|
|
return self::from($item); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* convert any input type into a real string |
64
|
|
|
* |
65
|
|
|
* @param mixed $item |
66
|
|
|
* the data to convert |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
|
|
public static function from($item) |
70
|
|
|
{ |
71
|
|
|
$method = self::lookupMethodFor($item, self::$dispatchTable); |
72
|
|
|
return self::$method($item); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* convert an array into a string |
77
|
|
|
* |
78
|
|
|
* @param array $item |
79
|
|
|
* the data to convert |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
|
|
private static function fromArray($item) |
83
|
|
|
{ |
84
|
|
|
return implode(" ", $item); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* convert a boolean value into a string |
89
|
|
|
* |
90
|
|
|
* @param boolean $item |
91
|
|
|
* the data to convert |
92
|
|
|
* @return string |
93
|
|
|
* either 'true' or 'false' |
94
|
|
|
*/ |
95
|
|
|
private static function fromBoolean($item) |
96
|
|
|
{ |
97
|
|
|
if ($item) { |
98
|
|
|
return "true"; |
99
|
|
|
} |
100
|
|
|
return "false"; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* convert a callable into a string |
105
|
|
|
* |
106
|
|
|
* @param callable $item |
107
|
|
|
* the data to convert |
108
|
|
|
* @return string |
109
|
|
|
*/ |
110
|
|
|
private static function fromCallable($item) |
111
|
|
|
{ |
112
|
|
|
$callable_text = ''; |
113
|
|
|
is_callable($item, false, $callable_text); |
114
|
|
|
return "(callable {$callable_text}())"; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* convert NULL into a string |
119
|
|
|
* |
120
|
|
|
* @return string |
121
|
|
|
* always 'null' |
122
|
|
|
*/ |
123
|
|
|
private static function fromNULL() |
124
|
|
|
{ |
125
|
|
|
return "null"; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* convert an object into a string |
130
|
|
|
* |
131
|
|
|
* @param object $item |
132
|
|
|
* the data to convert |
133
|
|
|
* @return string |
134
|
|
|
*/ |
135
|
|
|
private static function fromObject($item) |
136
|
|
|
{ |
137
|
|
|
// general case |
138
|
|
|
return "(object of type " . get_class($item) . ")"; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* convert a resource into a string |
143
|
|
|
* |
144
|
|
|
* @return string |
145
|
|
|
*/ |
146
|
|
|
private static function fromResource() |
147
|
|
|
{ |
148
|
|
|
return "(resource)"; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* convert a string into a string |
153
|
|
|
* |
154
|
|
|
* not quite as daft as it sounds, as $item could be a data type that |
155
|
|
|
* can be coerced into being a string |
156
|
|
|
* |
157
|
|
|
* @param mixed $item |
158
|
|
|
* the data to convert |
159
|
|
|
* @return string |
160
|
|
|
*/ |
161
|
|
|
private static function fromString($item) |
162
|
|
|
{ |
163
|
|
|
return (string)$item; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* our list of which method to call for which data type |
168
|
|
|
* @var array |
169
|
|
|
*/ |
170
|
|
|
private static $dispatchTable = [ |
171
|
|
|
"Array" => "fromArray", |
172
|
|
|
"Boolean" => "fromBoolean", |
173
|
|
|
"Callable" => "fromCallable", |
174
|
|
|
"Double" => "fromString", |
175
|
|
|
"Integer" => "fromString", |
176
|
|
|
"NULL" => "fromNull", |
177
|
|
|
"Object" => "fromObject", |
178
|
|
|
"Resource" => "fromResource", |
179
|
|
|
"String" => "fromString", |
180
|
|
|
]; |
181
|
|
|
} |