Collapse (minify) media

@param string $input

@return string
@throws \Exception
761556
public function collapse($input)
    {
        $this->inQuotes = false;
        $this->inSingleQuotes = false;
        $this->currentCharacter = false;
        $this->nextCharacter = false;
        $this->lastCharacter = false;
        $this->skipNext = 0;
        $this->lastAdded = 0;
        $this->buildingWord = $this->lastWord = '';

        $initialSize = mb_strlen($input);
        $timeStart = microtime(true);

        $this->input = str_replace(array("\r", "\t"), array('', ' '), trim($input));
        unset($input);

        if (empty($this->input))
        {
            throw new \Exception("No data has been supplied to media collapser.");
        }

        $characterCount = mb_strlen($this->input);

        $output = '';

        for ($i = 0; $i < $characterCount; ++$i)
        {
            if (!empty($this->skipNext)) {
                $this->skipNext--;
                continue;
            }

            $this->currentIndex = $i;
            $character = mb_substr($this->input, $i, 1);
            $this->currentCharacter = ord($character);
            $this->nextCharacter = ($this->currentIndex != $characterCount) ? ord(mb_substr($this->input, $i + 1, 1)) : false;
            $this->lastCharacter = ($this->currentIndex != 0) ? ord(mb_substr($this->input, $i - 1, 1)) : false;

            $methodName = 'handleCharacter' . $this->currentCharacter;

            if (method_exists($this, $methodName))
            {
                $return = $this->$methodName();
            }
            else
            {
                $return = $this->handleCharacter();
            }

            if ($return === false)
            {
                $this->lastWord = $this->buildingWord;
                $this->buildWord = '';
                continue;
            }
            else if ($return === true)
            {
                $output .= $character;
                $this->lastAdded = $this->currentCharacter;

                if (ctype_alpha($character)) {
                    $this->buildingWord .= $character;
                } else {
                    $this->lastWord = $this->buildingWord;
                    $this->buildingWord = '';
                }
            }
            else if (is_string($return))
            {
                $output .= $return;
                $this->lastAdded = ord($output[mb_strlen($output) - 1]);

                $this->lastWord = $this->buildingWord;
                $this->buildingWord = '';
            }
        }

        $this->sizeSavings = ($initialSize - mb_strlen($output));

        if ($this->debug) {
            $output .= "\n" . '/* culled ' . number_format($this->sizeSavings) . ' chars in ' . number_format((microtime(true) - $timeStart) * 1000, 2) . 'ms */';
        }

        return $output;
    }
276125
public function parseFunction(string $input): Method
    {
        if (! \preg_match(self::_FUNCTION, trim($input), $matches)) {
            throw new \Exception(sprintf('Unable to parse input [%s]', $input));
        }

        return new Method($matches['name'], $matches['in'], $matches['out']);
    }
684340
private function dilemma($input = null, $from = false) {
        // calculate from and to
        list($plugin, $from) = $this->_View->pluginSplit($from, false);
        $from = Configure::read('App.cssBaseUrl') . $from;
        if(isset($plugin))
            $from = Inflector::underscore($plugin) . '/' . $from;

        // start converter
        $converter = new Converter("/{$from}-fake-file.css", "{$this->_config['paths']['css']}/cache-fake-file.css");

        // fix paths
        return $this->morph($input, function($url) use($converter) {
            return $converter->convert($url);
        });
    }
1305240
public function canonicalize($input)
    {
        $strippedInput = str_replace(' ', '', $input);
        if (!preg_match('/^\d{4}[A-Za-z]{2}$/', $strippedInput)) {
            return $input;
        }

        return implode(' ', [substr($strippedInput, 0, 4), mb_strtoupper(substr($strippedInput, -2, 2))]);
    }
835327
private function filterEmptyInput($input)
    {
        foreach ($input as $key => $value)
        {
            if (empty($value))
            {
                unset($input[$key]);
            }
        }

        return $input;
    }
