1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ajax\service; |
4
|
|
|
|
5
|
|
|
class JReflection { |
6
|
|
|
public static function shortClassName($object){ |
7
|
|
|
$classNameWithNamespace = get_class($object); |
8
|
|
|
return substr($classNameWithNamespace, strrpos($classNameWithNamespace, '\\')+1); |
9
|
|
|
} |
10
|
|
|
|
11
|
|
|
public static function jsonObject($classname){ |
12
|
|
|
$object=new $classname(); |
13
|
|
|
$class = new \ReflectionClass($classname); |
14
|
|
|
$methods=$class->getMethods(\ReflectionMethod::IS_PUBLIC); |
15
|
|
|
foreach ($methods as $method){ |
16
|
|
|
$name=$method->getName(); |
17
|
|
|
if(JString::startswith($name, "set")){ |
18
|
|
|
$property=\lcfirst(JString::replaceAtFirst($name, "set", "")); |
19
|
|
|
$value="__".$property."__"; |
20
|
|
|
try{ |
21
|
|
|
if($class->getProperty($property)!==null){ |
22
|
|
|
\call_user_func_array([$object,$name],[$value]); |
23
|
|
|
} |
24
|
|
|
}catch(\Exception $e){ |
25
|
|
|
//Nothing to do |
26
|
|
|
} |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
return $object; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public static function callMethod($object,$callback,array $values){ |
33
|
|
|
return \call_user_func_array([$object,$callback],$values); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public static function getterName($propertyName,$prefix="get"){ |
37
|
|
|
return $prefix.\ucfirst($propertyName); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public static function callMethodFromAssociativeArray($object,$array,$methodPrefix="add"){ |
|
|
|
|
41
|
|
|
foreach ($array as $key=>$value){ |
42
|
|
|
if(\method_exists($object, "add".\ucfirst($key))){ |
43
|
|
|
\call_user_func([$object,"add".\ucfirst($key)],$value); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.