Beispiel #1
0
 /// <summary>
 /// Creates <see cref="PhpArray"/> from object's properties.
 /// </summary>
 /// <param name="obj">Object instance.</param>
 /// <returns>Array containing given object properties keyed according to PHP specifications.</returns>
 public static PhpArray ClassToArray(object obj)
 {
     if (object.ReferenceEquals(obj, null))
     {
         return(PhpArray.NewEmpty());
     }
     else if (obj.GetType() == typeof(stdClass))
     {
         // special case,
         // object is stdClass, we can simply copy its runtime fields
         var runtime_fields = ((stdClass)obj).GetRuntimeFields();
         return((runtime_fields != null) ? runtime_fields.DeepCopy() : PhpArray.NewEmpty());
     }
     else
     {
         if (obj is IPhpConvertible conv)
         {
             return(ToArray(conv));
         }
         else if (obj is Array)
         {
             // [] -> array
             return(new PhpArray((Array)obj));
         }
         else if (obj is System.Collections.IEnumerable)
         {
             // the same behavior as foreach for CLR enumerators
             return(PhpArray.Create(Operators.GetForeachEnumerator((System.Collections.IEnumerable)obj)));
         }
         else
         {
             // obj -> array
             var arr = new PhpArray();
             TypeMembersUtils.InstanceFieldsToPhpArray(obj, arr);
             return(arr);
         }
     }
 }