godsdev / tools
Pomocné funkce pro PHP
Installs: 5 703
Dependents: 2
Suggesters: 0
Security: 0
Stars: 0
Watchers: 5
Forks: 1
Open Issues: 0
Requires
- php: ^5.6 || ^7.0
Requires (Dev)
- phpunit/phpunit: ^4|^5
This package is auto-updated.
Last update: 2024-11-14 16:38:10 UTC
README
A class with useful all-purpose functions (methods). The methods are static. Some methods for HTML-output adopt classes used in the Bootstrap library.
Deployment
Once composer is installed, execute the following command in your project root to install this library:
composer require godsdev/tools:^0.3.8
The composer.json
file should then contain current version of the class - something similar to:
{ "require": { "godsdev/tools": "^0.3.8" } }
Then be sure to include the autoloader:
require_once '/path/to/your-project/vendor/autoload.php';
Finally, include this line
use GodsDev\Tools\Tools;
to the file where you want to use Tools' methods. Then you can address all its function like this - Tools::method()
.
Compatibility
- PHP 5.6+, 7+
- several string-processing functions call
mb_XXXX
functions that require theext-mbstring
extension relativeTime()
createsnew DateTime()
stripAttributes()
useslibxml
and createsnew DOMDocument()
,new DOMXPath()
str_putcsv()
opens the"php://memory"
streamcurlCall()
uses cURL extensionwebalize()
tests forext-iconv
extension andiconv()
function
Notes
- Methods handling messages use session (
$_SESSION["messages"]
variable) for storing messages. escapeSQL()
is obsolete and should not be usedescapeIn()
andescapeDbIdentifier()
are specific to MySQL/MariaDb DBMS.
PHP Extensions
The "require"
item in composer.json
should really be:
"require": { "php": "^5.6 || ^7.0", "ext-curl": "*", "ext-date": "*", "ext-dom": "*", "ext-iconv": "*", "ext-json": "*", "ext-mbstring": "*", "ext-pcre": "*", "ext-session": "*", "ext-SimpleXML": "*" }
But since not all methods are used by every project, all those requirements are not stated there. This might trigger some error messages in testing. See chapter Troubleshooting for more.
Configuration
Class Constants
- The
::$LOCALE[]
array contains configuration for locale date/time formats. Example for Czech language:
Tools::LOCALE['cs'] => [ 'date format' => 'j. F', 'full date format' => 'j. F Y', 'time format' => 'H:i:s', 'weekdays' => [ 'Sunday' => 'neděle', 'Monday' => 'pondělí', 'Tuesday' => 'úterý', 'Wednesday' => 'středa', 'Thursday' => 'čtvrtek', 'Friday' => 'pátek', 'Saturday' => 'sobota' ], 'months' => [ 'January' => 'leden', 'February' => 'únor', 'March' => 'březen', 'April' => 'duben', 'May' => 'květen', 'June' => 'červen', 'July' => 'červenec', 'August' => 'srpen', 'September' => 'září', 'October' => 'říjen', 'November' => 'listopad', 'December' => 'prosinec' ], 'time ago' => [ 'y' => ['rok', 'roky', 'let'], //enclination for "1 year" form, "2-4 years" form, and "5+ years" form 'm' => ['měsíc', 'měsíce', 'měsíců'], 'd' => ['den', 'dny', 'dnů'], 'h' => ['hodina', 'hodiny', 'hodin'], 'i' => ['minuta', 'minuty', 'minut'], 's' => ['vteřina', 'vteřiny', 'vteřin'], 'ago' => 'zpátky', 'in' => 'za', 'moment' => 'okamžik' ] ];
- The
::$MESSAGE_ICONS
array contains (HTML-coded) icons that accompany each type of message. The keys (and the message types) are:success
,danger
,warning
andinfo
. - The
::$PASSWORD_CHARS
constant is a string containing characters from which to generate passwords. Used byrandomPassword()
.
Testing
Testing is implemented using phpunit
in projects folder vendor/phpunit/phpunit
. The testing class is in test/ToolsTest.php
(methods there are tested in alphabetical order). If you add a new method, don't forget to add its testing to ToolsTest.php
and run:
vendor/phpunit/phpunit/phpunit
- Note: The
redir()
method (which performs HTTP redirection) is not included in unit testing.* - Note: PHP included in ubuntu-latest (for GitHub Actions) does not support iconv //TRANSLIT flag as iconv implementation is unknown, therefore PHPUnit group iconvtranslit is excluded
Troubleshooting
After running phpunit
you might get error messages saying that certain PHP extension is not available. (See chapter PHP Extensions for more). If your project does not require said extension(s), it will run without error messages of this kind. If it does, it's up to You to provide enabling of this/these extension(s).
Methods
Variable testing and setting
anyset()
– is any of given variables set?equal()
– comparison withisset()
ifempty()
–empty()
withisset()
ifnull()
–isnull()
withisset()
nonempty()
–!empty()
withisset()
nonzero()
– non-zero test withisset()
set()
– test forisset()
or set value to a variablesetarray()
– shortcut forisset()
andis_array()
setifempty()
– shortcut for ifisset()
andempty()
then setsetifnotset()
– shortcut for if!isset()
then setsetifnull()
– shortcut for ifisset()
andis_null()
then setsetscalar()
– shortcut forisset()
andis_scalar()
HTML output
dump()
– shortcut forvar_dump()
in<pre>
...</pre>
h()
– shortcut forhtmlspecialchars()
in UTF-8htmlInput()
– output<input>
of given typehtmlOption()
– output<option>
htmlRadio()
– output<input type="radio">
htmlSelect()
– output<select>
with given optionshtmlTextarea()
– output<textarea>
htmlTextInput()
– output<input type="text">
stripAttributes()
– strip attributes off a HTML code
HTTP
curlCall()
– call a URL, return resulthttpResponse()
– HTTP response split into headers and bodyredir()
– make an HTTP redirectionurlChange()
– add/delete/modify GET variables of an URL
Messages
addMessage()
– add a message to a sessionoutputMessage()
– output a messageresolve()
– add either a 'success' or 'error' message based on a resultshowMessages()
– show messages from session
Strings
begins()
– does a string begin with given parameter?cutTill()
– cut string to first occurence of given parameterends()
– does a string end with given parameter?exploded()
–explode()
and return item of given indexmb_lcfirst()
– lower case first character (multi-byte version)mb_ucfirst()
– upper case first character (multi-byte version)randomPassword()
– return random passwordstr_after()
– return a part of a string after occurence of a parameterstr_before()
– return a part of a string before occurence of a parameterstr_delete()
– delete part of a byref variablestr_putcsv()
– inverse tostr_getcsv()
Conversion
columnName()
– convert number to 26-base (alphabetical) systemescapeDbIdentifier()
– escape function for MySQL/MariaDb identifiersescapeIn()
– escape values in SQL'sIN()
clauseescapeJS()
– escape for JavaScriptescapeSQL()
– basic escape for SQL (OBSOLETE)shortify()
– limit long string to given length, add optional ellipsiswebalize()
– covert to URL-friendly stringxorCipher()
– basic cipheringxorDecipher()
– basic deciphering
Variables
among()
– is given value among listed valuesblacklist()
– set value to given default if on blacklistwhitelist()
– set value to given default if not on whitelist
Arrays
array_search_i()
– case-insensitivearray_search()
arrayConfineKeys()
– extract and return selected keys of given arrayarrayKeyAsValues()
– refill array's values by its keysarrayListed()
– output list of array's itemsarrayReindex()
– reindex array of arrays by given indexarrayRemoveItems()
– remove items from an arrayarraySearchAssoc()
– search array for given key:value pair(s)in_array_i()
– case-insensitivein_array()
Locale
localeDate()
– output date using ::$LOCALE[]
settingslocaleTime()
– output time using ::$LOCALE[]
settingsplural()
– output appropriate singular/plural version of given wordrelativeTime()
– output relative fromnow()
using ::$LOCALE[]
settings
Specific
GoogleAuthenticatorCode()
– GoogleAuthenticator hashpreg_max()
– return RegEx mask to match integer range from 0 to given maximum