github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

michaelherndon / midori-php

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 4
    • 0
  • Source
  • Commits
  • Network (0)
  • Issues (3)
  • Downloads (0)
  • Wiki (1)
  • Graphs
  • Tree: 9e83502

click here to add a description

click here to add a homepage

  • Branches (3)
    • ctp
    • master
    • php5.2.x
  • Tags (0)
Sending Request…
Enable Donations

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

moved the official repo to http://github.com/amptools-net/midori-php/tree/trunk — Read more

  cancel

http://www.amptools.net

  cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

adding inflector, Midori_Regex adding methods to string, endsWith, gsub, 
indexOf, insert, lastIndexOf, match, padLeft, padRight, replace, trim,  
also adding quick functions regex and val 
U-Michael-PC\Michael (author)
Sat Jun 06 17:45:59 -0700 2009
commit  9e83502e28cbc278ec6bb4a293331dce9fa9dc7f
tree    60886645af4c2c2e60d3573290dadcc199d242a7
parent  e949c8591a730dea753fb3ca6d4c30a9999fcfc4
midori-php / src / Midori / String.php src/Midori/String.php
100755 578 lines (515 sloc) 16.89 kb
edit raw blame history
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
<?php
 
require_once "Inflector.php";
require_once "Regex.php";
/**
*
* @author Michael
* @final true
* @package Midori
* @property integer $length Gets the length of the string
* @property string $value Gets or sets the actual unboxed string value.
* @property boolean $hasValue Gets whether or not the value is not null.
* @property-read string $nullReplacementValue; Get the defaulted value for __toString when the value is null.
*/
final class Midori_String extends Midori_Nullable implements ArrayAccess
{
 
/**
* constructor
*
* @param string $value
* @return Midori_String the class
*/
public function __construct($value = "")
{
if($value instanceof Midori_Nullable)
$value = $value->value;
 
if($value != null)
$this->value = $value;
}
 
 
/**
*
* @ignore
* @see src/Midori/Midori_Nullable#getNullReplacementValue()
*/
protected function getNullReplacementValue()
{
return "";
}
 
 
private function str($value)
{
return new Midori_String($value);
}
 
public function camelize($lower = false)
{
return $this->str(Midori_Inflector::camelize($this->value), $lower);
}
 
/**
* creates a copy of the string and returns it.
*
* <p>Creates a copy of the current unboxed string and
* returns it.</p>
*
* <pre class="brush: php">
* $string = new Midori_String("value");
* $copy = $string->copy();
* echo ($string == $copy)? "true" : "false"; // true
* </pre>
*
* @return Midori_String returns a copy of the string.
*/
public function copy()
{
return $this->str($this->value);
}
 
 
/**
* Compares the string value to another value and returns
* if its this value is less than, equal, or greater than.
*
* <p>Compares the strings and either returns 0 if the values
* are equal, -1 if the value is less than the value its
* being compared to, or 1 if the value is greater than
* the value its being compared to.</p>
*
* <p>This comparsion is helpful for sorting strings alphanumerically.</p>
*
* <pre class="brush: php">
* $string = new Midori_String("adam");
* $valueToCompare = "nathan";
* $comparison = $string->compareTo($valueToCompare);
* echo $comparsion; // -1 (which is less than)
* echo $string->compareTo("adam"); // 0 (which is equal)
* echo $string->compareTo(str("abba")); 1 (which is greater than)
* </pre>
*
* @param string|Midori_String $value The value that string is being compared to.
* @param boolean $ignoreCase <strong>[optional false]</strong> Instructs the
* the compre to be case insensitive or not.
* @see strcmp()
* @see strcasecmp()
* @return integer 0 if its equal, -1 if the current value is less than, 1 if its greater than.
*/
public function compareTo($value, $ignoreCase = false)
{
$value = $this->val($value);
 
if($ignoreCase){
$compare = strcasecmp($this->value, $value);
if($compare > 0)
return 1;
if($compare === 0)
return 0;
return -1;
}
else
return strcmp($this->value, $value);
}
 
 
 
 
/**
* Joins the current string value with the value that
* is being passed to this method.
*
* <p>
* This method takes the current string and joins the string
* that is being passed to the this method to create a new
* string of both values.
* </p>
*
* <pre class="brush: php">
* $array = array("I have", " a enumerable object ", "of strings");
* $start = new Midori_String("notice:");
* $container = $start->concat(" this is a text. ");
* foreach($array as $value)
* $container = $container->concat($value);
* echo $container; // notice: this is a test. I have a enumerable object of strings
* echo $start; // notice:
* </pre>
*
* @param mixed $value The value that is to be appended to the new copy
* of the string that is returned.
* @return Midori_String the concatinated string of both values.
*/
public function concat($value)
{
return $this->str($this->value .$value);
}
 
/**
* Determines if the current string contains the value.
*
* <p></p>
* <pre class="brush: php">
* $sentence = str("Till the roof comes off, Till the lights go out");
* if($sentence->contains("off"))
* echo "the lights have been turned off";
* </pre>
*
* @exception Midori_ArgumentNullException
* @param string|Midori_String $value The search value.
* @param boolean $ignoreCase <strong>[optional false]</strong> Instructs the
* search to be case insenstive or not.
* @return boolean returns true if the string contains the value.
*/
public function contains($value, $ignoreCase = false)
{
if($value instanceof Midori_String)
$value = $value->value;
 
if($value == null)
throw new Midori_ArgumentNullException("value");
 
if($ignoreCase)
return (stripos($this->value, $value) !== false);
return (strpos($this->value, $value) !== false);
}
 
/**
* Determines if the current string ends with a certain value.
*
* <p>
* This method compares the value to the current string and must match
* the full string or the specified ending of the string.
* </p>
*
* <pre class="brush: php">
* $sentence = str("My MySpace page is all totally pimped out");
* if($sentence->endsWith("out"))
* echo "pimped";
* </pre>
*
* @exception Midori_ArgumentNullException
* @param string|Midori_String $value The search value of the expected end of the string.
* @param boolean $ignoreCase <strong>[optional false]</strong> Instructs the
* search to be case insensitive or not.
* @return boolean Returns true if the string ends with the specified value.
*/
public function endsWith($value, $ignoreCase = false)
{
if($value instanceof Midori_String)
$value = $value->value;
 
if($value == null)
throw new Midori_ArgumentNullException("value");
 
if($this->value == $value)
return true;
 
$flags = $ignoreCase ? "i" : "";
return (boolean)preg_match("/{$value}$/{$flags}", $this->value);
}
 
/**
* Overridden. Determines if the strings have the same value
*
* <p>
* Equals is overriden to do a string comparion of the 2 values
* to see if they are equal, it also takes a second parameter to
* see if comparison needs to be case insensitive.
* </p>
*
* <pre class="brush: php">
* $sentence = str("My MySpace page is all totally pimped out");
* if(!$sentence->equals("big pimping spending gs"))
* echo "ahh homie we're out of luck, lets go find some bling";
* </pre>
*
* @param mixed $value The value that is being compared to the current string.
* @param boolean $ignoreCase <strong>[optional false]</strong> Instructs the
* the method to be case insensitive or not.
* @return boolean
*/
public function equals($value, $ignoreCase = false)
{
$value = $this->val($value);
 
if($ignoreCase)
return $this->compareTo($value, true) === 0;
return $value == $this->value;
}
 
/**
* Formats the string and Replaces the place holders in the
* string with the arguments that are passed in.
*
* <p>Using {@link sprintf() sprintf} internally, the format method replaces
* the values in the current string passes back a copy of the
* formatted string with values replaced.</p>
*
* <pre class="brush: php">
* $noun = "man";
* $comedicAccent = "yipe";
* $value = str('I am %2$s, hear me roar, %1$s!')
* ->format($comedicAccent,$noun);
* echo $value; // I am man! hear me roar, yipe!
* </pre>
*
* @see sprintf()
* @return Midori_String a formatted copy of the string with the replaced values.
*/
public function format()
{
$temp = func_get_args();
$args = array($this->value);
foreach($temp as $value)
$args[] = $value;
return $this->str(call_user_func_array("sprintf", $args));
}
 
/**
* Determines the index value of the string that is being
* searched for.
*
* <p>
* Searches the string for a specified value and returns the index or returns
* a -1 if the index is not found.
* </p>
*
* <pre class="brush: php">
* $line = str("I'll be back");
* echo $line->indexOf("back"); // 8
* echo $line->indexOf("i'll", true); // 0
* echo $line->indexOf("i'll"); // -1
* </pre>
*
* @param string|Midori_String $value The search value to find in the string.
* @param boolean $ignoreCase <strong>[optional false]</strong> Instructs the
* search to be case insensitive or not.
* @param integer $startPosition <strong>[optional 0]</strong> The index of the
* position to start searching in the string.
* @param integer $limit <strong>[optional null]</strong> The amount
* of characters to search after the start position.
* @return integer
*/
public function indexOf($value, $ignoreCase= false, $startPosition = 0, $limit = null)
{
$value = $this->val($value);
$search = $limit ? substr($this->value, 0, $startPosition + $limit) : $this->value;
$result = -1;
if($ignoreCase)
$result = stripos($search, $value, $startPosition);
else
$result = strpos($search, $value, $startPosition);
return is_int($result) ? $result : -1;
}
 
/**
* Inserts, injects a string value into the current string.
*
* <p>
* Using the index as the place to determine where to inject, this
* method will break a part a string into parts, and then concatinate
* all three parts together.
* </p>
*
* <pre class="brush: php">
* $line = str("Insert funny here !");
* $index = $line->indexOf("!");
* echo $line->insert($index, "TTLY MY BFF LOL "); //"Insert funny here TTLY MY BFF LOL !"
* </pre>
*
* @param string|Midori_String $value The value to be inserted into the string.
* @param integer $startIndex The starting position/index to inject
* the value into the string.
* @return Midori_String returns a copy of the string with the inserted value.
*/
public function insert($startIndex, $value)
{
$before = substr($this->value, 0, $startIndex);
$after = substr($this->value, $startIndex);
return $this->str($before.$this->val($value).$after);
}
 
/**
* Gets if the string is empty.
*
* @ignore
* @return boolean returns true if the string is empty.
*/
protected function getEmpty()
{
return ($this->length == 0);
}
 
 
/**
* Gets if the string is null or empty.
*
* @ignore
* @return boolean returns true if the string is null or empty.
*/
protected function getIsNullOrEmpty()
{
return ($this->value == null || $this->length == 0);
}
 
/**
* gets the length of the string.
*
* @ignore
* @return integer the length of the string
*/
protected function getLength()
{
return strlen($this->value);
}
 
 
public function gsub($pattern, $replacement, $limit = null, $count = null)
{
return regex($pattern)->replace($this->value, $replacement, $limit, $count);
}
 
/**
* Determines the last index of the specified value in the string.
*
*
*
*
*
* @param string|Midori_String $value The search value to find in the string.
* @param boolean $ignoreCase <strong>[optional false]</strong> Instructs the
* search to be case insensitive or not.
* @param integer $startPosition <strong>[optional 0]</strong> The index of the
* position to start searching in the string.
* @param integer $limit <strong>[optional null]</strong> The amount
* of characters to search after the start position.
* @return index the position of value or -1 if the value is not found.
*/
public function lastIndexOf($value, $ignoreCase= false, $startPosition = 0, $limit = null)
{
$value = $this->val($value);
$search = $limit ? substr($this->value, 0, $startPosition + $limit) : $this->value;
 
if($ignoreCase)
$result = strripos($this->value, $value, $startPosition);
else
$result = strrpos($this->value, $value, $startPosition);
return is_int($result) ? $result : -1;
}
 
/**
*
* Enter description here...
* @param $pattern
* @param $startPosition
* @param $flags
* @return unknown_type
*/
public function match($pattern, $startPosition = 0, $flags = null)
{
return regex($pattern)->match($this->value, $startPosition, $flags);
}
 
/**
*
* @ignore
* @param $offset
* @return unknown_type
*/
public function offsetExists($index)
{
if($index == null)
throw new Midori_ArgumentNullException("index");
if(is_int($index))
return isset($this->value[$index]);
if(is_string($index))
return $this->contains($index);
if($index instanceof Midori_Regex)
return $index->isMatch($this->value);
 
return false;
}
 
public function offsetGet($index)
{
if($index == null)
throw new Midori_ArgumentNullException("index");
if(is_int($index))
return $this->str($this->value[$index]);
if(is_string($index) && $this->contains($index))
return $this->str($index);
if($index instanceof Midori_Regex)
return $this->str($index->match($index));
return null;
}
 
public function offsetSet($index, $value)
{
$value = $this->val($value);
 
if(is_int($index))
{
$before = substr($this->value, 0, $index);
$after = substr($this->value, $index-1);
$this->value = $before.$value.$after;
}
 
if(is_string($index))
$this->value = str_replace($index, $value, $this->value);
 
if($index instanceof Midori_Regex)
$this->value = $this->val($index->replace($this->value, $value));
}
 
public function offsetUnset($index)
{
if(is_int($index))
{
$before = substr($this->value, 0, $index);
$after = substr($this->value, $index-1);
$this->value = $before.$after;
}
if(is_string($index))
$this->value = str_replace($index, "", $this->value);
 
if($index instanceof Midori_Regex)
$this->value = $this->val($index->replace($this->value, ""));
}
 
public function padLeft($totalWidth, $paddingChars = " ")
{
return $this->str($this->value, $totalWidth, $paddingChars, STR_PAD_LEFT);
}
 
public function padRight($totalWidth, $paddingChars = " ")
{
return $this->str($this->value, $totalWidth, $paddingChars, STR_PAD_RIGHT);
}
 
 
public function replace($search, $replacement = "", $limit = null, $count = null)
{
return $this->str(str_replace($this->val($search), $this->val($replacement), $this->value, $limit));
}
 
/**
* Trims whitespace or specified characters
* from the outer parts of the string.
*
* @param string $chars optional.
* @return Midori_String
*/
public function trim($chars = ' ')
{
return $this->str(trim($this->value, $chars));
}
 
/**
* Trims whitespace or specified characters from
* the end (right) of the string.
*
* @param string $chars optional
* @return Midori_String
*/
public function trimEnd($chars = ' ')
{
return $this->str(rtrim($this->value, $chars));
}
 
/**
* Trims whitespace or specified characters from
* the start (left) of the string.
*
* @param string $chars optional
* @return Midori_String
*/
public function trimStart($chars = ' ')
{
return $this->str(ltrim($this->value, $chars));
}
 
 
/**
* creates a new Midori_String to wrap the value.
*
* <p> {@link http://us3.php.net/manual/en/language.oop5.magic.php#language.oop5.magic.invoke __invoke} is
* magically called when you try to use this class like a function.</p>
*
* <pre class="brush: php">
* $str = new Midori_String();
* $x = $str("my cool string value");
* $y = $str("i miss ruby");
*
* echo $x; // my cool string value
* echo $y; // i miss ruby
* </pre>
*
* @param string $value The value that is to be wrapped into an {@see Midori_String}
* @return Midori_String creates string object wrapped around the value.
*/
public function __invoke($value)
{
return $this->str($value);
}
 
 
}
 
/**
* creates a {@see Midori_String} from a string value
*
* unfortunately Eclipse PDT does not regconigize __invoke()
* on a variable yet, so this function gives a quick way
* to chain a string type.
*
* @return Midori_String the string value is wrapped.
*/
function str($value){
return new Midori_String($value);
}
 
$str = "str";
 
 
 
 
Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server