ext_php_rs/zend/
ce.rs

1//! Stock class entries registered with PHP, primarily exceptions.
2
3#![allow(clippy::unwrap_used)]
4
5use crate::ffi::{
6    zend_ce_aggregate, zend_ce_argument_count_error, zend_ce_arithmetic_error, zend_ce_arrayaccess,
7    zend_ce_compile_error, zend_ce_countable, zend_ce_division_by_zero_error,
8    zend_ce_error_exception, zend_ce_exception, zend_ce_iterator, zend_ce_parse_error,
9    zend_ce_serializable, zend_ce_stringable, zend_ce_throwable, zend_ce_traversable,
10    zend_ce_type_error, zend_ce_unhandled_match_error, zend_ce_value_error,
11    zend_standard_class_def,
12};
13
14use super::ClassEntry;
15
16/// Returns the base [`stdClass`](https://www.php.net/manual/en/class.stdclass.php) class.
17///
18/// # Panics
19///
20/// If stdclass [`ClassEntry`] is not available
21pub fn stdclass() -> &'static ClassEntry {
22    unsafe { zend_standard_class_def.as_ref() }.unwrap()
23}
24
25/// Returns the base [`Throwable`](https://www.php.net/manual/en/class.throwable.php) class.
26///
27/// # Panics
28///
29/// If throwable [`ClassEntry`] is not available
30pub fn throwable() -> &'static ClassEntry {
31    unsafe { zend_ce_throwable.as_ref() }.unwrap()
32}
33
34/// Returns the base [`Exception`](https://www.php.net/manual/en/class.exception.php) class.
35///
36/// # Panics
37///
38/// If exception [`ClassEntry`] is not available
39pub fn exception() -> &'static ClassEntry {
40    unsafe { zend_ce_exception.as_ref() }.unwrap()
41}
42
43/// Returns the base [`ErrorException`](https://www.php.net/manual/en/class.errorexception.php) class.
44///
45/// # Panics
46///
47/// If error exception [`ClassEntry`] is not available
48pub fn error_exception() -> &'static ClassEntry {
49    unsafe { zend_ce_error_exception.as_ref() }.unwrap()
50}
51
52/// Returns the base [`CompileError`](https://www.php.net/manual/en/class.compileerror.php) class.
53///
54/// # Panics
55///
56/// If compile error [`ClassEntry`] is not available
57pub fn compile_error() -> &'static ClassEntry {
58    unsafe { zend_ce_compile_error.as_ref() }.unwrap()
59}
60
61/// Returns the base [`ParseError`](https://www.php.net/manual/en/class.parseerror.php) class.
62///
63/// # Panics
64///
65/// If parse error [`ClassEntry`] is not available
66pub fn parse_error() -> &'static ClassEntry {
67    unsafe { zend_ce_parse_error.as_ref() }.unwrap()
68}
69
70/// Returns the base [`TypeError`](https://www.php.net/manual/en/class.typeerror.php) class.
71///
72/// # Panics
73///
74/// If type error [`ClassEntry`] is not available
75pub fn type_error() -> &'static ClassEntry {
76    unsafe { zend_ce_type_error.as_ref() }.unwrap()
77}
78
79/// Returns the base [`ArgumentCountError`](https://www.php.net/manual/en/class.argumentcounterror.php) class.
80///
81/// # Panics
82///
83/// If argument count error [`ClassEntry`] is not available
84pub fn argument_count_error() -> &'static ClassEntry {
85    unsafe { zend_ce_argument_count_error.as_ref() }.unwrap()
86}
87
88/// Returns the base [`ValueError`](https://www.php.net/manual/en/class.valueerror.php) class.
89///
90/// # Panics
91///
92/// If value error [`ClassEntry`] is not available
93pub fn value_error() -> &'static ClassEntry {
94    unsafe { zend_ce_value_error.as_ref() }.unwrap()
95}
96
97/// Returns the base [`ArithmeticError`](https://www.php.net/manual/en/class.arithmeticerror.php) class.
98///
99/// # Panics
100///
101/// If arithmetic error [`ClassEntry`] is not available
102pub fn arithmetic_error() -> &'static ClassEntry {
103    unsafe { zend_ce_arithmetic_error.as_ref() }.unwrap()
104}
105
106/// Returns the base [`DivisionByZeroError`](https://www.php.net/manual/en/class.divisionbyzeroerror.php) class.
107///
108/// # Panics
109///
110/// If division by zero error [`ClassEntry`] is not available
111pub fn division_by_zero_error() -> &'static ClassEntry {
112    unsafe { zend_ce_division_by_zero_error.as_ref() }.unwrap()
113}
114
115/// Returns the base [`UnhandledMatchError`](https://www.php.net/manual/en/class.unhandledmatcherror.php) class.
116///
117/// # Panics
118///
119/// If unhandled match error [`ClassEntry`] is not available
120pub fn unhandled_match_error() -> &'static ClassEntry {
121    unsafe { zend_ce_unhandled_match_error.as_ref() }.unwrap()
122}
123
124/// Returns the [`Traversable`](https://www.php.net/manual/en/class.traversable.php) interface.
125///
126/// # Panics
127///
128/// If traversable [`ClassEntry`] is not available
129pub fn traversable() -> &'static ClassEntry {
130    unsafe { zend_ce_traversable.as_ref() }.unwrap()
131}
132
133/// Returns the [`IteratorAggregate`](https://www.php.net/manual/en/class.iteratoraggregate.php) interface.
134///
135/// # Panics
136///
137/// If aggregate [`ClassEntry`] is not available
138pub fn aggregate() -> &'static ClassEntry {
139    unsafe { zend_ce_aggregate.as_ref() }.unwrap()
140}
141
142/// Returns the [`Iterator`](https://www.php.net/manual/en/class.iterator.php) interface.
143///
144/// # Panics
145///
146/// If iterator [`ClassEntry`] is not available
147pub fn iterator() -> &'static ClassEntry {
148    unsafe { zend_ce_iterator.as_ref() }.unwrap()
149}
150
151/// Returns the [`ArrayAccess`](https://www.php.net/manual/en/class.arrayaccess.php) interface.
152///
153/// # Panics
154///
155/// If arrayaccess [`ClassEntry`] is not available
156pub fn arrayaccess() -> &'static ClassEntry {
157    unsafe { zend_ce_arrayaccess.as_ref() }.unwrap()
158}
159
160/// Returns the [`Serializable`](https://www.php.net/manual/en/class.serializable.php) interface.
161///
162/// # Panics
163///
164/// If serializable [`ClassEntry`] is not available
165pub fn serializable() -> &'static ClassEntry {
166    unsafe { zend_ce_serializable.as_ref() }.unwrap()
167}
168
169/// Returns the [`Countable`](https://www.php.net/manual/en/class.countable.php) interface.
170///
171/// # Panics
172///
173/// If countable [`ClassEntry`] is not available
174pub fn countable() -> &'static ClassEntry {
175    unsafe { zend_ce_countable.as_ref() }.unwrap()
176}
177
178/// Returns the [`Stringable`](https://www.php.net/manual/en/class.stringable.php) interface.
179///
180/// # Panics
181///
182/// If stringable [`ClassEntry`] is not available
183pub fn stringable() -> &'static ClassEntry {
184    unsafe { zend_ce_stringable.as_ref() }.unwrap()
185}
186
187#[cfg(test)]
188#[cfg(feature = "embed")]
189mod tests {
190    use super::*;
191    use crate::embed::Embed;
192
193    #[test]
194    fn test_stdclass() {
195        Embed::run(|| {
196            let stdclass = stdclass();
197            assert_eq!(stdclass.name(), Some("stdClass"));
198        });
199    }
200
201    #[test]
202    fn test_throwable() {
203        Embed::run(|| {
204            let throwable = throwable();
205            assert_eq!(throwable.name(), Some("Throwable"));
206        });
207    }
208
209    #[test]
210    fn test_exception() {
211        Embed::run(|| {
212            let exception = exception();
213            assert_eq!(exception.name(), Some("Exception"));
214        });
215    }
216
217    #[test]
218    fn test_error_exception() {
219        Embed::run(|| {
220            let error_exception = error_exception();
221            assert_eq!(error_exception.name(), Some("ErrorException"));
222        });
223    }
224
225    #[test]
226    fn test_compile_error() {
227        Embed::run(|| {
228            let compile_error = compile_error();
229            assert_eq!(compile_error.name(), Some("CompileError"));
230        });
231    }
232
233    #[test]
234    fn test_parse_error() {
235        Embed::run(|| {
236            let parse_error = parse_error();
237            assert_eq!(parse_error.name(), Some("ParseError"));
238        });
239    }
240
241    #[test]
242    fn test_type_error() {
243        Embed::run(|| {
244            let type_error = type_error();
245            assert_eq!(type_error.name(), Some("TypeError"));
246        });
247    }
248
249    #[test]
250    fn test_argument_count_error() {
251        Embed::run(|| {
252            let argument_count_error = argument_count_error();
253            assert_eq!(argument_count_error.name(), Some("ArgumentCountError"));
254        });
255    }
256
257    #[test]
258    fn test_value_error() {
259        Embed::run(|| {
260            let value_error = value_error();
261            assert_eq!(value_error.name(), Some("ValueError"));
262        });
263    }
264
265    #[test]
266    fn test_arithmetic_error() {
267        Embed::run(|| {
268            let arithmetic_error = arithmetic_error();
269            assert_eq!(arithmetic_error.name(), Some("ArithmeticError"));
270        });
271    }
272
273    #[test]
274    fn test_division_by_zero_error() {
275        Embed::run(|| {
276            let division_by_zero_error = division_by_zero_error();
277            assert_eq!(division_by_zero_error.name(), Some("DivisionByZeroError"));
278        });
279    }
280
281    #[test]
282    fn test_unhandled_match_error() {
283        Embed::run(|| {
284            let unhandled_match_error = unhandled_match_error();
285            assert_eq!(unhandled_match_error.name(), Some("UnhandledMatchError"));
286        });
287    }
288
289    #[test]
290    fn test_traversable() {
291        Embed::run(|| {
292            let traversable = traversable();
293            assert_eq!(traversable.name(), Some("Traversable"));
294        });
295    }
296
297    #[test]
298    fn test_aggregate() {
299        Embed::run(|| {
300            let aggregate = aggregate();
301            assert_eq!(aggregate.name(), Some("IteratorAggregate"));
302        });
303    }
304
305    #[test]
306    fn test_iterator() {
307        Embed::run(|| {
308            let iterator = iterator();
309            assert_eq!(iterator.name(), Some("Iterator"));
310        });
311    }
312
313    #[test]
314    fn test_arrayaccess() {
315        Embed::run(|| {
316            let arrayaccess = arrayaccess();
317            assert_eq!(arrayaccess.name(), Some("ArrayAccess"));
318        });
319    }
320
321    #[test]
322    fn test_serializable() {
323        Embed::run(|| {
324            let serializable = serializable();
325            assert_eq!(serializable.name(), Some("Serializable"));
326        });
327    }
328
329    #[test]
330    fn test_countable() {
331        Embed::run(|| {
332            let countable = countable();
333            assert_eq!(countable.name(), Some("Countable"));
334        });
335    }
336
337    #[test]
338    fn test_stringable() {
339        Embed::run(|| {
340            let stringable = stringable();
341            assert_eq!(stringable.name(), Some("Stringable"));
342        });
343    }
344}