Skip to content

Commit 3691333

Browse files
committed
some update
1 parent e00cd5e commit 3691333

File tree

5 files changed

+141
-60
lines changed

5 files changed

+141
-60
lines changed

libs/obj-utils/src/ObjectHelper.php

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,6 @@
1515
*/
1616
class ObjectHelper
1717
{
18-
/**
19-
* @param mixed $object An object instance
20-
* @param array $options
21-
* @return mixed
22-
*/
23-
public static function smartConfigure($object, array $options)
24-
{
25-
return self::init($object, $options);
26-
}
27-
2818
/**
2919
* 给对象设置属性值
3020
* - 会先尝试用 setter 方法设置属性
@@ -45,7 +35,7 @@ public static function init($object, array $options)
4535
// has setter
4636
if (\method_exists($object, $setter)) {
4737
$object->$setter($value);
48-
} else {
38+
} elseif (\property_exists($object, $property)) {
4939
$object->$property = $value;
5040
}
5141
}
@@ -61,7 +51,9 @@ public static function init($object, array $options)
6151
public static function configure($object, array $options)
6252
{
6353
foreach ($options as $property => $value) {
64-
$object->$property = $value;
54+
if (\property_exists($object, $property)) {
55+
$object->$property = $value;
56+
}
6557
}
6658
}
6759

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: Inhere
5+
* Date: 2018/5/6 0006
6+
* Time: 12:45
7+
*/
8+
9+
class Some {
10+
public $prop0;
11+
private $prop1;
12+
protected $prop2;
13+
14+
/**
15+
* @return mixed
16+
*/
17+
public function getProp1()
18+
{
19+
return $this->prop1;
20+
}
21+
}
22+
23+
24+
echo "use class:\n";
25+
26+
echo 'public: ' . (property_exists(Some::class, 'prop0') ? 'Y':'N') . PHP_EOL;
27+
echo 'private: ' . (property_exists(Some::class, 'prop1') ? 'Y':'N') . PHP_EOL;
28+
echo 'protected: ' . (property_exists(Some::class, 'prop2') ? 'Y':'N') . PHP_EOL;
29+
30+
echo "use object:\n";
31+
32+
$object = new Some();
33+
34+
echo 'public: ' . (property_exists($object, 'prop0') ? 'Y':'N') . PHP_EOL;
35+
echo 'private: ' . (property_exists($object, 'prop1') ? 'Y':'N') . PHP_EOL;
36+
echo 'protected: ' . (property_exists($object, 'prop2') ? 'Y':'N') . PHP_EOL;

libs/php-utils/src/PhpHelper.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,34 @@ public static function callByArray(callable $cb, array $args)
6363
return self::call($cb, ...$args);
6464
}
6565

