Skip to content

Commit 0c58476

Browse files
committed
feat: add new method for load data to DataObject
1 parent 02b79c5 commit 0c58476

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

src/Obj/DataObject.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,30 @@ public static function new(array $data = []): self
3131
return new static($data);
3232
}
3333

34+
/**
35+
* @param array $data
36+
* @param bool $override
37+
*/
38+
public function load(array $data, bool $override = false): void
39+
{
40+
if ($override) {
41+
$this->override($data);
42+
return;
43+
}
44+
45+
foreach ($data as $key => $val) {
46+
$this->offsetSet($key, $val);
47+
}
48+
}
49+
50+
/**
51+
* @param array $data
52+
*/
53+
public function override(array $data): void
54+
{
55+
$this->exchangeArray($data);
56+
}
57+
3458
/**
3559
* @param string $key
3660
* @param mixed $default

src/Str/Traits/StringConvertTrait.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,9 @@ public static function toTyped(string $str, bool $parseBool = false, int $intMax
129129
*
130130
* @return array
131131
*/
132-
public static function toInts(string $string, string $delimiter = ',', int $limit = 0): array
132+
public static function str2ints(string $string, string $delimiter = ',', int $limit = 0): array
133133
{
134-
return self::str2ints($string, $delimiter, $limit);
134+
return self::toInts($string, $delimiter, $limit);
135135
}
136136

137137
/**
@@ -141,7 +141,7 @@ public static function toInts(string $string, string $delimiter = ',', int $limi
141141
*
142142
* @return array
143143
*/
144-
public static function str2ints(string $str, string $delimiter = ',', int $limit = 0): array
144+
public static function toInts(string $str, string $delimiter = ',', int $limit = 0): array
145145
{
146146
$intArr = [];
147147
// $values = self::splitTrimFiltered($str, $delimiter, $limit);
@@ -394,6 +394,16 @@ public static function splitByWidth(string $string, int $width = 1): array
394394
return $lines;
395395
}
396396

397+
/**
398+
* @param string $str
399+
*
400+
* @return array
401+
*/
402+
public static function splitUtf8(string $str): array
403+
{
404+
return preg_split('//u', $str, -1, PREG_SPLIT_NO_EMPTY);
405+
}
406+
397407
/**
398408
* @param string $str
399409
* @param int $length
@@ -425,7 +435,7 @@ public static function splitUnicode(string $str, int $length = 1): array
425435
*/
426436
public static function splitUnicode2(string $str, int $length = 1): array
427437
{
428-
$tmp = preg_split('~~u', $str, -1, PREG_SPLIT_NO_EMPTY);
438+
$tmp = preg_split('//u', $str, -1, PREG_SPLIT_NO_EMPTY);
429439

430440
if ($length > 1) {
431441
$chunks = array_chunk($tmp, $length);

0 commit comments

Comments
 (0)