159 lines
5.3 KiB
PHP
159 lines
5.3 KiB
PHP
<?php
|
|
|
|
namespace Tell;
|
|
|
|
use RecursiveDirectoryIterator,
|
|
RecursiveIteratorIterator;
|
|
|
|
class Iterate
|
|
{
|
|
/**
|
|
* Set value to array with a given path using recursion
|
|
* @param array $array - array to set passed by reference !
|
|
* @param string $path - path like 'person.name.first'
|
|
* @param string $value - value to set
|
|
*/
|
|
public static function arraySet(?array &$array, string $path, $value, string $delimiter = '.') {
|
|
$pathArr = explode($delimiter, $path);
|
|
|
|
// Go to next node
|
|
if ( isset($pathArr[1])) {
|
|
static::arraySet($array[ array_shift($pathArr) ], implode($delimiter, $pathArr), $value);
|
|
}
|
|
// We are at the end of the path, set value
|
|
else {
|
|
$array[ $pathArr[0] ] = $value;
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
# Parses $obj1.obj2.obj3.propery
|
|
protected static function objectFromPath(object $object, array $pathArr) : array
|
|
{
|
|
while(count($pathArr) > 1) {
|
|
$propertyName = array_shift($pathArr);
|
|
$object = $object->$propertyName;
|
|
}
|
|
|
|
return [ $object, end($pathArr) ];
|
|
}
|
|
|
|
public static function arrayGet(iterable $array, string $path, string $delimiter = '.') {
|
|
$pathArr = explode($delimiter, $path);
|
|
|
|
if (isset($array[$pathArr[0]])) {
|
|
if ( isset($pathArr[1]) ) {
|
|
$recurse = $array[array_shift($pathArr)];
|
|
|
|
if ( ! is_iterable($recurse) ) {
|
|
if ( is_object($recurse) ) {
|
|
list($object, $method) = static::objectFromPath($recurse, $pathArr);
|
|
|
|
# @TODO An object caller would be a WAY better idea then this line of code
|
|
# Calling $obj->varname
|
|
|
|
return $object->$method ?? "";
|
|
}
|
|
elseif (is_scalar($recurse)) {
|
|
throw new \Exception("An error occured trying to fetch a value from a scalar value using path '$path'");
|
|
}
|
|
}
|
|
|
|
return static::arrayGet($recurse, implode($delimiter, $pathArr), $delimiter);
|
|
}
|
|
else {
|
|
return $array[$pathArr[0]];
|
|
}
|
|
}
|
|
else {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public static function splitKeys(array &$array, string $delimiter = ".", string $enclosed = '{}') {
|
|
if ( is_array($array) ) {
|
|
$swap = [];
|
|
|
|
foreach($array as $key => &$value) {
|
|
$process = strstr($key, $delimiter);
|
|
|
|
if ( $enclosed ) {
|
|
if ( ( strlen($key) - 2 ) !== strlen( $tkey = trim($key, $enclosed) ) ) {
|
|
$process = false;
|
|
}
|
|
else {
|
|
$oldKey = $key;
|
|
$key = $tkey;
|
|
}
|
|
}
|
|
|
|
if ( $process ) {
|
|
$tmp = [];
|
|
$keylist = explode($delimiter, $key);
|
|
$finalKey = array_shift($keylist);
|
|
static::arrayKeysSet($keylist, $tmp, $value);
|
|
$swap[$finalKey] = static::splitKeys($tmp, $delimiter, $enclosed);
|
|
|
|
unset($array[isset($oldKey) ? $oldKey : $key]);
|
|
}
|
|
else if ( is_array($value) ) {
|
|
$value = static::splitKeys($value, $delimiter, $enclosed);
|
|
}
|
|
}
|
|
|
|
foreach($swap as $key => $item) {
|
|
if ( $enclosed ) {
|
|
if ( ( strlen($key) - 2 ) === ( $tkey = trim($key, $enclosed)) ) {
|
|
$key = $tkey;
|
|
}
|
|
}
|
|
|
|
$array[$key] = $item;
|
|
}
|
|
}
|
|
|
|
return $array;
|
|
}
|
|
|
|
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);
|
|
|
|
foreach ($iterator as $file) {
|
|
if ( $file->isFile() || $file->isDir() ) {
|
|
if ($fileExtension && ( $file->getExtension() === $fileExtension )) {
|
|
yield $file;
|
|
}
|
|
else {
|
|
yield $file;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|