/** * Parse a string to get the string * with executed functions * * @param string $str * @return string */ public static function parse($str) { $explode = explode("{", $str); $shortcuts = array(); foreach ($explode as $i => $exp) { if (preg_match('/\\}/', $exp)) { $shCut = explode("}", $exp); $shortcuts[] = "{" . $shCut[0] . "}"; $shortcuts[] = $shCut[1]; } else { $shortcuts[] = $exp; } } $shortcuts = ArrayHelper::clean($shortcuts); $result = ""; // Execute shortcuts and build result string for ($i = 0, $len = count($shortcuts); $i < $len; $i++) { if (self::isShortcut($shortcuts[$i])) { $name = strtolower(self::getStringArguments($shortcuts[$i], 1)); $value = self::getStringArguments($shortcuts[$i], 2); $result .= self::executeShortcut($name, $value); } else { $result .= $shortcuts[$i]; } } return $result; }
/** * Returns the default destination for * a method (e.g. view:index) * * @param string $method * @return string */ public function getDefaultDestination($method) { $urlLayout = explode('/', $this->getUrlLayout()); $urlLayout = ArrayHelper::clean($urlLayout); $defaultMethod = 'index'; for ($i = 0; $i < count($urlLayout); $i++) { if (true == preg_match('/' . $method . ':/', $urlLayout[$i])) { $exploded = explode(':', $urlLayout[$i]); $defaultMethod = end($exploded); } } return $defaultMethod; }
/** * @return array */ public function getParts() { return ArrayHelper::clean(explode("/", $this->fullFrom)); }
/** * This will check for replacement flag the user is able * to set. This will prevent a merging of the childs * from a subtree and remove the one which is marked * as: replaceable="true" */ private function checkReplacements() { $xmls = array(); foreach ($this->xmls as $xml) { $name = $xml->getTreeElement()->getName(); if (!array_key_exists($name, $xmls)) { $xmls[$name] = array(); } $xmls[$name][] = $xml; } // Handle conflicts ... foreach ($xmls as $name => $xml) { // ... between 2 elements if (count($xml) == 2) { $affector = null; $replace = false; foreach ($xmls[$name] as $subXml) { if (filter_var($subXml->getTreeElement()->getAttribute('replaceable'), FILTER_VALIDATE_BOOLEAN)) { $affector = $subXml; } else { if (filter_var($subXml->getTreeElement()->getAttribute('replace'), FILTER_VALIDATE_BOOLEAN)) { $replace = true; } } } if ($replace) { if ($affector) { $index = array_search($affector, $this->xmls); if (is_int($index)) { // Delete from array and resort so the index won't mess up unset($this->xmls[$index]); ArrayHelper::clean($this->xmls); } else { throw new Config\Exception\XmlTreeNotFoundException('The xml tree could not be found (for replacement) in the config list'); } } else { throw new Config\Exception\NoAffectedXmlTreeFoundException('No affected xml tree found to remove. Please use replaceable="true" as attributes to define the replacement'); } } } } }
/** * Gets all element by a * path sequence. It creates * the list only for the last path * segment * * @param string $path * @param boolean $collect * @return \Fewlines\Core\Xml\Tree\Element|boolean|array */ public function getElementsByPath($path, $collect = true) { $parts = explode("/", $path); $parts = ArrayHelper::clean($parts); $rootName = $parts[0]; $treeElement = $this->getTreeElement(); if ($treeElement->getName() != $rootName || (!$this->isValid() || !$this->tree->hasRoot())) { return false; } $result = $treeElement; $resultList = array(); for ($i = 1, $partsLen = count($parts); $i < $partsLen; $i++) { if (true == $collect && $i == $partsLen - 1) { $resultList = $result->getChildrenByName($parts[$i]); } if ($result) { $result = $result->getChildByName($parts[$i]); } } if (true == $collect) { if (false == empty($resultList)) { return $resultList; } else { if (false == empty($result)) { return array($result); } else { return array(); } } } return $result; }
/** * Get a translation from a file by a path * * @param string|arary $path * @return string */ public static function get($path) { $project = ProjectManager::getActiveProject(); $project = $project ? $project : ProjectManager::getDefaultProject(); $pathParts = ArrayHelper::clean(explode(self::SUBPATH_SEPERATOR, $path)); $localeDir = PathHelper::createPath(array($project->getTranslationPath(), Locale::getKey())); $entryPoint = ''; $entryPointIndex = 0; // Check if path is empty if (empty($pathParts)) { return ''; } // Get entry point file for ($i = 0, $len = count($pathParts); $i < $len; $i++) { $isFile = false; $localeDir = PathHelper::getRealPath($localeDir); for ($x = 0, $lenX = count(self::$translationTypes); $x < $lenX; $x++) { $fileExt = self::$translationTypes[$x]; $pathPart = $pathParts[$i]; $isFile = is_file($localeDir . $pathPart . '.' . $fileExt); // Escape loop if file was found if (true == $isFile) { break; } } // Attach current "dir" to the localdir (next level) $localeDir .= $pathPart; // Excape loop if entry point was found if (true == $isFile) { $entryPointIndex = $i; $entryPoint = $localeDir . '.' . $fileExt; break; } } /** * If there is no entry point take * all supported translation files * from the given directory */ if (empty($entryPoint) && is_dir($localeDir)) { $files = DirHelper::scanDir($localeDir); $names = array(); foreach ($files as $file) { $extension = pathinfo($file['path'], PATHINFO_EXTENSION); $filename = pathinfo($file['path'], PATHINFO_FILENAME); if (array_search($extension, self::$translationTypes) !== false) { $names[] = $filename; } } return $names; } // if (true == empty($entryPoint)) { // throw new Translator\Exception\EntryPointNotFoundException("No entry point (file) found for: " . (string) $path); // } $pathParts = array_slice($pathParts, $entryPointIndex + 1); $pathParts = ArrayHelper::clean($pathParts); // The default translation value $value = $path; /** * Operate with the key functions for different file types. * At this point we are handling valid values */ switch ($fileExt) { case self::$translationTypes[0]: $value = self::getValueByKeyPHP($entryPoint, $pathParts); break; case self::$translationTypes[1]: $value = self::getValueByKeyCSV($entryPoint, $pathParts); break; } /** * If the value is a array and empty it doesn't * need to be returned. Return a empty string * instead */ if (empty($value)) { return ''; } else { return $value; } }