Elgg  Version master
StringObject.php
Go to the documentation of this file.
1 <?php
8 namespace CssCrush;
9 
10 #[\AllowDynamicProperties]
12 {
13  public function __construct($str)
14  {
15  $this->raw = $str;
16  }
17 
18  public function __toString()
19  {
20  return $this->raw;
21  }
22 
23  public static function endsWith($haystack, $needle)
24  {
25  return substr($haystack, -strlen($needle)) === $needle;
26  }
27 
28  public function update($str)
29  {
30  $this->raw = $str;
31 
32  return $this;
33  }
34 
35  public function substr($start, $length = null)
36  {
37  if (! isset($length)) {
38 
39  return substr($this->raw, $start);
40  }
41  else {
42 
43  return substr($this->raw, $start, $length);
44  }
45  }
46 
47  public function matchAll($patt, $offset = 0)
48  {
49  return Regex::matchAll($patt, $this->raw, $offset);
50  }
51 
52  public function replaceHash($replacements)
53  {
54  if ($replacements) {
55  $this->raw = str_replace(
56  array_keys($replacements),
57  array_values($replacements),
58  $this->raw);
59  }
60  return $this;
61  }
62 
63  public function pregReplaceHash($replacements)
64  {
65  if ($replacements) {
66  $this->raw = preg_replace(
67  array_keys($replacements),
68  array_values($replacements),
69  $this->raw);
70  }
71  return $this;
72  }
73 
74  public function pregReplaceCallback($patt, $callback)
75  {
76  $this->raw = preg_replace_callback($patt, $callback, $this->raw);
77  return $this;
78  }
79 
80  public function append($append)
81  {
82  $this->raw .= $append;
83  return $this;
84  }
85 
86  public function prepend($prepend)
87  {
88  $this->raw = $prepend . $this->raw;
89  return $this;
90  }
91 
92  public function splice($replacement, $offset, $length = null)
93  {
94  $this->raw = substr_replace($this->raw, $replacement, $offset, $length);
95  return $this;
96  }
97 
98  public function trim()
99  {
100  $this->raw = trim($this->raw);
101  return $this;
102  }
103 
104  public function rTrim()
105  {
106  $this->raw = rtrim($this->raw);
107  return $this;
108  }
109 
110  public function lTrim()
111  {
112  $this->raw = ltrim($this->raw);
113  return $this;
114  }
115 
116  public function restore($types, $release = false, $callback = null)
117  {
118  $this->raw = Crush::$process->tokens->restore($this->raw, $types, $release, $callback);
119 
120  return $this;
121  }
122 
123  public function captureDirectives($directive, $parse_options = [])
124  {
125  if (is_array($directive)) {
126  $directive = '(?:' . implode('|', $directive) . ')';
127  }
128 
129  $parse_options += [
130  'keyed' => true,
131  'lowercase_keys' => true,
132  'ignore_directives' => true,
133  'singles' => false,
134  'flatten' => false,
135  ];
136 
137  if ($parse_options['singles']) {
138  $patt = Regex::make('~@(?i)' . $directive . '(?-i)(?:\s*{{ block }}|\s+(?<name>{{ ident }})\s+(?<value>[^;]+)\s*;)~S');
139  }
140  else {
141  $patt = Regex::make('~@(?i)' . $directive . '(?-i)\s*{{ block }}~S');
142  }
143 
144  $captured_directives = [];
145  $this->pregReplaceCallback($patt, function ($m) use (&$captured_directives, $parse_options) {
146  if (isset($m['name'])) {
147  $name = $parse_options['lowercase_keys'] ? strtolower($m['name']) : $m['name'];
148  $captured_directives[$name] = $m['value'];
149  }
150  else {
151  $captured_directives = DeclarationList::parse($m['block_content'], $parse_options) + $captured_directives;
152  }
153  return '';
154  });
155 
156  return $captured_directives;
157  }
158 }
restore($types, $release=false, $callback=null)
captureDirectives($directive, $parse_options=[])
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
if(!$item instanceof\ElggEntity) $length
Definition: excerpt.php:16
matchAll($patt, $offset=0)
if(empty($count)) $offset
Definition: pagination.php:26
pregReplaceHash($replacements)
Balanced bracket matching on string objects.
foreach($notification_settings as $purpose=> $prefered_methods) if((bool) elgg_get_config('enable_delayed_email')) $start
replaceHash($replacements)
pregReplaceCallback($patt, $callback)
substr($start, $length=null)
static endsWith($haystack, $needle)
splice($replacement, $offset, $length=null)