- First working version of Tell

- JsonReader and PhpReader added
This commit is contained in:
mcndave committed 2020-05-09 22:55:07 -04:00
1 parent a58efcdef4
commit 026126be76
7 files changed
+277 -88

No files matched your search

+34 -20
View File
@@ -3,9 +3,7 @@
namespace Tell;
use RecursiveDirectoryIterator,
RecursiveIteratorIterator,
RecursiveRegexIterator,
RegexIterator;
RecursiveIteratorIterator;
class Iterate
{
@@ -15,7 +13,7 @@ class Iterate
* @param string $path - path like 'person.name.first'
* @param string $value - value to set
*/
public static function arraySet(array &$array, string $path, string $value = '', string $delimiter = '.') {
public static function arraySet(?array &$array, string $path, $value, string $delimiter = '.') {
$pathArr = explode($delimiter, $path);
// Go to next node
@@ -28,6 +26,29 @@ class Iterate
}
}
public static function arrayKeysSet($key_list, &$array, $value, $append = false) {
$current_key = array_shift($key_list);
if ($key_list) {
return static::arrayKeysSet($key_list, $array[$current_key], $value, $append);
}
else {
if ( $append && isset($array[$current_key]) ) {
if (is_array($array[$current_key])) {
return $array[$current_key] = array_merge_recursive($value, $array[$current_key]); #: array_replace_recursive($value, $array[$current_key]);
}
else {
return $array[$current_key] = $array[$current_key] . $value;
}
}
else {
return $array[$current_key] = $value;
}
}
return false;
}
public static function arrayGet(array $array, string $path, string $delimiter = '.') {
$pathArr = explode($delimiter, $path);
@@ -65,8 +86,7 @@ class Iterate
$tmp = [];
$keylist = explode($delimiter, $key);
$finalKey = array_shift($keylist);
# Arrayobj::iterate_set($keylist, $tmp, $value);
static::arraySet($tmp, $key, $value);
static::arrayKeysSet($keylist, $tmp, $value);
$swap[$finalKey] = static::splitKeys($tmp, $delimiter);
unset($array[isset($oldKey) ? $oldKey : $key]);
@@ -90,28 +110,22 @@ class Iterate
return $array;
}
public static function files(string $path, string $fileExtension = "") {
$retval = [];
public static function files(string $path, string $fileExtension = "") : \Generator
{
if ( \file_exists($path) ) {
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::SELF_FIRST, RecursiveIteratorIterator::CATCH_GET_CHILD);
if ($fileExtension) {
$iterator = new RegexIterator($iterator, '/^.+\.'.$fileExtension.'$/i', RecursiveRegexIterator::GET_MATCH);
}
foreach ($iterator as $file) {
if ($fileExtension) {
$retval[] = $file[0];
}
else {
if ( $file->isFile() || $file->isDir() ) {
$retval[] = $file->getRealPath();
if ( $file->isFile() || $file->isDir() ) {
if ($fileExtension && ( $file->getExtension() === $fileExtension )) {
yield $file;
}
else {
yield $file;
}
}
}
}
return $retval;
}
}