1+ using System ;
2+ using System . Linq ;
3+
4+ namespace Kontent . Ai . Delivery . Abstractions . QueryBuilders . Filtering ;
5+
6+ /// <summary>
7+ /// Represents a filter value that can be a single value, multiple values, a range, or empty.
8+ /// </summary>
9+ public abstract class FilterValue
10+ {
11+ /// <summary>
12+ /// Serializes this filter value to a string format suitable for the API.
13+ /// </summary>
14+ /// <returns>The serialized value string.</returns>
15+ public abstract string Serialize ( ) ;
16+
17+ /// <summary>
18+ /// Represents an empty filter value for operators that don't require values.
19+ /// </summary>
20+ public sealed class Empty : FilterValue
21+ {
22+ internal Empty ( ) { }
23+ public override string Serialize ( ) => "" ;
24+ }
25+
26+ /// <summary>
27+ /// Represents a single string value.
28+ /// </summary>
29+ public sealed class StringValue : FilterValue
30+ {
31+ public string Value { get ; }
32+ internal StringValue ( string value ) => Value = value ;
33+ public override string Serialize ( ) => System . Text . Encodings . Web . UrlEncoder . Default . Encode ( Value ) ;
34+ }
35+
36+ /// <summary>
37+ /// Represents a single integer value.
38+ /// </summary>
39+ public sealed class IntValue : FilterValue
40+ {
41+ public int Value { get ; }
42+ internal IntValue ( int value ) => Value = value ;
43+ public override string Serialize ( ) => Value . ToString ( ) ;
44+ }
45+
46+ /// <summary>
47+ /// Represents a single DateTime value.
48+ /// </summary>
49+ public sealed class DateTimeValue : FilterValue
50+ {
51+ public DateTime Value { get ; }
52+ internal DateTimeValue ( DateTime value ) => Value = value ;
53+ public override string Serialize ( ) => Value . ToString ( "yyyy-MM-ddTHH:mm:ssZ" ) ;
54+ }
55+
56+ /// <summary>
57+ /// Represents a single boolean value.
58+ /// </summary>
59+ public sealed class BooleanValue : FilterValue
60+ {
61+ public bool Value { get ; }
62+ internal BooleanValue ( bool value ) => Value = value ;
63+ public override string Serialize ( ) => Value . ToString ( ) . ToLowerInvariant ( ) ;
64+ }
65+
66+ /// <summary>
67+ /// Represents multiple string values.
68+ /// </summary>
69+ public sealed class StringArrayValue : FilterValue
70+ {
71+ public string [ ] Value { get ; }
72+ internal StringArrayValue ( string [ ] value ) => Value = value ;
73+ public override string Serialize ( ) => string . Join ( "," , Value . Select ( System . Text . Encodings . Web . UrlEncoder . Default . Encode ) ) ;
74+ }
75+
76+ /// <summary>
77+ /// Represents multiple integer values.
78+ /// </summary>
79+ public sealed class IntArrayValue : FilterValue
80+ {
81+ public int [ ] Value { get ; }
82+ internal IntArrayValue ( int [ ] value ) => Value = value ;
83+ public override string Serialize ( ) => string . Join ( "," , Value . Select ( v => v . ToString ( ) ) ) ;
84+ }
85+
86+ /// <summary>
87+ /// Represents multiple DateTime values.
88+ /// </summary>
89+ public sealed class DateTimeArrayValue : FilterValue
90+ {
91+ public DateTime [ ] Value { get ; }
92+ internal DateTimeArrayValue ( DateTime [ ] value ) => Value = value ;
93+ public override string Serialize ( ) => string . Join ( "," , Value . Select ( v => v . ToString ( "yyyy-MM-ddTHH:mm:ssZ" ) ) ) ;
94+ }
95+
96+ /// <summary>
97+ /// Represents a string range with lower and upper bounds.
98+ /// </summary>
99+ public sealed class StringRange : FilterValue
100+ {
101+ public string Lower { get ; }
102+ public string Upper { get ; }
103+ internal StringRange ( string lower , string upper ) => ( Lower , Upper ) = ( lower , upper ) ;
104+ public override string Serialize ( ) => $ "{ System . Text . Encodings . Web . UrlEncoder . Default . Encode ( Lower ) } ,{ System . Text . Encodings . Web . UrlEncoder . Default . Encode ( Upper ) } ";
105+ }
106+
107+ /// <summary>
108+ /// Represents a numeric range with lower and upper bounds.
109+ /// </summary>
110+ public sealed class NumericRange : FilterValue
111+ {
112+ public int Lower { get ; }
113+ public int Upper { get ; }
114+ internal NumericRange ( int lower , int upper ) => ( Lower , Upper ) = ( lower , upper ) ;
115+ public override string Serialize ( ) => $ "{ Lower } ,{ Upper } ";
116+ }
117+
118+ /// <summary>
119+ /// Represents a date range with lower and upper bounds.
120+ /// </summary>
121+ public sealed class DateRange : FilterValue
122+ {
123+ public DateTime Lower { get ; }
124+ public DateTime Upper { get ; }
125+ internal DateRange ( DateTime lower , DateTime upper ) => ( Lower , Upper ) = ( lower , upper ) ;
126+ public override string Serialize ( ) => $ "{ Lower : yyyy-MM-ddTHH:mm:ssZ} ,{ Upper : yyyy-MM-ddTHH:mm:ssZ} ";
127+ }
128+
129+ /// <summary>
130+ /// Empty filter value for operators that don't require values.
131+ /// </summary>
132+ public static readonly FilterValue EmptyValue = new Empty ( ) ;
133+
134+ // Implicit conversion operators for ease of use
135+ public static implicit operator FilterValue ( string value ) => new StringValue ( value ) ;
136+ public static implicit operator FilterValue ( int value ) => new IntValue ( value ) ;
137+ public static implicit operator FilterValue ( DateTime value ) => new DateTimeValue ( value ) ;
138+ public static implicit operator FilterValue ( bool value ) => new BooleanValue ( value ) ;
139+ public static implicit operator FilterValue ( string [ ] values ) => new StringArrayValue ( values ) ;
140+ public static implicit operator FilterValue ( int [ ] values ) => new IntArrayValue ( values ) ;
141+ public static implicit operator FilterValue ( DateTime [ ] values ) => new DateTimeArrayValue ( values ) ;
142+ }
0 commit comments