1 | <?php |
||
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) |
||
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) |
||
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) |
||
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) |
||
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) |
||
116 | |||
117 | /** |
||
118 | * convert NULL into a string |
||
119 | * |
||
120 | * @return string |
||
121 | * always 'null' |
||
122 | */ |
||
123 | private static function fromNULL() |
||
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) |
||
140 | |||
141 | /** |
||
142 | * convert a resource into a string |
||
143 | * |
||
144 | * @return string |
||
145 | */ |
||
146 | private static function fromResource() |
||
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) |
||
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 | } |