Skip to main content

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