1<?php
2/**
3 * This file is part of the FreeDSx ASN1 package.
4 *
5 * (c) Chad Sikorra <Chad.Sikorra@gmail.com>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10
11namespace FreeDSx\Asn1;
12
13use DateTimeInterface;
14use FreeDSx\Asn1\Type\AbstractTimeType;
15use FreeDSx\Asn1\Type\AbstractType;
16use FreeDSx\Asn1\Type\BitStringType;
17use FreeDSx\Asn1\Type\BmpStringType;
18use FreeDSx\Asn1\Type\BooleanType;
19use FreeDSx\Asn1\Type\CharacterStringType;
20use FreeDSx\Asn1\Type\EnumeratedType;
21use FreeDSx\Asn1\Type\GeneralizedTimeType;
22use FreeDSx\Asn1\Type\GeneralStringType;
23use FreeDSx\Asn1\Type\GraphicStringType;
24use FreeDSx\Asn1\Type\IA5StringType;
25use FreeDSx\Asn1\Type\IntegerType;
26use FreeDSx\Asn1\Type\NullType;
27use FreeDSx\Asn1\Type\NumericStringType;
28use FreeDSx\Asn1\Type\OctetStringType;
29use FreeDSx\Asn1\Type\OidType;
30use FreeDSx\Asn1\Type\PrintableStringType;
31use FreeDSx\Asn1\Type\RealType;
32use FreeDSx\Asn1\Type\RelativeOidType;
33use FreeDSx\Asn1\Type\SequenceOfType;
34use FreeDSx\Asn1\Type\SequenceType;
35use FreeDSx\Asn1\Type\SetOfType;
36use FreeDSx\Asn1\Type\SetType;
37use FreeDSx\Asn1\Type\TeletexStringType;
38use FreeDSx\Asn1\Type\UniversalStringType;
39use FreeDSx\Asn1\Type\UtcTimeType;
40use FreeDSx\Asn1\Type\Utf8StringType;
41use FreeDSx\Asn1\Type\VideotexStringType;
42use FreeDSx\Asn1\Type\VisibleStringType;
43
44/**
45 * Used to construct various ASN1 structures.
46 *
47 * @author Chad Sikorra <Chad.Sikorra@gmail.com>
48 */
49class Asn1
50{
51    /**
52     * @param AbstractType ...$types
53     * @return SequenceType
54     */
55    public static function sequence(AbstractType ...$types): SequenceType
56    {
57        return new SequenceType(...$types);
58    }
59
60    /**
61     * @param AbstractType ...$types
62     * @return SequenceOfType
63     */
64    public static function sequenceOf(AbstractType ...$types): SequenceOfType
65    {
66        return new SequenceOfType(...$types);
67    }
68
69    /**
70     * @param int $int
71     * @return IntegerType
72     */
73    public static function integer(int $int): IntegerType
74    {
75        return new IntegerType($int);
76    }
77
78    /**
79     * @param bool $bool
80     * @return BooleanType
81     */
82    public static function boolean(bool $bool): BooleanType
83    {
84        return new BooleanType($bool);
85    }
86
87    /**
88     * @param int $enum
89     * @return EnumeratedType
90     */
91    public static function enumerated(int $enum): EnumeratedType
92    {
93        return new EnumeratedType($enum);
94    }
95
96    /**
97     * @param float $real
98     * @return RealType
99     */
100    public static function real(float $real): RealType
101    {
102        return new RealType($real);
103    }
104
105    /**
106     * @return NullType
107     */
108    public static function null(): NullType
109    {
110        return new NullType();
111    }
112
113    /**
114     * @param string $string
115     * @return OctetStringType
116     */
117    public static function octetString(string $string): OctetStringType
118    {
119        return new OctetStringType($string);
120    }
121
122    /**
123     * @param string $bitString
124     * @return BitStringType
125     */
126    public static function bitString(string $bitString): BitStringType
127    {
128        return new BitStringType($bitString);
129    }
130
131    /**
132     * @param int $integer
133     * @return BitStringType
134     */
135    public static function bitStringFromInteger(int $integer): BitStringType
136    {
137        return BitStringType::fromInteger($integer);
138    }
139
140    /**
141     * @param string $binary
142     * @return BitStringType
143     */
144    public static function bitStringFromBinary($binary): BitStringType
145    {
146        return BitStringType::fromBinary($binary);
147    }
148
149    /**
150     * @param string $oid
151     * @return OidType
152     */
153    public static function oid(string $oid): OidType
154    {
155        return new OidType($oid);
156    }
157
158    /**
159     * @param string $oid
160     * @return RelativeOidType
161     */
162    public static function relativeOid(string $oid): RelativeOidType
163    {
164        return new RelativeOidType($oid);
165    }
166
167    /**
168     * @param string $string
169     * @return BmpStringType
170     */
171    public static function bmpString(string $string): BmpStringType
172    {
173        return new BmpStringType($string);
174    }
175
176    /**
177     * @param string $string
178     * @return CharacterStringType
179     */
180    public static function charString(string $string): CharacterStringType
181    {
182        return new CharacterStringType($string);
183    }
184
185    /**
186     * @param DateTimeInterface|null $dateTime
187     * @param string $dateFormat
188     * @param string $tzFormat
189     * @return GeneralizedTimeType
190     */
191    public static function generalizedTime(?DateTimeInterface $dateTime = null, string $dateFormat = AbstractTimeType::FORMAT_FRACTIONS, string $tzFormat = AbstractTimeType::TZ_UTC): GeneralizedTimeType
192    {
193        return new GeneralizedTimeType($dateTime, $dateFormat, $tzFormat);
194    }
195
196    /**
197     * @param DateTimeInterface|null $dateTime
198     * @param string $dateFormat
199     * @param string $tzFormat
200     * @return UtcTimeType
201     */
202    public static function utcTime(?DateTimeInterface $dateTime = null, string $dateFormat = AbstractTimeType::FORMAT_SECONDS, string $tzFormat = AbstractTimeType::TZ_UTC): UtcTimeType
203    {
204        return new UtcTimeType($dateTime, $dateFormat, $tzFormat);
205    }
206
207    /**
208     * @param string $string
209     * @return GeneralStringType
210     */
211    public static function generalString(string $string): GeneralStringType
212    {
213        return new GeneralStringType($string);
214    }
215
216    /**
217     * @param string $string
218     * @return GraphicStringType
219     */
220    public static function graphicString(string $string): GraphicStringType
221    {
222        return new GraphicStringType($string);
223    }
224
225    /**
226     * @param string $string
227     * @return IA5StringType
228     */
229    public static function ia5String(string $string): IA5StringType
230    {
231        return new IA5StringType($string);
232    }
233
234    /**
235     * @param string $string
236     * @return NumericStringType
237     */
238    public static function numericString(string $string): NumericStringType
239    {
240        return new NumericStringType($string);
241    }
242
243    /**
244     * @param string $string
245     * @return PrintableStringType
246     */
247    public static function printableString(string $string): PrintableStringType
248    {
249        return new PrintableStringType($string);
250    }
251
252    /**
253     * @param string $string
254     * @return TeletexStringType
255     */
256    public static function teletexString(string $string): TeletexStringType
257    {
258        return new TeletexStringType($string);
259    }
260
261    /**
262     * @param string $string
263     * @return UniversalStringType
264     */
265    public static function universalString(string $string): UniversalStringType
266    {
267        return new UniversalStringType($string);
268    }
269
270    /**
271     * @param string $string
272     * @return Utf8StringType
273     */
274    public static function utf8String(string $string): Utf8StringType
275    {
276        return new Utf8StringType($string);
277    }
278
279    /**
280     * @param string $string
281     * @return VideotexStringType
282     */
283    public static function videotexString(string $string): VideotexStringType
284    {
285        return new VideotexStringType($string);
286    }
287
288    /**
289     * @param string $string
290     * @return VisibleStringType
291     */
292    public static function visibleString(string $string): VisibleStringType
293    {
294        return new VisibleStringType($string);
295    }
296
297    /**
298     * @param AbstractType ...$types
299     * @return SetType
300     */
301    public static function set(AbstractType ...$types): SetType
302    {
303        return new SetType(...$types);
304    }
305
306    /**
307     * @param AbstractType ...$types
308     * @return SetOfType
309     */
310    public static function setOf(AbstractType ...$types): SetOfType
311    {
312        return new SetOfType(...$types);
313    }
314
315    /**
316     * @template T of AbstractType
317     * @param int $tagNumber
318     * @param T $type
319     * @return T
320     */
321    public static function context(int $tagNumber, AbstractType $type)
322    {
323        return $type->setTagClass(AbstractType::TAG_CLASS_CONTEXT_SPECIFIC)->setTagNumber($tagNumber);
324    }
325
326    /**
327     * @template T of AbstractType
328     * @param int $tagNumber
329     * @param T $type
330     * @return T
331     */
332    public static function application(int $tagNumber, AbstractType $type)
333    {
334        return $type->setTagClass(AbstractType::TAG_CLASS_APPLICATION)->setTagNumber($tagNumber);
335    }
336
337    /**
338     * @template T of AbstractType
339     * @param int $tagNumber
340     * @param T $type
341     * @return T
342     */
343    public static function universal(int $tagNumber, AbstractType $type)
344    {
345        return $type->setTagClass(AbstractType::TAG_CLASS_UNIVERSAL)->setTagNumber($tagNumber);
346    }
347
348    /**
349     * @template T of AbstractType
350     * @param int $tagNumber
351     * @param T $type
352     * @return T
353     */
354    public static function private(int $tagNumber, AbstractType $type)
355    {
356        return $type->setTagClass(AbstractType::TAG_CLASS_PRIVATE)->setTagNumber($tagNumber);
357    }
358}
359