Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame^] | 1 | <?php |
| 2 | |
| 3 | namespace LdapRecord; |
| 4 | |
| 5 | trait DetectsErrors |
| 6 | { |
| 7 | /** |
| 8 | * Determine if the error was caused by a lost connection. |
| 9 | * |
| 10 | * @param string $error |
| 11 | * |
| 12 | * @return bool |
| 13 | */ |
| 14 | protected function causedByLostConnection($error) |
| 15 | { |
| 16 | return $this->errorContainsMessage($error, ["Can't contact LDAP server", 'Operations error']); |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * Determine if the error was caused by lack of pagination support. |
| 21 | * |
| 22 | * @param string $error |
| 23 | * |
| 24 | * @return bool |
| 25 | */ |
| 26 | protected function causedByPaginationSupport($error) |
| 27 | { |
| 28 | return $this->errorContainsMessage($error, 'No server controls in result'); |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Determine if the error was caused by a size limit warning. |
| 33 | * |
| 34 | * @param $error |
| 35 | * |
| 36 | * @return bool |
| 37 | */ |
| 38 | protected function causedBySizeLimit($error) |
| 39 | { |
| 40 | return $this->errorContainsMessage($error, ['Partial search results returned', 'Size limit exceeded']); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Determine if the error was caused by a "No such object" warning. |
| 45 | * |
| 46 | * @param string $error |
| 47 | * |
| 48 | * @return bool |
| 49 | */ |
| 50 | protected function causedByNoSuchObject($error) |
| 51 | { |
| 52 | return $this->errorContainsMessage($error, ['No such object']); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Determine if the error contains the any of the messages. |
| 57 | * |
| 58 | * @param string $error |
| 59 | * @param string|array $messages |
| 60 | * |
| 61 | * @return bool |
| 62 | */ |
| 63 | protected function errorContainsMessage($error, $messages = []) |
| 64 | { |
| 65 | foreach ((array) $messages as $message) { |
| 66 | if (strpos($error, $message) !== false) { |
| 67 | return true; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | return false; |
| 72 | } |
| 73 | } |