Source of file StringObject.php
Size: 3,741 Bytes - Last Modified: 2021-12-23T10:20:55+00:00
/var/www/docs.ssmods.com/process/src/thirdparty/css-crush/lib/CssCrush/StringObject.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 | <?php /** * * String sugar. * */ namespace CssCrush; class StringObject { public function __construct($str) { $this->raw = $str; } public function __toString() { return $this->raw; } public static function endsWith($haystack, $needle) { return substr($haystack, -strlen($needle)) === $needle; } public function update($str) { $this->raw = $str; return $this; } public function substr($start, $length = null) { if (! isset($length)) { return substr($this->raw, $start); } else { return substr($this->raw, $start, $length); } } public function matchAll($patt, $offset = 0) { return Regex::matchAll($patt, $this->raw, $offset); } public function replaceHash($replacements) { if ($replacements) { $this->raw = str_replace( array_keys($replacements), array_values($replacements), $this->raw); } return $this; } public function pregReplaceHash($replacements) { if ($replacements) { $this->raw = preg_replace( array_keys($replacements), array_values($replacements), $this->raw); } return $this; } public function pregReplaceCallback($patt, $callback) { $this->raw = preg_replace_callback($patt, $callback, $this->raw); return $this; } public function append($append) { $this->raw .= $append; return $this; } public function prepend($prepend) { $this->raw = $prepend . $this->raw; return $this; } public function splice($replacement, $offset, $length = null) { $this->raw = substr_replace($this->raw, $replacement, $offset, $length); return $this; } public function trim() { $this->raw = trim($this->raw); return $this; } public function rTrim() { $this->raw = rtrim($this->raw); return $this; } public function lTrim() { $this->raw = ltrim($this->raw); return $this; } public function restore($types, $release = false, $callback = null) { $this->raw = Crush::$process->tokens->restore($this->raw, $types, $release, $callback); return $this; } public function captureDirectives($directive, $parse_options = array()) { if (is_array($directive)) { $directive = '(?:' . implode('|', $directive) . ')'; } $parse_options += array( 'keyed' => true, 'lowercase_keys' => true, 'ignore_directives' => true, 'singles' => false, 'flatten' => false, ); if ($parse_options['singles']) { $patt = Regex::make('~@(?i)' . $directive . '(?-i)(?:\s*{{ block }}|\s+(?<name>{{ ident }})\s+(?<value>[^;]+)\s*;)~S'); } else { $patt = Regex::make('~@(?i)' . $directive . '(?-i)\s*{{ block }}~S'); } $captured_directives = array(); $this->pregReplaceCallback($patt, function ($m) use (&$captured_directives, $parse_options) { if (isset($m['name'])) { $name = $parse_options['lowercase_keys'] ? strtolower($m['name']) : $m['name']; $captured_directives[$name] = $m['value']; } else { $captured_directives = DeclarationList::parse($m['block_content'], $parse_options) + $captured_directives; } return ''; }); return $captured_directives; } } |