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