Skip to content

Commit 896cf8c

Browse files
committed
update some help methods
1 parent 6e54cd6 commit 896cf8c

File tree

5 files changed

+57
-17
lines changed

5 files changed

+57
-17
lines changed

src/Obj/AbstractMap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public function toJson(bool $filterEmpty = false): string
126126
/**
127127
* @return mixed
128128
*/
129-
public function jsonSerialize()
129+
public function jsonSerialize(): array
130130
{
131131
return $this->toArray();
132132
}

src/Obj/Traits/StdObjectTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ protected function init(): void
9191
*
9292
* @return mixed
9393
*/
94-
public function __call($method, array $args)
94+
public function __call(string $method, array $args)
9595
{
9696
// if (method_exists($this, $method) && $this->isAllowCall($method) ) {
9797
// return call_user_func_array( array($this, $method), (array) $args);

src/Str/Traits/StringCaseHelperTrait.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -178,24 +178,25 @@ public static function toCamel(string $str, bool $upperFirstChar = false): strin
178178
}
179179

180180
/**
181-
* to camel
181+
* string to camel case
182182
*
183183
* @param string $name
184-
* @param bool $upperFirst
184+
* @param bool $upFirst
185+
* @param string $sep eg: -_/
185186
*
186187
* @return string
187188
*/
188-
public static function camelCase(string $name, bool $upperFirst = false): string
189+
public static function camelCase(string $name, bool $upFirst = false, string $sep = '-'): string
189190
{
190-
$name = trim($name, '-_');
191+
$name = trim($name, " $sep");
191192

192193
// convert 'first-second' to 'firstSecond'
193-
if (strpos($name, '-')) {
194-
$name = ucwords(str_replace('-', ' ', $name));
195-
$name = str_replace(' ', '', lcfirst($name));
194+
if (strpos($name, $sep) > 0) {
195+
$name = ucwords(str_replace($sep, ' ', $name));
196+
$name = str_replace(' ', '', $name);
196197
}
197198

198-
return $upperFirst ? ucfirst($name) : $name;
199+
return $upFirst ? $name : lcfirst($name);
199200
}
200201

201202
/**

src/Str/UrlHelper.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,18 +72,22 @@ public static function isFullUrl(string $url): bool
7272
}
7373

7474
/**
75-
* @param string $url
76-
* @param mixed $data
75+
* @param string $baseUrl
76+
* @param array|object $data
7777
*
7878
* @return string
7979
*/
80-
public static function build($url, $data = null): string
80+
public static function build(string $baseUrl, $data = null): string
8181
{
8282
if ($data && ($param = http_build_query($data))) {
83-
$url .= (strpos($url, '?') ? '&' : '?') . $param;
83+
if ($baseUrl) {
84+
$baseUrl .= (strpos($baseUrl, '?') ? '&' : '?') . $param;
85+
} else {
86+
$baseUrl = $param;
87+
}
8488
}
8589

86-
return $url;
90+
return $baseUrl;
8791
}
8892

8993
/**

src/Type.php

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,19 @@
99

1010
namespace Toolkit\Stdlib;
1111

12+
use function gettype;
13+
1214
/**
1315
* Class Type - php data type
1416
*
1517
* @package Toolkit\PhpKit
18+
* @see \gettype()
1619
*/
1720
final class Type
1821
{
19-
// basic types
22+
// ------ basic types ------
23+
24+
// TIPS: not in gettype returns.
2025
public const INT = 'int';
2126

2227
public const INTEGER = 'integer';
@@ -25,21 +30,51 @@ final class Type
2530

2631
public const DOUBLE = 'double';
2732

33+
// TIPS: not in gettype returns.
2834
public const BOOL = 'bool';
2935

3036
public const BOOLEAN = 'boolean';
3137

3238
public const STRING = 'string';
3339

34-
// complex types
40+
// ------ complex types ------
3541
public const ARRAY = 'array';
3642

3743
public const OBJECT = 'object';
3844

3945
public const RESOURCE = 'resource';
4046

47+
// TIPS: since 7.2.0
48+
public const RESOURCE_CLOSED = 'resource (closed)';
49+
50+
// ------ other type names ------
51+
52+
public const CALLABLE = 'callable';
53+
54+
public const MiXED = 'mixed';
55+
56+
public const UNKNOWN = 'unknown type';
57+
58+
/**
59+
* @param mixed $val
60+
*
61+
* @return string
62+
*/
63+
public static function get($val): string
64+
{
65+
$typName = gettype($val);
66+
if ($typName === self::UNKNOWN) {
67+
$typName = self::MiXED;
68+
} elseif ($typName === self::RESOURCE_CLOSED) {
69+
$typName = self::RESOURCE;
70+
}
71+
72+
return $typName;
73+
}
74+
4175
/**
4276
* @return array
77+
* @see \gettype()
4378
*/
4479
public static function all(): array
4580
{

0 commit comments

Comments
 (0)