public static bool Ceq(string sx, PhpValue y) { switch (y.TypeCode) { case PhpTypeCode.Boolean: return(Convert.ToBoolean(sx) == y.Boolean); case PhpTypeCode.Long: return(Compare(sx, y.Long) == 0); case PhpTypeCode.Double: return(Compare(sx, y.Double) == 0); case PhpTypeCode.PhpArray: return(false); case PhpTypeCode.String: return(Ceq(sx, y.String)); case PhpTypeCode.MutableString: return(Ceq(sx, y.MutableString.ToString())); case PhpTypeCode.Object: return(CompareStringToObject(sx, y.Object) == 0); case PhpTypeCode.Alias: return(Ceq(sx, y.Alias.Value)); case PhpTypeCode.Null: return(sx.Length == 0); } throw InvalidTypeCodeException("ceq", PhpVariable.TypeNameString, PhpVariable.GetDebugType(y)); }
public static int Compare(PhpString.Blob sx, PhpValue y) { switch (y.TypeCode) { case PhpTypeCode.Null: return((sx.Length == 0) ? 0 : 1); case PhpTypeCode.Boolean: return(Compare(sx.ToBoolean(), y.Boolean)); case PhpTypeCode.Long: return(Compare(sx.ToString(), y.Long)); case PhpTypeCode.Double: return(Compare(sx.ToString(), y.Double)); case PhpTypeCode.PhpArray: return(-1); // - 1 * (array.CompareTo(string)) case PhpTypeCode.String: return(Compare(sx.ToString(), y.String)); case PhpTypeCode.MutableString: return(Compare(sx, y.MutableStringBlob)); case PhpTypeCode.Object: return(CompareStringToObject(sx.ToString(), y.Object)); case PhpTypeCode.Alias: return(Compare(sx, y.Alias.Value)); } throw InvalidTypeCodeException("compare", PhpVariable.TypeNameString, PhpVariable.GetDebugType(y)); }
public static int Compare(double dx, PhpValue y) { switch (y.TypeCode) { case PhpTypeCode.Null: return((dx == 0.0) ? 0 : 1); case PhpTypeCode.Boolean: return(Compare(dx != 0.0, y.Boolean)); case PhpTypeCode.Long: return(Compare(dx, (double)y.Long)); case PhpTypeCode.Double: return(Compare(dx, y.Double)); case PhpTypeCode.PhpArray: return(-1); case PhpTypeCode.String: return(-Compare(y.String, dx)); case PhpTypeCode.MutableString: return(-Compare(y.MutableString.ToString(), dx)); case PhpTypeCode.Object: // we cannot use ToNumber(object) because it treats some builtin classes as numbers if (y.Object is decimal yd) { return(Compare(dx, (double)yd)); } // Notice: Object of class {0} could not be converted to int PhpException.Throw(PhpError.Notice, string.Format(Resources.ErrResources.object_could_not_be_converted, PhpVariable.GetDebugType(y), PhpVariable.TypeNameDouble)); return(Compare(dx, 1.0)); // object is treated as '1' case PhpTypeCode.Alias: return(Compare(dx, y.Alias.Value)); } throw InvalidTypeCodeException("compare", PhpVariable.TypeNameDouble, PhpVariable.GetDebugType(y)); }
/// <summary> /// Gets <see cref="PhpTypeInfo"/> from a string or an object instance. /// </summary> /// <param name="ctx">Current runtime context.</param> /// <param name="object">String or object. Other value types cause an exception.</param> /// <returns>Corresponding <see cref="PhpTypeInfo"/> descriptor. Cannot be <c>null</c>.</returns> public static PhpTypeInfo TypeNameOrObjectToType(Context ctx, PhpValue @object) { object obj; string str; if ((obj = (@object.AsObject())) != null) { return(obj.GetType().GetPhpTypeInfo()); } else if ((str = PhpVariable.AsString(@object)) != null) { return(ctx.GetDeclaredType(str, true)); } else { throw new ArgumentException(); } }
/// <summary> /// Reports an error when a variable should be PHP object but it is not. /// </summary> /// <param name="reference">Whether a reference modifier (=&) is used.</param> /// <param name="var">The variable which was misused.</param> /// <exception cref="PhpException"><paramref name="var"/> is <see cref="PhpArray"/> (Warning).</exception> /// <exception cref="PhpException"><paramref name="var"/> is scalar type (Warning).</exception> /// <exception cref="PhpException"><paramref name="var"/> is a string (Warning).</exception> public static void VariableMisusedAsObject(PhpValue var, bool reference) { if (var.IsEmpty) { Throw(PhpError.Notice, ErrResources.empty_used_as_object); } else if (var.IsArray) { Throw(PhpError.Warning, ErrResources.array_used_as_object); } else if (var.TypeCode == PhpTypeCode.String || var.TypeCode == PhpTypeCode.MutableString) { Throw(PhpError.Warning, reference ? ErrResources.string_item_used_as_reference : ErrResources.string_used_as_object); } else if (var.IsAlias) { VariableMisusedAsObject(var.Alias.Value, reference); } else { Throw(PhpError.Warning, ErrResources.scalar_used_as_object, PhpVariable.GetTypeName(var)); } }
public static int Compare(double dx, PhpValue y) { switch (y.TypeCode) { case PhpTypeCode.Double: return(Compare(dx, y.Double)); case PhpTypeCode.Long: return(Compare(dx, (double)y.Long)); case PhpTypeCode.Boolean: return(Compare(dx != 0.0, y.Boolean)); case PhpTypeCode.String: return(-Compare(y.String, dx)); case PhpTypeCode.MutableString: return(-Compare(y.MutableString.ToString(), dx)); case PhpTypeCode.PhpArray: return(-1); case PhpTypeCode.Alias: return(Compare(dx, y.Alias.Value)); case PhpTypeCode.Null: return((dx == 0.0) ? 0 : 1); case PhpTypeCode.Object: if (y.Object == null) { goto case PhpTypeCode.Null; } // Notice: Object of class {0} could not be converted to int PhpException.Throw(PhpError.Notice, string.Format(Resources.ErrResources.object_could_not_be_converted, PhpVariable.GetTypeName(y), PhpVariable.TypeNameDouble)); return(Compare(dx, 1.0)); // object is treated as '1' } throw new NotImplementedException($"compare(Double, {y.TypeCode})"); }
public static int Compare(long lx, PhpValue y) { switch (y.TypeCode) { case PhpTypeCode.Long: return(Compare(lx, y.Long)); case PhpTypeCode.Boolean: return(Compare(lx != 0, y.Boolean)); case PhpTypeCode.Double: return(Compare((double)lx, y.Double)); case PhpTypeCode.String: return(-Compare(y.String, lx)); case PhpTypeCode.MutableString: return(-Compare(y.MutableString.ToString(), lx)); case PhpTypeCode.PhpArray: return(-1); case PhpTypeCode.Alias: return(Compare(lx, y.Alias.Value)); case PhpTypeCode.Null: return((lx == 0) ? 0 : 1); case PhpTypeCode.Object: Debug.Assert(y.Object != null); // Notice: Object of class {0} could not be converted to int PhpException.Throw(PhpError.Notice, string.Format(Resources.ErrResources.object_could_not_be_converted, PhpVariable.GetTypeName(y), PhpVariable.TypeNameInt)); return(Compare(lx, 1L)); // object is treated as '1' } throw new NotImplementedException($"compare(Long, {y.TypeCode})"); }
public static void ThrowIfArgumentNotCallable(Context ctx, RuntimeTypeHandle callerCtx, PhpValue value, bool nullAllowed, int arg) { if (nullAllowed && value.IsNull) { return; } var callable = value.AsCallable(callerCtx); if (callable == null || (callable is PhpCallback phpcallback && !phpcallback.IsValidBound(ctx))) { throw TypeErrorException(string.Format(ErrResources.argument_not_callable, arg, PhpVariable.GetTypeName(value))); } }
public static int Compare(object x, PhpValue y) { Debug.Assert(x != null); if (x.Equals(y.Object)) { return(0); } if (x is IPhpComparable) { return(((IPhpComparable)x).Compare(y)); } if (y.Object is IPhpComparable) { return(-((IPhpComparable)y.Object).Compare(PhpValue.FromClass(x))); } if (x is decimal xd) { return(Compare((double)xd, y)); } switch (y.TypeCode) { case PhpTypeCode.Null: return(1); case PhpTypeCode.Boolean: return(y.Boolean ? 0 : 1); case PhpTypeCode.Long: // Notice: Object of class {0} could not be converted to int PhpException.Throw(PhpError.Notice, string.Format(Resources.ErrResources.object_could_not_be_converted, PhpVariable.GetDebugType(y), PhpVariable.TypeNameInt)); return(Compare(1L, y.Long)); case PhpTypeCode.Double: // Notice: Object of class {0} could not be converted to float PhpException.Throw(PhpError.Notice, string.Format(Resources.ErrResources.object_could_not_be_converted, PhpVariable.GetDebugType(y), PhpVariable.TypeNameFloat)); return(Compare(1L, y.Long)); case PhpTypeCode.String: return(-CompareStringToObject(y.String, x)); case PhpTypeCode.Object: Debug.Assert(y.Object != null); var result = CompareObjects(x, y.Object, PhpComparer.Default, out var incomparable); if (incomparable) { PhpException.Throw(PhpError.Warning, Resources.ErrResources.incomparable_objects_compared_exception, x.GetPhpTypeInfo().Name, y.Object.GetPhpTypeInfo().Name); return(1); } return(result); case PhpTypeCode.Alias: return(Compare(x, y.Alias.Value)); default: PhpException.Throw(PhpError.Notice, string.Format(Resources.ErrResources.incomparable_objects_compared_exception, x.GetPhpTypeInfo().Name, PhpVariable.GetDebugType(y))); return(0); } }