|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the ramsey/collection library |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
* |
|
8
|
|
|
* @copyright Copyright (c) Ben Ramsey <[email protected]> |
|
9
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
|
10
|
|
|
* @link https://benramsey.com/projects/ramsey-collection/ Documentation |
|
11
|
|
|
* @link https://packagist.org/packages/ramsey/collection Packagist |
|
12
|
|
|
* @link https://github.com/ramsey/collection GitHub |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Ramsey\Collection\Tool; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Provides functionality to express a value as string |
|
19
|
|
|
*/ |
|
20
|
|
|
trait ValueToStringTrait |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* Return a string with the information of the value |
|
24
|
|
|
* null value: NULL |
|
25
|
|
|
* boolean: TRUE, FALSE |
|
26
|
|
|
* array: Array |
|
27
|
|
|
* scalar: converted-value |
|
28
|
|
|
* resource: (type resource #number) |
|
29
|
|
|
* object with __toString(): result of __toString() |
|
30
|
|
|
* object DateTime: ISO 8601 date |
|
31
|
|
|
* object: (className Object) |
|
32
|
|
|
* anonymous function: same as object |
|
33
|
|
|
* |
|
34
|
|
|
* @param mixed $value |
|
35
|
|
|
* @return string |
|
36
|
|
|
*/ |
|
37
|
|
|
protected function toolValueToString($value) |
|
38
|
|
|
{ |
|
39
|
|
|
// null |
|
40
|
|
|
if (is_null($value)) { |
|
41
|
|
|
return 'NULL'; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
// boolean constants |
|
45
|
|
|
if (is_bool($value)) { |
|
46
|
|
|
return ($value) ? 'TRUE' : 'FALSE'; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
// array |
|
50
|
|
|
if (is_array($value)) { |
|
51
|
|
|
return 'Array'; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
// scalar types (integer, float, string) |
|
55
|
|
|
if (is_scalar($value)) { |
|
56
|
|
|
return (string) $value; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
// resource |
|
60
|
|
|
if (is_resource($value)) { |
|
61
|
|
|
return '(' . get_resource_type($value) . ' resource #' . (int) $value . ')'; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
// after this line $value is an object since is not null, scalar, array or resource |
|
65
|
|
|
|
|
66
|
|
|
// __toString() is implemented |
|
67
|
|
|
if (is_callable([$value, '__toString'])) { |
|
68
|
|
|
return (string) $value->__toString(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
// object of type \DateTime |
|
72
|
|
|
if ($value instanceof \DateTimeInterface) { |
|
73
|
|
|
return $value->format("c"); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
// unknown type |
|
77
|
|
|
return '(' . get_class($value) . ' Object)'; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|