/** * List files under a path * * @param string $path Path which files will be enlisted * @param boolean $recursive [optional] If listing shall be recursive. Default is FALSE * @return array */ function listfiles($path, $recursive = false) { $iterator = $recursive ? new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::SKIP_DOTS) : new DirectoryIterator($path); $array = \Utils\Arrays::FromTraversable($iterator); if (!$recursive) { $array = \Utils\Arrays::Filter($array, ['.', '..']); } return $array; }
/** * Get or set a JSON key value * * @param string $key_or_dimension The key or JSON dimension, which value will be assigned * @param string $value [optional] If specified, the value to be assigned to this key * @return string */ public function json($key_or_dimension, $value = UNDEFINED) { if ($value == UNDEFINED) { // If target key is single-dimensional if (in_array(preg_match('/^(\\{.+\\})+$/', $key_or_dimension), [false, 0])) { if (isset($this->json_contents[$key_or_dimension])) { return $this->json_contents[$key_or_dimension]; } } else { return \Utils\Arrays::Traverse($this->json_contents, $key_or_dimension); } return UNDEFINED; } }
/** * Convert a runtime value into a virtual runtime value. Returns FALSE if value can't be determined. * * @param string $value Any scalar value, EXCEPT if it's an array. * @param boolean $throw [optional] If an exception will be thrown once a virtual runtime value can't be determined. This is useful when determining complex array values. * @return string|boolean */ public static function GetVirtual($value, $throw = true) { if (is_string($value)) { return String::Surround($value, "'"); } else { if (is_array($value)) { $closure_for_assoc = function ($current) { $newresult = []; foreach ($current as $key => $value) { array_push($newresult, sprintf("%s => %s", self::GetVirtual($key), $value)); // note that we didn't get Virtual value of $value since this has been previously virualized using array_map() } return $newresult; }; $result = array_map(function ($current) { return self::GetVirtual($current); }, $value); $result = str("[ {0} ]", implode(", ", Arrays::IsIndexed($result) ? $result : $closure_for_assoc($result))); return $result; } else { if (is_bool($value)) { return $value ? 'true' : 'false'; } else { if (is_null($value)) { return "null"; } else { if (is_scalar($value)) { return (string) $value; } } } } } if ($throw) { throw new ScalarValueException($value); } return false; }
/** * Assert that given arrays are equal * * @param array $array1 First array * @param array $array2 Second array * @param string $message [optional] The testing error message * * @return boolean If this test passed */ public function assertArrayEqual(array $array1, array $array2, $message = null) { $diff = \Utils\Arrays::Diff($array1, $array2); return $this->assert(new EqualExpectation(0), sizeof($diff), !empty($message) ? $message : sprintf("Arrays not equal, difference: %s", dump($diff, true))); }
public function testFilter_Recursive() { $test = $this->array; $filter = ['Allen Linatoc', 21, 'Laguna', 14.1241]; $expect = ['address' => ['city' => 'Calamba', 'country' => 'Philippines', 'coordinates' => ['latitude' => 121.00212]]]; $result = Arrays::Filter($test, $filter, true); $this->assertArrayEqual($expect, $result); }