183364
public static function pluralize($input) {
		if (self::$pluralizer === null) {
			self::$pluralizer = new StandardEnglishSingularizer();
		}

		return self::$pluralizer->getPluralForm($input);
	}
469944
public function encrypt($input)
    {
        if (! $input) {
            return $input;
        }

        $methods = $this->getEncryptionMethods();
        $method  = reset($methods);
        $mkey    = key($methods);

        if ('mcrypt' == $method) {
            $result = $this->encryptMcrypt($input);
        } elseif ('null' == $method) {
            $result = $input;
        } else {
            $result = $this->encryptOpenSsl($input, $method);
        }

        return ":$mkey:" . base64_encode($result);
    }
312999
public function canonicalize($input)
    {
        //first, return unmodified any input less than 5 characters long, or anything not a string
        if (!is_string($input) or strlen($input) < 5) {
            return $input;
        }

        //second, remove any spaces, uppercase, and break off the last three characters of the string
        $unspaced = mb_strtoupper(preg_replace('/\s/', '', $input) ?? '', 'UTF-8');
        $spaced = sprintf('%s %s', substr($unspaced, 0, -3), substr($unspaced, -3));

        //now check against the regex
        if (!preg_match(self::CODE_REGEX, $spaced)) {
            return $input;
        }

        return $spaced;
    }
988405
protected static function tagDispatcher($input)
    {
        // textarea, pre, code tags and comments are to be left alone to avoid any non-wanted whitespace inside them so it just outputs them as they were
        if (substr($input[1], 0, 9) == '<textarea' || substr($input[1], 0, 4) == '<pre' || substr($input[1], 0, 5) == '<code' || substr($input[1], 0, 4) == '<!--' || substr($input[1], 0, 9) == '<![CDATA[') {
            return $input[1] . $input[3];
        }
        // closing textarea, code and pre tags and self-closed tags (i.e. <br />) are printed as singleTags because we didn't use openTag for the formers and the latter is a single tag
        if (substr($input[1], 0, 10) == '</textarea' || substr($input[1], 0, 5) == '</pre' || substr($input[1], 0, 6) == '</code' || substr($input[1], - 2) == '/>') {
            return self::singleTag($input[1], $input[3], $input[2]);
        }
        // it's the closing tag
        if ($input[0][1] == '/') {
            return self::closeTag($input[1], $input[3], $input[2]);
        }

        // opening tag
        return self::openTag($input[1], $input[3], $input[2]);
    }
446302
public function minify()
    {
        $css_files = $this->cleanStack($this->get(), 'file');

        $organized_css = array('rest'=>array());
        foreach ($css_files as $_file) {
            if (!empty($_file['media'])) {
                if (!isset($organized_css[ $_file['media'] ])) {
                    $organized_css[ $_file['media'] ] = array();
                }
                $organized_css[ $_file['media'] ][] = $_file;
            } else {
                $organized_css['rest'][] = $_file;
            }
        }

        foreach ($organized_css as $media=>$stack) {
            $cleaned_stack = $this->extractFromStack($stack, 'file');
            if (!empty($cleaned_stack)) {
                $this->addMinified(
                    $this->minifyStack($cleaned_stack), $media=='rest' ? 'screen' : $media
                );
            }
        }

        return $this;
    }
1107013
public function modify($input)
    {
        foreach ($this->getModifiers() as $modifier) {
            $input = $modifier->modify($input);
        }

        return $input;
    }
883727
public function input($filename, $input)
    {
        if (substr($filename, strlen($this->_settings['ext']) * -1) !== $this->_settings['ext']) {
            return $input;
        }

        $tmpfile = tempnam(TMP, 'asset_compress_less');
        $this->_generateScript($tmpfile, $input);

        $bin = $this->_settings['node'] . ' ' . $tmpfile;
        $env = array('NODE_PATH' => $this->_settings['node_path']);
        $return = $this->_runCmd($bin, '', $env);
        unlink($tmpfile);
        return $return;
    }
