Skip to content

Commit 691a5fd

Browse files
committed
feat: add an base collection utl class
1 parent 0c58476 commit 691a5fd

File tree

3 files changed

+500
-6
lines changed

3 files changed

+500
-6
lines changed

src/Arr/Traits/ArrayValueGetSetTrait.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use ArrayAccess;
1313
use Toolkit\Stdlib\Php;
14+
use Traversable;
1415
use function array_filter;
1516
use function array_shift;
1617
use function count;
@@ -168,8 +169,8 @@ public static function gets(array &$data, array $needKeys = [], bool $unsetKey =
168169
* Get data from array or object by path.
169170
* Example: `DataCollector::getByPath($array, 'foo.bar.yoo')` equals to $array['foo']['bar']['yoo'].
170171
*
171-
* @param array|ArrayAccess $data An array or object to get value.
172-
* @param mixed $path The key path.
172+
* @param array|Traversable $data An array or object to get value.
173+
* @param string $path The key path.
173174
* @param mixed $default
174175
* @param string $separator Separator of paths.
175176
*
@@ -188,12 +189,11 @@ public static function getByPath($data, string $path, $default = null, string $s
188189
}
189190

190191
$dataTmp = $data;
191-
192192
foreach ($nodes as $arg) {
193-
if (is_object($dataTmp) && isset($dataTmp->$arg)) {
194-
$dataTmp = $dataTmp->$arg;
195-
} elseif ((is_array($dataTmp) || $dataTmp instanceof ArrayAccess) && isset($dataTmp[$arg])) {
193+
if ((is_array($dataTmp) || $dataTmp instanceof ArrayAccess) && isset($dataTmp[$arg])) {
196194
$dataTmp = $dataTmp[$arg];
195+
} elseif (is_object($dataTmp) && isset($dataTmp->$arg)) {
196+
$dataTmp = $dataTmp->$arg;
197197
} else {
198198
return $default;
199199
}

src/OS.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,20 @@
99

1010
namespace Toolkit\Stdlib;
1111

12+
use InvalidArgumentException;
1213
use RuntimeException;
1314
use function defined;
15+
use function dirname;
1416
use function explode;
17+
use function file_get_contents;
18+
use function file_put_contents;
1519
use function function_exists;
1620
use function getcwd;
1721
use function getenv;
1822
use function getmyuid;
1923
use function in_array;
2024
use function is_dir;
25+
use function is_file;
2126
use function is_writable;
2227
use function mkdir;
2328
use function php_uname;
@@ -373,6 +378,36 @@ public static function isInteractive($fileDescriptor): bool
373378
return function_exists('posix_isatty') && @posix_isatty($fileDescriptor);
374379
}
375380

381+
/**
382+
* @param string $filepath
383+
*
384+
* @return string
385+
*/
386+
public static function readFile(string $filepath): string
387+
{
388+
if (!is_file($filepath)) {
389+
throw new InvalidArgumentException('no such file: ' . $filepath);
390+
}
391+
392+
return file_get_contents($filepath);
393+
}
394+
395+
/**
396+
* @param string $filepath
397+
* @param string $contents
398+
* @param int $flags
399+
*
400+
* @return int
401+
*/
402+
public static function writeFile(string $filepath, string $contents, int $flags = 0): int
403+
{
404+
// if (!is_dir($dir = dirname($filepath))) {
405+
// self::mkdir($dir);
406+
// }
407+
408+
return (int)file_put_contents($filepath, $contents, $flags);
409+
}
410+
376411
/**
377412
* Support the creation of hierarchical directories
378413
*

0 commit comments

Comments
 (0)