Overview

Namespaces

  • PHP
  • Saklient
    • Cloud
      • Enums
      • Errors
      • Models
      • Resources
    • Errors

Classes

  • Util
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: 
  3: namespace Saklient;
  4: 
  5: require_once dirname(__FILE__) . "/Errors/SaklientException.php";
  6: use \Saklient\Errors\SaklientException;
  7: require_once dirname(__FILE__) . "/Cloud/Client.php";
  8: use \Saklient\Cloud\Client;
  9: 
 10: class Util {
 11:     
 12:     /**
 13:      * @access public
 14:      * @param mixed $obj
 15:      * @param string $path
 16:      * @return mixed
 17:      */
 18:     static public function existsPath($obj, $path)
 19:     {
 20:         $aPath = explode(".", $path);
 21:         foreach ($aPath as $seg) {
 22:             if ($obj == null) {
 23:                 return false;
 24:             }
 25:             if (!is_object($obj) && !is_array($obj)) {
 26:                 return false;
 27:             }
 28:             if ($seg == "") {
 29:                 continue;
 30:             }
 31:             if (is_array($obj) || $obj instanceof \ArrayObject) {
 32:                 $obj = (array)$obj;
 33:                 if (!array_key_exists($seg, $obj)) {
 34:                     return false;
 35:                 }
 36:                 $obj = @$obj[$seg];
 37:             }
 38:             else {
 39:                 if (!property_exists($obj, $seg)) {
 40:                     return false;
 41:                 }
 42:                 $obj = @$obj->{$seg};
 43:             }
 44:         }
 45:         return true;
 46:     }
 47:     
 48:     /**
 49:      * @access public
 50:      * @param mixed $obj
 51:      * @param string $path
 52:      * @return mixed
 53:      */
 54:     static public function getByPath($obj, $path)
 55:     {
 56:         $aPath = explode(".", $path);
 57:         foreach ($aPath as $seg) {
 58:             if ($obj == null) {
 59:                 return null;
 60:             }
 61:             if (!is_object($obj) && !is_array($obj)) {
 62:                 return null;
 63:             }
 64:             if ($seg == "") {
 65:                 continue;
 66:             }
 67:             if (is_array($obj) || $obj instanceof \ArrayObject) {
 68:                 $obj = (array)$obj;
 69:                 if (!array_key_exists($seg, $obj)) {
 70:                     return null;
 71:                 }
 72:                 $obj = @$obj[$seg];
 73:             }
 74:             else {
 75:                 $obj = @$obj->{$seg};
 76:             }
 77:         }
 78:         return $obj;
 79:     }
 80:     
 81:     static public function getByPathAny($objects, $pathes) {
 82:         foreach ($objects as $obj) {
 83:             foreach ($pathes as $path) {
 84:                 $ret = static::getByPath($obj, $path);
 85:                 if ($ret != null) return $ret;
 86:             }
 87:         }
 88:         return null;
 89:     }
 90:     
 91:     
 92:     /**
 93:      * @todo array support
 94:      * @todo overwriting
 95:      * @todo writing into children of non-object
 96:      * @access public
 97:      * @param mixed $obj
 98:      * @param mixed $value
 99:      * @param string $path
100:      * @return void
101:      */
102:     static public function setByPath($obj, $path, $value)
103:     {
104:         $aPath = explode(".", $path);
105:         $key = array_pop($aPath);
106:         foreach ($aPath as $seg) {
107:             if ($seg == "") {
108:                 continue;
109:             }
110:             if (!array_key_exists($seg, $obj)) {
111:                 $obj->{$seg} = (object)[];
112:             }
113:             $obj = $obj->{$seg};
114:         }
115:         $obj->{$key} = $value;
116:     }
117:     
118:     /**
119:      * @access public
120:      * @param string $classPath
121:      * @param \ArrayObject $args
122:      * @return mixed
123:      */
124:     static public function createClassInstance($classPath, $args)
125:     {
126:         $ret = null;
127:         $classPath = implode('\\', array_map(function($x){return strtoupper(substr($x,0,1)).substr($x,1);}, explode('.', $classPath)));
128:         $ref = new \ReflectionClass($classPath);
129:         $ret = $ref->newInstanceArgs(Client::arrayObject2array($args));
130:         if ($ret == null) {
131:             throw new \Exception("Could not create class instance of " . $classPath);
132:         }
133:         return $ret;
134:     }
135:     
136:     /**
137:      * @access public
138:      * @param string $s
139:      * @return NativeDate
140:      */
141:     static public function str2date($s)
142:     {
143:         if ($s == null) {
144:             return null;
145:         }
146:         return new \DateTime($s);
147:     }
148:     
149:     /**
150:      * @access public
151:      * @param NativeDate $d
152:      * @return string
153:      */
154:     static public function date2str(NativeDate $d)
155:     {
156:         if ($d == null) {
157:             return null;
158:         }
159:         return $d->format("c");
160:     }
161:     
162:     /**
163:      * @access public
164:      * @param string $s
165:      * @return string
166:      */
167:     static public function urlEncode($s)
168:     {
169:         return rawurlencode($s);
170:     }
171:     
172:     /**
173:      * @access public
174:      * @param ArrayObject $a
175:      * @return ArrayObject
176:      */
177:     static public function sortArray(\ArrayObject $aobj)
178:     {
179:         $ret = (array)$aobj;
180:         sort($ret);
181:         return new \ArrayObject($ret);
182:     }
183:     
184:     /**
185:      * @access public
186:      * @param int $sec
187:      * @return void
188:      */
189:     static public function sleep($sec)
190:     {
191:         \sleep($sec);
192:     }
193:     
194:     static public function popAobj(\ArrayObject &$aobj)
195:     {
196:         $arr = (array)$aobj;
197:         $ret = array_pop($arr);
198:         $aobj->exchangeArray($arr);
199:         return $ret;
200:     }
201:     
202:     static public function shiftAobj(\ArrayObject &$aobj)
203:     {
204:         $arr = (array)$aobj;
205:         $ret = array_shift($arr);
206:         $aobj->exchangeArray($arr);
207:         return $ret;
208:     }
209:     
210:     /**
211:      * @access public
212:      * @param int $actual
213:      * @param int $expected
214:      * @return void
215:      */
216:     static public function validateArgCount($actual, $expected) {
217:         if ($actual < $expected) throw new SaklientException('argument_count_mismatch', 'Argument count mismatch');
218:     }
219:     
220:     /**
221:      * @access public
222:      * @param mixed $value
223:      * @param string $typeName
224:      * @return void
225:      */
226:     static public function validateType($value, $typeName, $force=false)
227:     {
228:         $isOk = false;
229:         if (!$force) {
230:             if ($typeName=="mixed" || $typeName=="void" || is_null($value)) return;
231:             switch ($typeName) {
232:                 case 'float':
233:                 case 'double':
234:                 case 'int':
235:                     $isOk = is_numeric($value);
236:                     break;
237:                 case 'string':
238:                     $isOk = is_scalar($value);
239:                     break;
240:                 case 'callback':
241:                     $isOk = is_callable($value);
242:                     break;
243:                 case 'array':
244:                 case '\\ArrayObject':
245:                     $isOk = is_array($value) || $value instanceof \ArrayObject;
246:                     break;
247:                 
248:                 //case 'boolean':
249:                 default:
250:                     $isOk = true; // already checked by PHP language
251:             }
252:         }
253:         if (!$isOk) throw new SaklientException('argument_type_mismatch', 'Argument type mismatch (expected '.$typeName.')');
254:     }
255:     
256:     
257:     
258: }
259: 
260: 
API documentation generated by ApiGen 2.8.0