63751
protected function prepareInputFilesForParsing(array $input = [])
    {
        $parse = [];
        foreach ($input as $in) {
            $less = realpath(WWW_ROOT . $in);
            // If we have plugin notation (Plugin.less/file.less)
            // ensure to properly load the files
            list($plugin, $basefile) = $this->_View->pluginSplit($in, false);
            if (!empty($plugin)) {
                $less = realpath(Plugin::path($plugin) . 'webroot' . DS . $basefile);

                if ($less !== false) {
                    $parse[$less] = $this->assetBaseUrl($plugin, $basefile);
                    continue;
                }
            }
            if ($less !== false) {
                $parse[$less] = '';
                continue;
            }
            // Plugins without plugin notation (/plugin/less/file.less)
            list($plugin, $basefile) = $this->assetSplit($in);
            if ($file = $this->pluginAssetFile([$plugin, $basefile])) {
                $parse[$file] = $this->assetBaseUrl($plugin, $basefile);
                continue;
            }

            // Will probably throw a not found error
            $parse[$in] = '';
        }

        return $parse;
    }
1141631
function sanitize( $input ) {
		$options = [];

		if ( ! is_array( $input ) ) {
			$input = [];
		}

		if ( property_exists( $this, 'booleans' ) ) {
			foreach ( $this->booleans as $key ) {
				if ( empty( $input[ $key ] ) ) {
					$options[ $key ] = 0;
				} else {
					$options[ $key ] = 1;
				}
			}
		}

		if ( property_exists( $this, 'strings' ) ) {
			foreach ( $this->strings as $key ) {
				if ( empty( $input[ $key ] ) ) {
					unset( $options[ $key ] );
				} else {
					$options[ $key ] = sanitize_text_field( $input[ $key ] );
				}
			}
		}

		if ( property_exists( $this, 'urls' ) ) {
			foreach ( $this->urls as $key ) {
				if ( empty( $input[ $key ] ) ) {
					unset( $options[ $key ] );
				} else {
					$value = trim( strip_tags( stripslashes( $input[ $key ] ) ) );
					if ( $value ) {
						$options[ $key ] = \Pressbooks\Sanitize\canonicalize_url( $value );
					} else {
						unset( $options[ $key ] );
					}
				}
			}
		}

		if ( property_exists( $this, 'integers' ) ) {
			foreach ( $this->integers as $key ) {
				if ( empty( $input[ $key ] ) ) {
					unset( $options[ $key ] );
				} else {
					$options[ $key ] = absint( $input[ $key ] );
				}
			}
		}

		if ( property_exists( $this, 'floats' ) ) {
			foreach ( $this->floats as $key ) {
				if ( empty( $input[ $key ] ) ) {
					unset( $options[ $key ] );
				} else {
					$options[ $key ] = filter_var( $input[ $key ], FILTER_VALIDATE_FLOAT );
				}
			}
		}

		if ( property_exists( $this, 'predefined' ) ) {
			foreach ( $this->predefined as $key ) {
				if ( empty( $input[ $key ] ) ) {
					unset( $options[ $key ] );
				} else {
					$options[ $key ] = $input[ $key ];
				}
			}
		}

		return $options;
	}
661556
public function prepare($input)
    {
        $form = $this->buildEmbeddedForm();

        return $form->setOriginal($this->original)->prepare($input);
    }
691515
public function minify()
	{
		$data = '';

		foreach ($this->files as $fileData)
		{
			$fileData = $this->handleFile($fileData);

			$data .= $this->doMinify($fileData['content'], $fileData['options']);
		}

		return $data;
	}
66933
protected function _combine($input)
    {
        $inp_len = count($input);
        foreach (self::$NP['replacemaps'] as $np_src => $np_target) {
            if ($np_target[0] != $input[0]) continue;
            if (count($np_target) != $inp_len) continue;
            $hit = false;
            foreach ($input as $k2 => $v2) {
                if ($v2 == $np_target[$k2]) {
                    $hit = true;
                } else {
                    $hit = false;
                    break;
                }
            }
            if ($hit) return $np_src;
        }
        return false;
    }