|
1 | 1 | # php-utils
|
2 |
| -Basic Classes for PHP |
| 2 | +This is a collection of useful classes and functions for every day PHP life. It includes things such as: |
| 3 | + |
| 4 | +* Handling dates and times and various appearances of it |
| 5 | +* Searching and sorting of arrays of objects |
| 6 | +* Logging to error_log with different levels |
| 7 | +* Notification to users across various requests within a session |
| 8 | +* Extracting information from the current HTTP request |
| 9 | +* Generating random strings |
| 10 | + |
| 11 | +These classes are no rocket science, just simple helpers that prevent from wiriting the |
| 12 | +same code in various flavours over and over again. |
| 13 | + |
| 14 | +# License |
| 15 | +This project is licensed under [GNU LGPL 3.0](LICENSE.md). |
| 16 | + |
| 17 | +# Installation |
| 18 | + |
| 19 | +## By Composer |
| 20 | + |
| 21 | +``` |
| 22 | +composer install technicalguru/utils |
| 23 | +``` |
| 24 | + |
| 25 | +## By Package Download |
| 26 | +You can download the source code packages from [GitHub Release Page](https://github.com/technicalguru/php-utils/releases) |
| 27 | + |
| 28 | +# Classes |
| 29 | + |
| 30 | +## Request class |
| 31 | +The Request class extracts many useful information from the current HTTP request. It provides: |
| 32 | + |
| 33 | +* Header information |
| 34 | +* GET and POST parameter values, assigning defaults if not present |
| 35 | +* Getting the method, protocol, host and path information from the HTTP request |
| 36 | +* Checking the elapsed time of this request |
| 37 | + |
| 38 | +It is possible to create an object yourself, but it is recommend to use the singleton: |
| 39 | + |
| 40 | +``` |
| 41 | +$request = \TgUtils\Request::getRequest(); |
| 42 | +``` |
| 43 | + |
| 44 | +Inspect the [source code](https://github.com/technicalguru/php-utils/blob/src/TgUtils/Request.php) to find out about the various methods available. |
| 45 | + |
| 46 | +## Date class |
| 47 | + |
| 48 | +The date class handles issues for localization, timezones and conversion into different formats or creation from various formats. Here are some examples: |
| 49 | + |
| 50 | +``` |
| 51 | +use TgUtils\Date; |
| 52 | +
|
| 53 | +$timezone = 'Europe/Berlin'; |
| 54 | +
|
| 55 | +// Creating instances with local timezone |
| 56 | +$date = new Date(time(), $timezone); |
| 57 | +$date = Date::createLocalInstance(time(), $timezone); |
| 58 | +$date = Date::createFromMysql($mySqlTimestampString, $timezone); |
| 59 | +
|
| 60 | +// The Epoch time in seconds |
| 61 | +$unixTimestamp = $date->toUnix(); |
| 62 | +
|
| 63 | +// Converting and formatting with local timezone |
| 64 | +$mysqlTimstamp = $date->toMysql(TRUE); |
| 65 | +$iso8601 = $date->toISO8601(TRUE); |
| 66 | +$someString = $date->format('d.m.Y H:i:s', TRUE); |
| 67 | +
|
| 68 | +// Converting to UTC timezones instances |
| 69 | +$mysqlTimstamp = $date->toMysql(); |
| 70 | +$iso8601 = $date->toISO8601(); |
| 71 | +$someString = $date->format('d.m.Y H:i:s'); |
| 72 | +``` |
| 73 | + |
| 74 | +Inspect the [source code](https://github.com/technicalguru/php-utils/blob/src/TgUtils/Date.php) to find out about the various methods available. |
| 75 | + |
| 76 | +## Logging |
| 77 | + |
| 78 | +Logging is very simple. Set your log level (if not INFO) and start logging: |
| 79 | + |
| 80 | +``` |
| 81 | +use TgLog::Log; |
| 82 | +
|
| 83 | +// Set the log level |
| 84 | +Log::setLogLevel('error'); |
| 85 | +
|
| 86 | +// Simple line |
| 87 | +Log::error('A simple error message'); |
| 88 | +
|
| 89 | +// This message will not go into the log |
| 90 | +Log::info('This message is lost.'); |
| 91 | +
|
| 92 | +// Log an exception |
| 93 | +Log::error('An expception was caught: ', $exception); |
| 94 | +
|
| 95 | +// Log an object for later debugging |
| 96 | +Log::error('We have some problem here:', $object); |
| 97 | +``` |
| 98 | + |
| 99 | +The Log can also help when you need to debug something: |
| 100 | + |
| 101 | +``` |
| 102 | +use TgLog::Log; |
| 103 | +
|
| 104 | +// Get the stacktrace |
| 105 | +$stacktrace = Log::getStackTrace(__FILE__); |
| 106 | +
|
| 107 | +// Or log it with INFO level |
| 108 | +Log::infoStackTrace(__FILE__); |
| 109 | +``` |
| 110 | + |
| 111 | +The `__FILE__` parameter will oppress the current file from appearing in the stacktrace. You |
| 112 | +can easily use the function without this argument to have the current file included: |
| 113 | + |
| 114 | +``` |
| 115 | +$stacktrace = Log::getStackTrace(); |
| 116 | +Log::infoStackTrace(__FILE__); |
| 117 | +``` |
| 118 | + |
| 119 | +## User Notifications |
| 120 | + |
| 121 | +Sometimes it is hard to display success or error messages because the message needs to |
| 122 | +be displayed in another script, another HTTP call or even just later. The following snippet |
| 123 | +will remember the message across multiple call within a session. |
| 124 | + |
| 125 | +``` |
| 126 | +use TgLog::Log; |
| 127 | +use TgLog::Success; |
| 128 | +
|
| 129 | +Log::register(new Success('Your data was saved successfully.)); |
| 130 | +``` |
| 131 | + |
| 132 | +The registartion will not only remember the message but also print the message into |
| 133 | +the error log when it's an instance of `Debug`, `Error`, `Warning` or `Info`. |
| 134 | + |
| 135 | +In another call or script you can retrieve your messages again to display: |
| 136 | + |
| 137 | +``` |
| 138 | +use TgLog::Log; |
| 139 | +use TgLog::Success; |
| 140 | +
|
| 141 | +foreach (Log::get() AS $message) { |
| 142 | + switch ($message->getType()) { |
| 143 | + case 'success': |
| 144 | + echo '<div class="text-success">'.$message->getMessage().'</div>'; |
| 145 | + break; |
| 146 | + case 'error': |
| 147 | + echo '<div class="text-danger">'.$message->getMessage().'</div>'; |
| 148 | + break; |
| 149 | + } |
| 150 | +} |
| 151 | +
|
| 152 | +// Finally, clean all messages |
| 153 | +Log::clear(); |
| 154 | +``` |
| 155 | + |
| 156 | +## Other Utils |
| 157 | +There are some daily tasks that need to be done in applications. The `Utils` class addresses a few of them: |
| 158 | + |
| 159 | +``` |
| 160 | +use TgUtils\Utils; |
| 161 | +
|
| 162 | +// create a random string |
| 163 | +$myId = Utils::generateRandomString(20); |
| 164 | +
|
| 165 | +// Find an object with UID 1 |
| 166 | +$myObject = Utils::findByUid($objectList, 3); |
| 167 | +
|
| 168 | +// Find an object with a certain name |
| 169 | +$myObject = Utils::findBy($objectList, 'name', 'John'); |
| 170 | +
|
| 171 | +// Sort an array of objects by its names |
| 172 | +Utils::sort($objectList, 'name'); |
| 173 | +
|
| 174 | +// Sort an array of objects in reverse order of names |
| 175 | +Utils::sort($objectList, 'name', TRUE); |
| 176 | +
|
| 177 | +// Sort an array of objects in reverse order of names, ignore upper/lower case |
| 178 | +Utils::sort($objectList, 'name', TRUE, TRUE); |
| 179 | +
|
| 180 | +// Get all names from the list of objects |
| 181 | +$allNames = Utils::extractAttributeFromList($objectList, 'name'); |
| 182 | +
|
| 183 | +// Mask a sensitive string |
| 184 | +$masked = Utils::anonymize($aPhoneNumber); |
| 185 | +``` |
| 186 | + |
| 187 | +Inspect the [source code](https://github.com/technicalguru/php-utils/blob/src/TgUtils/Utils.php) to find more about the methods available. |
| 188 | + |
| 189 | +# Contribution |
| 190 | +Report a bug, request an enhancement or pull request at the [GitHub Issue Tracker](https://github.com/technicalguru/php-utils/issues). |
0 commit comments