Paper Cello
Paper Cello is a library of miscellaneous PHP functions.
There are functions for datetimes, bcrypt hashing, clamping,
pagination, routing and SHA1 token generation., (*1)
Documentation
All functions have a DocBlock. So you can read the source code or
generate documentation with phpDocumentor., (*2)
Requirements
PHP 5.3 or greater, (*3)
Source Code
The project is on GitHub.
All the source code is in this file., (*4)
Tests
All tests are in the test directory.
There is a test for each function; the name of the test script is the function name., (*5)
Installation
Install using composer:, (*6)
{
"require": {
"paper-cello/paper-cello": "0.6.0",
}
}
or you can install manually:, (*7)
require 'paper-cello.php';
datetime_now() and datetime_to()
Use datetime_now() and datetime_to() to handle datetimes.
These functions handle time zones and daylight saving time correctly
without having to use any other time settings., (*8)
Get the current UTC datetime with datetime_now():, (*9)
$utc_now = pc\datetime_now(); //2014-07-24 04:33:12
Use datetime_to() to convert a UTC datetime to a time zone and format:, (*10)
echo pc\datetime_to($utc_now, 'America/Los_Angeles', 'M j, Y g:ia');
//Jul 23, 2014 9:33pm
bcrypt_hash()
Use bcrypt_hash() to hash passwords.
Use either a cost value from 4-31:, (*11)
$hash = pc\bcrypt_hash('password', 12);
//$2a$12$DMWzyZ.iU444JC/.270Bqe84eIwqHOD7ct4jkHY/0gaNv98fHNGx.
Or use a previously obtained hash:, (*12)
echo pc\bcrypt_hash('password', $hash);
//$2a$12$DMWzyZ.iU444JC/.270Bqe84eIwqHOD7ct4jkHY/0gaNv98fHNGx.
clamp()
Clamp a value to a range:, (*13)
echo implode(', ', array(
pc\clamp(5, 1, 10),
pc\clamp(0, 1, 10),
pc\clamp(11, 1, 10),
pc\clamp(11, 1, 10.4),
pc\clamp('d', 'c', 'f'),
pc\clamp('a', 'c', 'f'),
pc\clamp('i', 'c', 'f')));
//5, 1, 10, 10.4, d, c, f
paginate()
Compute total number of pages and current page number
given number of items, items per page and raw current page number:, (*14)
$num_items = 24;
$items_per_page = 5;
$raw_current_page_num = 2;
var_dump(pc\paginate(
$num_items,
$items_per_page,
$raw_current_page_num));
/*
array
0 => int 5
1 => int 2
*/
route()
Use route() to include one of many PHP scripts based on a value.
These examples use echo; use require in your application., (*15)
Route on the default $_GET['r']:, (*16)
echo pc\route(array(
null => 'home.php',
'contact' => 'contact.php'));
//home.php
Use a base directory:, (*17)
echo 'my/route/' . pc\route(array(
null => 'home.php',
'contact' => 'contact.php'));
//my/route/home.php
Route on a POST value:, (*18)
echo pc\route(
array(
null => 'one.php',
'two' => 'two.php'),
$_POST['r']);
//one.php
Route on a value for testing:, (*19)
echo pc\route(
array(
null => 'home.php',
'chess' => 'chess.php',
'golf' => 'golf.php'),
'golf');
//golf.php
There is a shortcut so you don't have to
specify keys in the array. For example the
below is equivalent to the above:, (*20)
echo pc\route(
array(
null => 'home.php',
'chess.php',
'golf.php'),
'golf');
//golf.php
sha1_token()
Get a random token:, (*21)
echo pc\sha1_token(); //003046aec403e654eaadad31658bcad04ad7f95c
LICENSE
MIT http://ryf.mit-license.org/, (*22)