66+
/**
67+
* 给对象设置属性值
68+
* - 会先尝试用 setter 方法设置属性
69+
* - 再尝试直接设置属性
70+
* @param mixed $object An object instance
71+
* @param array $options
72+
* @return mixed
73+
*/
74+
public static function initObject($object, array $options)
75+
{
76+
foreach ($options as $property => $value) {
77+
if (\is_numeric($property)) {
78+
continue;
79+
}
80+
81+
$setter = 'set' . \ucfirst($property);
82+
83+
// has setter
84+
if (\method_exists($object, $setter)) {
85+
$object->$setter($value);
86+
} elseif (\property_exists($object, $property)) {
87+
$object->$property = $value;
88+
}
89+
}
90+
91+
return $object;
92+
}
93+
6694
/**
6795
* 获取资源消耗
6896
* @param int $startTime

libs/str-utils/src/StringHelper.php

Lines changed: 47 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,11 @@ public static function zhSubStr($str, $start = 0, $length, $charset = 'utf-8', $
189189
* @param array|string $param -
190190
* @internal param string $chars
191191
* @return string
192+
* @throws \Exception
192193
*/
193194
public static function random($length, array $param = []): string
194195
{
195-
$param = array_merge([
196+
$param = \array_merge([
196197
'prefix' => '',
197198
'suffix' => '',
198199
'chars' => 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
@@ -211,24 +212,28 @@ public static function random($length, array $param = []): string
211212

212213
/**
213214
* @param int $length
214-
* @return bool|string
215+
* @return string
215216
*/
216-
public static function genSalt($length = 32)
217+
public static function genSalt(int $length = 32): string
217218
{
218-
return substr(str_replace('+', '.', base64_encode(hex2bin(random_token($length)))), 0, 44);
219+
return \substr(
220+
\str_replace('+', '.', \base64_encode(\hex2bin(\random_token($length)))),
221+
0,
222+
44
223+
);
219224
}
220225

221226
/**
222227
* @param int $length
223228
* @return bool|string
224229
*/
225-
public static function genUid($length = 7)
230+
public static function genUid(int $length = 7): string
226231
{
227232
if (!\is_int($length) || $length > 32 || $length < 1) {
228233
$length = 7;
229234
}
230235

231-
return substr(hash('md5', uniqid('', true)), 0, $length);
236+
return \substr(\hash('md5', \uniqid('', true)), 0, $length);
232237
}
233238

234239
/**
@@ -268,7 +273,7 @@ public static function strtolower($str)
268273
return $str;
269274
}
270275

271-
return \function_exists('mb_strtolower') ? mb_strtolower($str, 'utf-8') : strtolower($str);
276+
return \function_exists('mb_strtolower') ? \mb_strtolower($str, 'utf-8') : \strtolower($str);
272277
}
273278

274279
/**
@@ -281,7 +286,7 @@ public static function strtoupper($str)
281286
return $str;
282287
}
283288

284-
return \function_exists('mb_strtoupper') ? mb_strtoupper($str, 'utf-8') : strtoupper($str);
289+
return \function_exists('mb_strtoupper') ? \mb_strtoupper($str, 'utf-8') : \strtoupper($str);
285290
}
286291

287292
/**
@@ -291,13 +296,13 @@ public static function strtoupper($str)
291296
* @param string $encoding
292297
* @return bool|string
293298
*/
294-
public static function substr($str, $start, $length = false, $encoding = 'utf-8')
299+
public static function substr(string $str, int $start, int $length = null, string $encoding = 'utf-8')
295300
{
296301
if (\function_exists('mb_substr')) {
297-
return mb_substr($str, (int)$start, ($length === false ? self::strlen($str) : (int)$length), $encoding);
302+
return mb_substr($str, $start, ($length === null ? self::strlen($str) : (int)$length), $encoding);
298303
}
299304

300-
return substr($str, $start, ($length === false ? self::strlen($str) : (int)$length));
305+
return substr($str, $start, ($length === null ? self::strlen($str) : (int)$length));
301306
}
302307

303308
/**
@@ -307,23 +312,25 @@ public static function substr($str, $start, $length = false, $encoding = 'utf-8'
307312
* @param string $encoding
308313
* @return bool|int
309314
*/
310-
public static function strpos($str, $find, $offset = 0, $encoding = 'UTF-8')
315+
public static function strpos(string $str, string $find, int $offset = 0, string $encoding = 'UTF-8')
311316
{
312-
return \function_exists('mb_strpos') ? mb_strpos($str, $find, $offset, $encoding) : strpos($str, $find,
313-
$offset);
317+
return \function_exists('mb_strpos') ?
318+
mb_strpos($str, $find, $offset, $encoding) :
319+
strpos($str, $find, $offset);
314320
}
315321

316322
/**
317-
* @param $str
318-
* @param $find
323+
* @param string $str
324+
* @param string $find
319325
* @param int $offset
320326
* @param string $encoding
321327
* @return bool|int
322328
*/
323-
public static function strrpos($str, $find, $offset = 0, $encoding = 'utf-8')
329+
public static function strrpos(string $str, string $find, int $offset = 0, string $encoding = 'utf-8')
324330
{
325-
return \function_exists('mb_strrpos') ? mb_strrpos($str, $find, $offset, $encoding) : strrpos($str, $find,
326-
$offset);
331+
return \function_exists('mb_strrpos') ?
332+
mb_strrpos($str, $find, $offset, $encoding) :
333+
strrpos($str, $find, $offset);
327334
}
328335

329336
/**
@@ -350,7 +357,7 @@ public static function ucwords($str): string
350357
* @param string $sep
351358
* @return array
352359
*/
353-
public static function toArray($str, $sep = ','): array
360+
public static function toArray(string $str, string $sep = ','): array
354361
{
355362
$array = [];
356363

@@ -373,23 +380,23 @@ public static function toArray($str, $sep = ','): array
373380
* @param string $sep
374381
* @return array
375382
*/
376-
public static function str2array($str, $sep = ','): array
383+
public static function str2array(string $str, string $sep = ','): array
377384
{
378385
$str = trim($str, "$sep ");
379386

380387
if (!$str) {
381388
return [];
382389
}
383390

384-
return preg_split("/\s*$sep\s*/", $str, -1, PREG_SPLIT_NO_EMPTY);
391+
return \preg_split("/\s*$sep\s*/", $str, -1, PREG_SPLIT_NO_EMPTY);
385392
}
386393

387394
/**
388395
* @param string $path
389396
* @param string $separator
390397
* @return array
391398
*/
392-
public static function split2Array($path, $separator = '.'): array
399+
public static function split2Array(string $path, string $separator = '.'): array
393400
{
394401
return array_values(array_filter(explode($separator, $path), '\strlen'));
395402
}
@@ -421,7 +428,7 @@ public static function truncate($str, $max_length, $suffix = '...'): string
421428
* @param null|int $length
422429
* @return string
423430
*/
424-
public static function truncate_two($str, $start, $length = null): string
431+
public static function truncate2(string $str, int $start, int $length = null): string
425432
{
426433
if (!$length) {
427434
$length = $start;
@@ -448,7 +455,7 @@ public static function truncate_two($str, $start, $length = null): string
448455
* @param array $options
449456
* @return bool|string
450457
*/
451-
public static function truncateString($text, $length = 120, array $options = array())
458+
public static function truncate3(string $text, int $length = 120, array $options = array())
452459
{
453460
$default = [
454461
'ellipsis' => '...',
@@ -575,7 +582,12 @@ public static function truncateString($text, $length = 120, array $options = arr
575582
return $truncate;
576583
}
577584

578-
public static function toCamel($str, $upperFirstChar = false)
585+
/**
586+
* @param $str
587+
* @param bool $upperFirstChar
588+
* @return mixed
589+
*/
590+
public static function toCamel(string $str, bool $upperFirstChar = false): string
579591
{
580592
return self::toCamelCase($str, $upperFirstChar);
581593
}
@@ -587,9 +599,9 @@ public static function toCamel($str, $upperFirstChar = false)
587599
* @param bool $upperFirstChar
588600
* @return mixed
589601
*/
590-
public static function toCamelCase($str, $upperFirstChar = false)
602+
public static function toCamelCase(string $str, bool $upperFirstChar = false): string
591603
{
592-
$str = self::strtolower($str);
604+
$str = (string)self::strtolower($str);
593605

594606
if ($upperFirstChar) {
595607
$str = self::ucfirst($str);
@@ -600,7 +612,7 @@ public static function toCamelCase($str, $upperFirstChar = false)
600612
}, $str);
601613
}
602614

603-
public static function toSnake($str, $sep = '_'): string
615+
public static function toSnake(string $str, string $sep = '_'): string
604616
{
605617
return self::toSnakeCase($str, $sep);
606618
}
@@ -611,7 +623,7 @@ public static function toSnake($str, $sep = '_'): string
611623
* @param string $sep
612624
* @return string
613625
*/
614-
public static function toSnakeCase($str, $sep = '_'): string
626+
public static function toSnakeCase(string $str, string $sep = '_'): string
615627
{
616628
// 'CMSCategories' => 'cms_categories'
617629
// 'RangePrice' => 'range_price'
@@ -620,18 +632,18 @@ public static function toSnakeCase($str, $sep = '_'): string
620632

621633
/**
622634
* 驼峰式 <=> 下划线式
623-
* @param [type] $str [description]
635+
* @param string $str [description]
624636
* @param bool $toCamelCase
625637
* true : 驼峰式 => 下划线式
626638
* false : 驼峰式 <= 下划线式
627-
* @return mixed|string
639+
* @return string
628640
*/
629-
public static function nameChange($str, $toCamelCase = true)
641+
public static function nameChange(string $str, bool $toCamelCase = true): string
630642
{
631643
$str = trim($str);
632644

633645
// 默认 :下划线式 =>驼峰式
634-
if ((bool)$toCamelCase) {
646+
if ($toCamelCase) {
635647
if (strpos($str, '_') === false) {
636648
return $str;
637649
}
@@ -647,7 +659,7 @@ public static function nameChange($str, $toCamelCase = true)
647659
}
648660

649661
// 驼峰式 => 下划线式
650-
return strtolower(preg_replace('/((?<=[a-z])(?=[A-Z]))/', '_', $str));
662+
return \strtolower(preg_replace('/((?<=[a-z])(?=[A-Z]))/', '_', $str));
651663
}
652664

653665
/**

0 commit comments

Comments
 (0)