1
|
|
|
<?php namespace Tarsana\Functional; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Generic common functions. |
5
|
|
|
* @file |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Gets the type of the given argument. |
10
|
|
|
* |
11
|
|
|
* ```php |
12
|
|
|
* F\type(null); //=> 'Null' |
13
|
|
|
* F\type(true); //=> 'Boolean' |
14
|
|
|
* F\type(false); //=> 'Boolean' |
15
|
|
|
* F\type('Hello World'); //=> 'String' |
16
|
|
|
* F\type(1234); //=> 'Number' |
17
|
|
|
* F\type('123'); //=> 'String' |
18
|
|
|
* F\type(function($x) {return $x;}); //=> 'Function' |
19
|
|
|
* F\type(new \stdClass); //=> 'Object' |
20
|
|
|
* F\type(['name' => 'Foo', 'age' => 21]); //=> 'Array' |
21
|
|
|
* F\type(['Hello', 'World', 123, true]); //=> 'List' |
22
|
|
|
* F\type(['name' => 'Foo', 'Hello', 'Mixed']); //=> 'Array' |
23
|
|
|
* F\type(fopen('php://temp', 'w')); //=> 'Resource' |
24
|
|
|
* F\type(F\Error::of('Ooops !')); //=> 'Error' |
25
|
|
|
* F\type(F\Stream::of('Hello')); //=> 'Stream' |
26
|
|
|
* // Anything else is 'Unknown' |
27
|
|
|
* ``` |
28
|
|
|
* |
29
|
|
|
* @signature * -> String |
30
|
|
|
* @param mixed $data |
|
|
|
|
31
|
|
|
* @return string |
32
|
|
|
*/ |
33
|
|
|
function type() { |
34
|
|
|
static $type = false; |
35
|
|
|
$type = $type ?: _curry_one(function($data) { |
36
|
|
|
if ($data instanceof Error) return 'Error'; |
37
|
|
|
if ($data instanceof Stream) return 'Stream'; |
38
|
|
|
if (is_callable($data)) return 'Function'; |
39
|
|
|
switch (gettype($data)) { |
40
|
|
|
case 'boolean': |
41
|
|
|
return 'Boolean'; |
42
|
|
|
case 'NULL': |
43
|
|
|
return 'Null'; |
44
|
|
|
case 'integer': |
45
|
|
|
case 'double': |
46
|
|
|
return 'Number'; |
47
|
|
|
case 'string': |
48
|
|
|
return 'String'; |
49
|
|
|
case 'resource': |
50
|
|
|
return 'Resource'; |
51
|
|
|
case 'array': |
52
|
|
|
if (allSatisfies('is_numeric', keys($data))) |
53
|
|
|
return 'List'; |
54
|
|
|
return 'Array'; |
55
|
|
|
case 'object': |
56
|
|
|
return 'Object'; |
57
|
|
|
default: |
58
|
|
|
return 'Unknown'; |
59
|
|
|
} |
60
|
|
|
}); |
61
|
|
|
return _apply($type, func_get_args()); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Checks if a variable has a specific type. |
66
|
|
|
* |
67
|
|
|
* ```php |
68
|
|
|
* $isNumber = F\is('Number'); |
69
|
|
|
* $isNumber(5); //=> true |
70
|
|
|
* $isNumber('5'); //=> false |
71
|
|
|
* F\is('Any', '5'); //=> true |
72
|
|
|
* F\is('Any', [1, 2, 3]); //=> true |
73
|
|
|
* ``` |
74
|
|
|
* |
75
|
|
|
* @stream |
76
|
|
|
* @signature String -> * -> Boolean |
77
|
|
|
* @param string $type |
|
|
|
|
78
|
|
|
* @param mixed $data |
|
|
|
|
79
|
|
|
* @return boolean |
80
|
|
|
*/ |
81
|
|
|
function is() { |
82
|
|
|
static $is = false; |
83
|
|
|
$is = $is ?: curry(function($type, $data) { |
84
|
|
|
return 'Any' == $type || type($data) == $type; |
85
|
|
|
}); |
86
|
|
|
return _apply($is, func_get_args()); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Converts a variable to its string value. |
91
|
|
|
* |
92
|
|
|
* ```php |
93
|
|
|
* F\toString(53); //=> '53' |
94
|
|
|
* F\toString(true); //=> 'true' |
95
|
|
|
* F\toString(false); //=> 'false' |
96
|
|
|
* F\toString(null); //=> 'null' |
97
|
|
|
* F\toString('Hello World'); //=> '"Hello World"' |
98
|
|
|
* F\toString([]); //=> '[]' |
99
|
|
|
* F\toString(new \stdClass); //=> '{}' |
100
|
|
|
* F\toString(function(){}); //=> '[Function]' |
101
|
|
|
* F\toString(F\Error::of('Ooops')); //=> '[Error: Ooops]' |
102
|
|
|
* F\toString(F\Stream::of('Hello')); //=> '[Stream of String]' |
103
|
|
|
* F\toString(fopen('php://temp', 'r')); //=> '[Resource]' |
104
|
|
|
* F\toString(['hi', 'hello', 'yo']); //=> '["hi", "hello", "yo"]' |
105
|
|
|
* F\toString([ |
106
|
|
|
* 'object' => null, |
107
|
|
|
* 'numbers' => [1, 2, 3], |
108
|
|
|
* 'message' |
109
|
|
|
* ]); //=> '{object: null, numbers: [1, 2, 3], 0: "message"}' |
110
|
|
|
* ``` |
111
|
|
|
* |
112
|
|
|
* @stream |
113
|
|
|
* @signature * -> String |
114
|
|
|
* @param mixed $something |
|
|
|
|
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
|
|
function toString () { |
118
|
|
|
static $toString = false; |
119
|
|
|
$toString = $toString ?: curry(function($something) { |
120
|
|
|
switch (type($something)) { |
121
|
|
|
case 'String': |
122
|
|
|
return "\"{$something}\""; |
123
|
|
|
break; |
|
|
|
|
124
|
|
|
case 'Boolean': |
125
|
|
|
return $something ? 'true' : 'false'; |
126
|
|
|
break; |
|
|
|
|
127
|
|
|
case 'Null': |
128
|
|
|
return 'null'; |
129
|
|
|
break; |
|
|
|
|
130
|
|
|
case 'Number': |
131
|
|
|
return (string) $something; |
132
|
|
|
break; |
|
|
|
|
133
|
|
|
case 'List': |
134
|
|
|
return '[' . join(', ', map(toString(), $something)) . ']'; |
135
|
|
|
case 'Error': |
136
|
|
|
case 'Stream': |
137
|
|
|
return $something->__toString(); |
138
|
|
|
case 'Object': |
139
|
|
|
case 'Array': |
140
|
|
|
return '{' . join(', ', map(function($pair){ |
141
|
|
|
return $pair[0].': '. toString($pair[1]); |
142
|
|
|
}, toPairs($something))) . '}'; |
143
|
|
|
default: |
144
|
|
|
return '['.type($something).']'; |
145
|
|
|
} |
146
|
|
|
}); |
147
|
|
|
return _apply($toString, func_get_args()); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Creates a `Stream` containing the provided data. |
152
|
|
|
* |
153
|
|
|
* ```php |
154
|
|
|
* $s = F\s('! World Hello') |
155
|
|
|
* ->split(' ') |
156
|
|
|
* ->reverse() |
157
|
|
|
* ->join(' '); |
158
|
|
|
* $s->result(); //=> 'Hello World !' |
159
|
|
|
* ``` |
160
|
|
|
* |
161
|
|
|
* @signature a -> Stream(a) |
162
|
|
|
* @param mixed $data |
163
|
|
|
* @return Stream |
164
|
|
|
*/ |
165
|
|
|
function s($data) { |
166
|
|
|
return Stream::of($data); |
167
|
|
|
} |
168
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.