$newValue) { if (is_array($newValue) && empty($newValue)) { continue; } if (is_array($newValue) && array_key_exists($newName, $currentArray) && is_array($currentArray[$newName])) { // check __APPEND__ identifier $appendKey = array_search($mergeIdentifier, $newValue, true); if ($appendKey !== false) { unset($newValue[$appendKey]); $newValue = array_merge($currentArray[$newName], $newValue); } else if (!static::isSingleArray($newValue)) { $newValue = static::merge($currentArray[$newName], $newValue); } } //check if exists __APPEND__ identifier and remove its if (!isset($currentArray[$newName]) && is_array($newValue)) { $newValue = static::unsetInArrayByValue($mergeIdentifier, $newValue); } $currentArray[$newName] = $newValue; } return $currentArray; } /** * Unset a value in array recursively * * @param string $needle * @param array $haystack * @param bool $reIndex * @return array */ public static function unsetInArrayByValue($needle, array $haystack, $reIndex = true) { foreach($haystack as $key => $value) { if (is_array($value)) { $haystack[$key] = static::unsetInArrayByValue($needle, $value); } else if ($needle === $value) { if ($reIndex) { array_splice($haystack, $key, 1); } else { unset($haystack[$key]); } } } return $haystack; } /** * Get a full path of the file * * @param string | array $folderPath - Folder path, Ex. myfolder * @param string $filePath - File path, Ex. file.json * * @return string */ public static function concatPath($folderPath, $filePath = null) { if (is_array($folderPath)) { $fullPath = ''; foreach ($folderPath as $path) { $fullPath = static::concatPath($fullPath, $path); } return static::fixPath($fullPath); } if (empty($filePath)) { return static::fixPath($folderPath); } if (empty($folderPath)) { return static::fixPath($filePath); } if (substr($folderPath, -1) == static::getSeparator() || substr($folderPath, -1) == '/') { return static::fixPath($folderPath . $filePath); } return $folderPath . static::getSeparator() . $filePath; } /** * Fix path separator * * @param string $path * @return string */ public static function fixPath($path) { return str_replace('/', static::getSeparator(), $path); } /** * Convert array to object format recursively * * @param array $array * @return object */ public static function arrayToObject($array) { if (is_array($array)) { return (object) array_map("static::arrayToObject", $array); } else { return $array; // Return an object } } /** * Convert object to array format recursively * * @param object $object * @return array */ public static function objectToArray($object) { if (is_object($object)) { $object = (array) $object; } return is_array($object) ? array_map("static::objectToArray", $object) : $object; } /** * Appends 'Obj' if name is reserved PHP word. * * @param string $name * @return string */ public static function normilizeClassName($name) { if (in_array($name, self::$reservedWords)) { $name .= 'Obj'; } return $name; } /** * Get Naming according to prefix or postfix type * * @param string $name * @param string $prePostFix * @param string $type * * @return string */ public static function getNaming($name, $prePostFix, $type = 'prefix', $symbol = '_') { if ($type == 'prefix') { return static::toCamelCase($prePostFix.$symbol.$name, $symbol); } else if ($type == 'postfix') { return static::toCamelCase($name.$symbol.$prePostFix, $symbol); } return null; } /** * Replace $search in array recursively * * @param string $search * @param string $replace * @param string $array * @param string $isKeys * * @return array */ public static function replaceInArray($search = '', $replace = '', $array = false, $isKeys = true) { if (!is_array($array)) { return str_replace($search, $replace, $array); } $newArr = array(); foreach ($array as $key => $value) { $addKey = $key; if ($isKeys) { //Replace keys $addKey = str_replace($search, $replace, $key); } // Recurse $newArr[$addKey] = static::replaceInArray($search, $replace, $value, $isKeys); } return $newArr; } /** * Unset content items defined in the unset.json * * @param array $content * @param string | array $unsets in format * array( * 'EntityName1' => array( 'unset1', 'unset2' ), * 'EntityName2' => array( 'unset1', 'unset2' ), * ) * OR * array('EntityName1.unset1', 'EntityName1.unset2', .....) * OR * 'EntityName1.unset1' * * @return array */ public static function unsetInArray(array $content, $unsets) { if (empty($unsets)) { return $content; } if (is_string($unsets)) { $unsets = (array) $unsets; } foreach($unsets as $rootKey => $unsetItem){ $unsetItem = is_array($unsetItem) ? $unsetItem : (array) $unsetItem; foreach($unsetItem as $unsetSett){ if (!empty($unsetSett)){ $keyItems = explode('.', $unsetSett); $currVal = isset($content[$rootKey]) ? "\$content['{$rootKey}']" : "\$content"; $lastKey = array_pop($keyItems); foreach($keyItems as $keyItem){ $currVal .= "['{$keyItem}']"; } $unsetElem = $currVal . "['{$lastKey}']"; $currVal = " if (isset({$unsetElem}) || ( is_array({$currVal}) && array_key_exists({$lastKey}, {$currVal}) )) { unset({$unsetElem}); } "; eval($currVal); } } } return $content; } /** * Get class name from the file path * * @param string $filePath * * @return string */ public static function getClassName($filePath) { $className = preg_replace('/\.php$/i', '', $filePath); $className = preg_replace('/^(application|custom)\//i', '', $className); $className = '\\'.static::toFormat($className, '\\'); return $className; } /** * Return values of defined $key. * * @param array $array * @param string $key Ex. of key is "entityDefs", "entityDefs.User" * @param mixed $default * @return mixed */ public static function getValueByKey(array $array, $key = null, $default = null) { if (!isset($key) || empty($key)) { return $array; } $keys = explode('.', $key); $lastItem = $array; foreach($keys as $keyName) { if (isset($lastItem[$keyName]) && is_array($lastItem)) { $lastItem = $lastItem[$keyName]; } else { return $default; } } return $lastItem; } /** * Check if two variables are equals * * @param mixed $var1 * @param mixed $var2 * @return boolean */ public static function isEquals($var1, $var2) { if (is_array($var1)) { static::ksortRecursive($var1); } if (is_array($var2)) { static::ksortRecursive($var2); } return ($var1 === $var2); } /** * Sort array recursively * @param array $array * @return bool */ public static function ksortRecursive(&$array) { if (!is_array($array)) { return false; } ksort($array); foreach ($array as $key => $value) { static::ksortRecursive($array[$key]); } return true; } public static function isSingleArray(array $array) { foreach ($array as $key => $value) { if (!is_int($key)) { return false; } } return true; } } ?>