robert-b/utils

There is no license information available for the latest version (dev-master) of this package.

Laravel 4/5 helper methods

This package's canonical repository appears to be gone and the package has been frozen as a result.

dev-master 2015-08-20 08:58 UTC

This package is not auto-updated.

Last update: 2019-02-27 16:23:04 UTC


README

A package meant for personal usage, but anyone is invited to get a copy and play with it.

The package was created mainly for Laravel 4 and 5 usage and contains a few helpful methods and an image resizing method collection.

How to Install

  1. Install the robert-b/utils package

    $ composer require "robert-b/utils:1.0.0"
  2. Update config/app.php (or app/config/app.php for Laravel 4) to activate the Utils package

    # Add `UtilsServiceProvider` to the `providers` array
    'providers' => array(
        ...
        'RobertB\Utils\UtilsServiceProvider',
    )
  3. (Laravel 5 only) Use the Utils package in your code.

    <?php
    
    use RobertB\Utils\Utils;
    
    class AwesomeController extends Controller {
        ...
    }
    ?>

Methods

v1.0.1

inflectorPlural

Accepts a string as input parameter, one word, and will return the plural form of the passed in word, or the same word if it's already in plural form.

Usage:

$plural = Utils::inflectorPlural("mouse");
// $plural = "mice"

inflectorSingular

Accepts a string as input parameter, one word, and will return the singular form of the passed in word, or the same word if it's already in singular form.

Usage:

$singular = Utils::inflectorPlural("women");
// $singular = "woman"

inflectorOrdinal

Converts a passed in number to its ordinal English form.

Usage:

$ordinal = Utils::inflectorOrdinal(10);
// $ordinal = "10th"
$ordinal = Utils::inflectorOrdinal(2);
// $ordinal = "2nd"

v1.0.0

generateCode

Will generate a random codebased on an array of options passed.

Available options:

  • seed: (array, optional, default 0 through 9) an array of elements of which the code will be formed.
  • size: (int, optional, default 8) the character size of the code
  • prefix: (string, optional, default null) the prefix for the generated code. Will add to the size.
  • suffix: (string, optional, default null) the suffix for the generated code. Will add to the size.
  • table: (string, optional, default null) the table name against which to check if the code exists. Improves randomness
  • field: (string, optional/required if 'table' is set, default null) the field name from the afformentioned table in which the codes are saved.

Usage:

$code = Utils::generateCode(['table'=>'codes', 'field'=>'code', 'size'=>16]);
$code = Utils::generateCode(['size'=>32]);

purify

Will clean up a string of all the odd and special characters. Usually used to generate URLs for database entries, but can be used to keep clean searchable text in the database if needed.

Parameters:

  • str: (string, required, default null) the string that needs an urgent bath
  • replacer: (string, optional, default _) the character to use when replacing the special chars.

Usage:

$string = "Let's clean, this-puppy up!";
$clean = Utils::purify($string, '-');
// $clean = "let-s-clean-this-puppy-up"

packData

Pack a string for transport so it's not that humanly readable. Usually used in form hidden elements so peekers don't know what you're sending around. It's not Fort Knox but it works agains amateurs.

Usage:

$data = "Some random data";
$pack = Utils::packData($data);
// $pack = "U29tZSByYW5kb20gZGF0YQ=="

unpackData

The reverse of the packData function.

Usage:

$data = "U29tZSByYW5kb20gZGF0YQ==";
$unpacked = Utils::unpackData($data);
// $unpacked = "Some random data";

fuzzyDate

Will convert timestamp formatted date into a fuzzy string interpretation. i.e. "2 minutes ago", "in 1 hour".

Usage:

$now = strtotime("now");
$an_hour_ago = strotime("-1 hour");
$in_an_hour = strotime("+3 months");

$now_fuzzy = Utils::fuzzyDate($now); // a few seconds ago
$an_hour_ago_fuzzy = Utils::fuzzyDate($an_hour_ago); // 1 hour ago
$in_an_hour_fuzzy = Utils::fuzzyDate($in_an_hour); // in 1 hour

formPrep

Method used mainly to clean up text for database entry. Will convert tags and characters to their special entities counterparts.

Parameters:

  • str (string, required, default null) the string in need of cleaning up.

Usage:

$content = "<p>Lorem ipsum's sit & dolor.";
$clean = Utils::formPrep($content); // "&lt;p&gt;Lorem ipsum&#039;s sit &amp; dolor."

restoreTags

Method used to reverse part of the form prepped text. This will restore the tags.

Usage:

$content = "&lt;p&gt;Lorem ipsum&#039;s sit &amp; dolor.";
$restored = Utils::restoreTags($content); // "<p>Lorem ipsum&#039;s sit &amp; dolor."

stopWords

Used to extract a list of words from a large text without the English stop words.

Usage:

$content = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s";
$clean = Utils::stopWords($content) 
//lorem ipsum simply dummy text printing typesetting industry industry's standard dummy text 1500s

Image resizing using Utils

The simple image resizing methods make use of the GD functions available in PHP to rotate, resize and crop any local images passed into the methods.

Usage:

$image = "/tmp/original.png";
$save_to = "/tmp/thumb_100.png";

// Load up the original image
$resize = new Utils();
$resize->sirLoad($image);
$resize->sirResizeToWidth(100);
$resize->sirSave($save_to);

Available methods

sirLoad - Used to load the original file into the resizer

Params:

  • path (string, required) The path to the local file

sirResizeToWidth - Used to resize the loaded file to a mentioned width

Params:

  • width (int, required) The desired width to which to resize to.

sirResizeToHeight - Used to resize the loaded file to a mentioned height

Params:

  • heigth (int, required) The desired height to which to resize to.

sirScale - Used to scale the loaded file up or down

Params:

  • scale (int, required) The desired new scale of the image

sirResize - Used to resize a loaded image to a specified width and height

Params:

  • width (int, required) The desired width to which to resize to.
  • heigth (int, required) The desired height to which to resize to.

Usage:

$resize->sirResize(100, 100);

License

This utils package is free software released under the MIT License. See LICENSE.txt for details.