File modules/dsl/standard/StringInstance.class.php

Last commit: Tue Jul 19 00:10:00 2022 +0200	Jan Dankert	New: Scripting language: Ignore Keyword "new"; Support for Calling object constructors; Splitting objects into an instance and a wrapper.
1 <?php 2 3 4 namespace dsl\standard; 5 6 7 use dsl\context\BaseScriptableObject; 8 9 class StringInstance extends BaseScriptableObject 10 { 11 private $value; 12 public $length; 13 public $test = 1; 14 15 /** 16 * Number constructor. 17 * @param $value 18 */ 19 public function __construct($value=null) 20 { 21 if ( $value instanceof BaseScriptableObject ) 22 $this->value = $value->__toString(); 23 24 elseif ( is_object( $value ) ) 25 $this->value = get_class($value); 26 27 elseif ( is_callable( $value ) ) 28 $this->value = 'function'; 29 30 elseif ( is_bool( $value ) ) 31 $this->value = $value ? 'true' : 'false'; 32 else 33 $this->value = (string) $value; 34 35 $this->length = strlen( $this->value ); 36 } 37 38 public function __toString() 39 { 40 return (string)$this->value; 41 } 42 43 44 public function __invoke( $value ) 45 { 46 return new StringInstance( $value ); 47 } 48 49 50 public function split( $splitChar = null ) { 51 if ( $splitChar == '' ) 52 return str_split( $this->value ); 53 if ( $splitChar == null ) 54 return array( $this->value ); 55 56 return explode( $splitChar, $this->value ); 57 } 58 59 60 public function trim() { 61 return trim( $this->value); 62 } 63 public function trimEnd() { 64 return rtrim( $this->value); 65 66 } 67 public function trimStart() { 68 return ltrim( $this->value); 69 } 70 71 public function valueOf( $val ) { 72 return new StringInstance( $val ); 73 } 74 75 public function charAt( $pos ) { 76 return substr( $this->value,$pos,1 ); 77 } 78 79 public function concat( $str ) { 80 return $this->value.$str; 81 } 82 83 public function startsWith($str) { 84 return substr( $this->value, 0,strlen($str) ); 85 } 86 87 public function endsWith($str ) { 88 return substr( $this->value,strlen($str)*-1,strlen($str)) == $str; 89 } 90 91 public function indexOf( $search ) { 92 return strpos( $this->value, $search ); 93 } 94 95 public function lastIndexOf( $search) { 96 return strrpos( $this->value, $search ); 97 } 98 99 public function padStart($length,$pad=null) { 100 $this->value = str_pad( $this->value,$length, STR_PAD_LEFT); 101 } 102 103 public function padEnd( $length,$pad=null) { 104 $this->value = str_pad( $this->value,$length, STR_PAD_RIGHT); 105 } 106 107 public function repeat( $count) { 108 return str_repeat( $this->value,$count); 109 } 110 111 public function replace( $search,$replaceWith ) { 112 $c = 1; 113 return str_replace( $search,$replaceWith,$this->value,$c ); 114 } 115 116 public function replaceAll( $search,$replaceWith ) { 117 return str_replace( $search,$replaceWith,$this->value ); 118 } 119 120 public function slice( $begin,$end=null) { 121 return array_slice( $this->value,$begin,$end-$begin); 122 } 123 124 public function substring( $start,$end) { 125 return substr( $this->value, $start,$end-$start+1 ); 126 } 127 128 public function toLowerCase() { 129 return strtolower($this->value); 130 } 131 132 public function toUpperCase() { 133 return strtoupper($this->value); 134 } 135 136 // public function length() { 137 // return strlen($this->value); 138 // } 139 140 }
Download modules/dsl/standard/StringInstance.class.php
History Tue, 19 Jul 2022 00:10:00 +0200 Jan Dankert New: Scripting language: Ignore Keyword "new"; Support for Calling object constructors; Splitting objects into an instance and a wrapper.