1616use ReflectionFunctionAbstract ;
1717use ReflectionMethod ;
1818use RuntimeException ;
19+ use Toolkit \Stdlib \Helper \PhpHelper ;
1920use Traversable ;
2021use function base64_decode ;
2122use function base64_encode ;
23+ use function basename ;
24+ use function dirname ;
25+ use function gettype ;
2226use function gzcompress ;
2327use function gzuncompress ;
2428use function is_array ;
3236use function serialize ;
3337use function spl_object_hash ;
3438use function sprintf ;
39+ use function str_replace ;
40+ use function strpos ;
3541use function ucfirst ;
3642use function unserialize ;
3743
@@ -49,7 +55,7 @@ class ObjectHelper
4955 * - 会先尝试用 setter 方法设置属性
5056 * - 再尝试直接设置属性
5157 *
52- * @param mixed $object An object instance
58+ * @param object| mixed $object An object instance
5359 * @param array $options
5460 *
5561 * @return mixed
@@ -92,14 +98,30 @@ public static function configure($object, array $options): void
9298 /**
9399 * 给对象设置属性值
94100 *
95- * @param $object
101+ * @param object|mixed $object
96102 * @param array $options
97103 */
98104 public static function setAttrs ($ object , array $ options ): void
99105 {
100106 self ::configure ($ object , $ options );
101107 }
102108
109+ /**
110+ * @param object|mixed $object
111+ * @param array $data
112+ *
113+ * @throws ReflectionException
114+ */
115+ public static function mappingProps ($ object , array $ data ): void
116+ {
117+ // TODO
118+ $ rftObj = PhpHelper::reflectClass ($ object );
119+ foreach ($ rftObj ->getProperties () as $ rftProp ) {
120+ // TODO
121+ // $typeName = $rftProp->getType()
122+ }
123+ }
124+
103125 /**
104126 * 定义一个用来序列化数据的函数
105127 *
@@ -160,7 +182,7 @@ public static function toArray($data, bool $recursive = false)
160182 }
161183
162184 /**
163- * @param object $object
185+ * @param object|mixed $object
164186 *
165187 * @return bool
166188 */
@@ -171,11 +193,11 @@ public static function isArrayable($object): bool
171193
172194 /**
173195 * @param mixed $object
174- * @param bool $unique
196+ * @param bool $unique
175197 *
176198 * @return string
177199 */
178- public static function hash ($ object , $ unique = true ): string
200+ public static function hash ($ object , bool $ unique = true ): string
179201 {
180202 if (is_object ($ object )) {
181203 $ hash = spl_object_hash ($ object );
@@ -192,8 +214,7 @@ public static function hash($object, $unique = true): string
192214 }
193215
194216 /**
195- * @from https://github.com/ventoviro/windwalker
196- * Build an array of constructor parameters.
217+ * Build an array of class method parameters.
197218 *
198219 * @param ReflectionMethod $method Method for which to build the argument array.
199220 * @param array $provideArgs Manual provide params map.
@@ -245,6 +266,8 @@ public static function getMethodArgs(ReflectionMethod $method, array $provideArg
245266 }
246267
247268 /**
269+ * Build an array of class method parameters.
270+ *
248271 * @param ReflectionFunctionAbstract $rftFunc
249272 * @param array $provideArgs
250273 *
@@ -264,9 +287,9 @@ public static function buildReflectCallArgs(ReflectionFunctionAbstract $rftFunc,
264287 continue ;
265288 }
266289
267- // filling by param name
290+ // filling by param name and type is same.
268291 $ name = $ param ->getName ();
269- if (isset ($ provideArgs [$ name ])) {
292+ if (isset ($ provideArgs [$ name ]) && $ typeName === gettype ( $ provideArgs [ $ name ]) ) {
270293 $ funcArgs [] = $ provideArgs [$ name ];
271294 continue ;
272295 }
@@ -325,6 +348,7 @@ public static function create(string $class)
325348 */
326349 public static function createByArray ($ config )
327350 {
351+ // is class name.
328352 if (is_string ($ config )) {
329353 return new $ config ;
330354 }
@@ -341,4 +365,34 @@ public static function createByArray($config)
341365
342366 return null ;
343367 }
368+
369+
370+ /**
371+ * Get class namespace
372+ *
373+ * @param string $fullClass
374+ *
375+ * @return string
376+ */
377+ public static function spaceName (string $ fullClass ): string
378+ {
379+ $ fullClass = str_replace ('\\' , '/ ' , $ fullClass );
380+
381+ return strpos ($ fullClass , '/ ' ) ? dirname ($ fullClass ) : '' ;
382+ }
383+
384+ /**
385+ * Get class name without namespace.
386+ *
387+ * @param null|string $fullClass
388+ *
389+ * @return string
390+ */
391+ public static function className (string $ fullClass ): string
392+ {
393+ $ fullClass = str_replace ('\\' , '/ ' , $ fullClass );
394+
395+ return basename ($ fullClass );
396+ }
397+
344398}
0 commit comments