1<?php 2/** 3 * Copyright (c) 2020. ComboStrap, Inc. and its affiliates. All Rights Reserved. 4 * 5 * This source code is licensed under the GPL license found in the 6 * COPYING file in the root directory of this source tree. 7 * 8 * @license GPL 3 (https://www.gnu.org/licenses/gpl-3.0.en.html) 9 * @author ComboStrap <support@combostrap.com> 10 * 11 */ 12 13namespace ComboStrap; 14 15/** 16 * Class PipelineUtility 17 * @package ComboStrap 18 * A pipeline to perform filter transformation 19 * 20 * See also 21 * https://getbootstrap.com/docs/5.0/helpers/text-truncation/ 22 */ 23class PipelineUtility 24{ 25 26 /** 27 * @param $input 28 * @return string 29 */ 30 static public function execute($input) 31 { 32 33 /** 34 * Get the value 35 */ 36 $firstQuoteChar = strpos($input, '"'); 37 $input = substr($input, $firstQuoteChar + 1); 38 $secondQuoteChar = strpos($input, '"'); 39 $value = substr($input, 0, $secondQuoteChar); 40 $input = substr($input, $secondQuoteChar + 1); 41 42 /** 43 * Go to the first | and delete it from the input 44 */ 45 $pipeChar = strpos($input, '|'); 46 $input = substr($input, $pipeChar + 1); 47 48 /** 49 * Get the command and applies them 50 */ 51 $commands = preg_split("/\|/", $input); 52 foreach ($commands as $command) { 53 $command = trim($command, " )"); 54 $leftParenthesis = strpos($command, "("); 55 $commandName = substr($command, 0, $leftParenthesis); 56 $signature = substr($command, $leftParenthesis + 1); 57 $commandArgs = preg_split("/,/", $signature); 58 $commandArgs = array_map( 59 'trim', 60 $commandArgs, 61 array_fill(0, sizeof($commandArgs), "\"") 62 ); 63 $commandName = trim($commandName); 64 if (!empty($commandName)) { 65 switch ($commandName) { 66 case "replace": 67 $value = self::replace($commandArgs, $value); 68 break; 69 case "head": 70 $value = self::head($commandArgs, $value); 71 break; 72 case "tail": 73 $value = self::tail($commandArgs, $value); 74 break; 75 case "rconcat": 76 $value = self::concat($commandArgs, $value, "right"); 77 break; 78 case "lconcat": 79 $value = self::concat($commandArgs, $value, "left"); 80 break; 81 case "cut": 82 $value = self::cut($commandArgs, $value); 83 break; 84 case "trim": 85 $value = trim($value); 86 break; 87 case "capitalize": 88 $value = ucwords($value); 89 break; 90 default: 91 LogUtility::msg("command ($commandName) is unknown", LogUtility::LVL_MSG_ERROR, "pipeline"); 92 } 93 } 94 } 95 return $value; 96 } 97 98 private static function replace(array $commandArgs, $value) 99 { 100 $search = $commandArgs[0]; 101 $replace = $commandArgs[1]; 102 return str_replace($search, $replace, $value); 103 } 104 105 /** 106 * @param array $commandArgs 107 * @param $value 108 * @return false|string 109 * See also: https://getbootstrap.com/docs/5.0/helpers/text-truncation/ 110 */ 111 private static function head(array $commandArgs, $value) 112 { 113 $length = $commandArgs[0]; 114 if (strlen($value) < $length) { 115 return $value; 116 } 117 $headValue = substr($value, 0, $length); 118 $tail = $commandArgs[1]; 119 if ($tail !== null) { 120 $headValue .= $tail; 121 } 122 return $headValue; 123 } 124 125 private static function concat(array $commandArgs, $value, $side) 126 { 127 $string = $commandArgs[0]; 128 switch ($side) { 129 case "left": 130 return $string . $value; 131 case "right": 132 return $value . $string; 133 default: 134 LogUtility::msg("The side value ($side) is unknown", LogUtility::LVL_MSG_ERROR, "pipeline"); 135 } 136 137 138 } 139 140 private static function tail(array $commandArgs, $value) 141 { 142 $length = $commandArgs[0]; 143 return substr($value, strlen($value) - $length); 144 } 145 146 private static function cut(array $commandArgs, $value) 147 { 148 $pattern = $commandArgs[0]; 149 $words = preg_split("/$pattern/i", $value); 150 if ($words !== false) { 151 $selector = $commandArgs[1]; 152 $startEndSelector = preg_split("/-/i", $selector); 153 $start = $startEndSelector[0] - 1; 154 $end = null; 155 if (isset($startEndSelector[1])) { 156 $end = $startEndSelector[1]; 157 if (empty($end)) { 158 $end = sizeof($words); 159 } 160 $end = $end - 1; 161 } 162 if ($end == null) { 163 if (isset($words[$start])) { 164 return $words[$start]; 165 } else { 166 return $value; 167 } 168 } else { 169 $result = ""; 170 for ($i = $start; $i <= $end; $i++) { 171 if (isset($words[$i])) { 172 if (!empty($result)) { 173 $result .= $pattern; 174 } 175 $result .= $words[$i]; 176 } 177 } 178 return $result; 179 } 180 181 } else { 182 return "An error occurred: could not split with the pattern `$pattern`, the value `$value`."; 183 } 184 } 185 186} 187