Giter Site home page Giter Site logo

laravel-helpers's Introduction

Mojo's Laravel Helpers

Build Status Latest Stable Version Latest Unstable Version License Total Downloads

Mojo's Laravel Helpers: A suite of various complimentary helpers for the Laravel framework and PHP 5.4/5.5/5.6/7.0. Some of these helpers are based on code found in Stackoverflow, look for the @see properties for references.

Installation

Step 1: Add package via composer

Add this package to your composer.json file with the following command

composer require mojopollo/laravel-helpers

Step 2: Update laravel 5.x config/app.php file

Add the following into the providers array:

Mojopollo\Helpers\StringHelperServiceProvider::class,
Mojopollo\Helpers\ArrayHelperServiceProvider::class,
Mojopollo\Helpers\DateTimeHelperServiceProvider::class,
Mojopollo\Helpers\FileHelperServiceProvider::class,

Add the following into the aliases array:

'StringHelper'   => Mojopollo\Helpers\Facades\StringHelper::class,
'ArrayHelper'    => Mojopollo\Helpers\Facades\ArrayHelper::class,
'DateTimeHelper' => Mojopollo\Helpers\Facades\DateTimeHelper::class,
'FileHelper'     => Mojopollo\Helpers\Facades\FileHelper::class,

Usage

To enable any of the helpers classes inside a controller, invoke the use keyword:

<?php namespace App\Http\Controllers;

use ArrayHelper;
use FileHelper;
...

You can now call any of helper methods via their facades:

StringHelper::replaceFirstMatch('one two three four five six', 3)

FileHelper::directoryFiles('/directory-path')

...

If you do not want to utilize the use keyword, you can alternatively use a blackslash\ when calling the helpers:

\StringHelper::replaceFirstMatch('one two three four five six', 3)

\FileHelper::directoryFiles('/directory-path')

...

StringHelper

camelCase

Convert a value to camel case

string camelCase(string $value)
StringHelper::camelCase('mojo_pollo');

// mojoPollo

snakeCase

Convert a value to snake case

string snakeCase(string $value [, string $delimiter = '_'])
StringHelper::snakeCase('mojoPollo');

// mojo_pollo

replaceFirstMatch

str_replace() for replacing just the first match in a string search

string replaceFirstMatch(string $search, string $replace, string $subject)
StringHelper::replaceFirstMatch('mojo', 'jojo', 'mojo is a pollo and mojo');

// jojo is a pollo and mojo

limitByWords

Returns a string limited by the word count specified

string limitByWords(string $str [, int $wordCount = 10])
StringHelper::limitByWords('one two three four five six', 3);

// one two three

ArrayHelper

random

Get a random element from the array supplied

mixed random(array $array)
ArrayHelper::random(['one', 'two', 'three']);

// two

morphKeys

Will camelize all keys found in a array or multi dimensional array

array morphKeys(array $originalArray [, $morphTo = 'camel'])
ArrayHelper::morphKeys([
  'user' => [
    'first_name' => 'mojo',
    'attributes' => [
      'second_key' => 'second value',
    ],
  ],
], 'camel');

// [
//   'user' => [
//     'firstName' => 'mojo',
//     'attributes' => [
//       'secondKey' => 'second value',
//     ],
//   ],
// ]

castValues

Will cast '123' as int, 'true' as the boolean true, etc

array castValues(array $originalArray)
ArrayHelper::castValues([
  'value1' => 'true',
  'value2' => 'false',
  'value3' => '123',
  'value4' => '{"mojo": "pollo"}',
]);

// [
//   'value1' => true,
//   'value2' => false,
//   'value3' => 123,
//   'value4' => ['mojo' => 'pollo'],
// ]

sortByPriority

Re-orders an array by moving elements to the top of the array based on a pre-defined array stating which elements to move to top of array. Note: when $strictMatch is set to false, the match will not take into account type and case sensitivity

array sortByPriority(array $originalArray, array $priority [, $strictMatch = true])
$originalArray = [
  [
    'name' => 'White Castle',
    'city' => 'Las Vegas',
    'zip' => '89109',
  ],
  [
    'name' => 'Burger Town',
    'city' => 'Sherman Oaks',
    'zip' => '91403',
  ],
  [
    'name' => 'Krabby Patty',
    'city' => 'Walking the Plankton',
    'zip' => '00000',
  ],
  [
    'name' => 'Uber Burger',
    'city' => 'Little Rock',
    'zip' => '72201',
  ],
];

$priority = [
  [
    'city' => 'Walking the Plankton'
  ],
  [
    'name' => 'Burger Town'
  ],
];


ArrayHelper::sortByPriority($originalArray, $priority);

// [
//   [
//     'name' => 'Krabby Patty',
//     'city' => 'Walking the Plankton',
//     'zip' => '00000',
//   ],
//   [
//     'name' => 'Burger Town',
//     'city' => 'Sherman Oaks',
//     'zip' => '91403',
//   ],
//   [
//     'name' => 'White Castle',
//     'city' => 'Las Vegas',
//     'zip' => '89109',
//   ],
//   [
//     'name' => 'Uber Burger',
//     'city' => 'Little Rock',
//     'zip' => '72201',
//   ],
// ]

DateTimeHelper

range

Generates an array of start and endates for a specified period of time (UTC)

array range(string $startDate, string $endDate, string $periodDate, string $step = '+1 day', string $daysOfWeek = null, string $dateFormat = 'Y-m-d H:i:s')
$startDate = '2015-06-04 08:00:00';
$endDate = '2015-06-04 12:00:00';
$periodDate = '2015-06-06 12:00:00';
$step = '+1 day';
$daysOfWeek = null;
$dateFormat = 'Y-m-d H:i:s';

DateTimeHelper::range($startDate, $endDate, $periodDate, $step, $daysOfWeek, $dateFormat);

// [
//   [
//     'start' => '2015-06-04 08:00:00',
//     'end' => '2015-06-04 12:00:00',
//   ],
//   [
//     'start' => '2015-06-05 08:00:00',
//     'end' => '2015-06-05 12:00:00',
//   ],
//   [
//     'start' => '2015-06-06 08:00:00',
//     'end' => '2015-06-06 12:00:00',
//   ],
// ]
$startDate = '2015-09-23 10:11:51';
$endDate = '2015-09-24 02:55:51';
$periodDate = '2015-09-30 11:59:59';
$step = '+1 week';
$daysOfWeek = 'mon,wed,fri';
$dateFormat = 'Y-m-d H:i:s';

DateTimeHelper::range($startDate, $endDate, $periodDate, $step, $daysOfWeek, $dateFormat);

// [
//   [
//     'start' => '2015-09-21 10:11:51',
//     'end' => '2015-09-22 02:55:51',
//   ],
//   [
//     'start' => '2015-09-23 10:11:51',
//     'end' => '2015-09-24 02:55:51',
//   ],
//   [
//     'start' => '2015-09-25 10:11:51',
//     'end' => '2015-09-26 02:55:51',
//   ],
//   [
//     'start' => '2015-09-28 10:11:51',
//     'end' => '2015-09-29 02:55:51',
//   ],
//   [
//     'start' => '2015-09-30 10:11:51',
//     'end' => '2015-10-01 02:55:51',
//   ],
//   [
//     'start' => '2015-10-02 10:11:51',
//     'end' => '2015-10-03 02:55:51',
//   ],
// ];

FileHelper

directoryFiles

Return an array of files with their full paths contained in a directory and its subdirectories

array directoryFiles(string $path)
FileHelper::directoryFiles('/directory-path');

// [
//   '/directory-path/file1.txt',
//   '/directory-path/file2.txt',
//   '/directory-path/subdirectory/file3.txt',
// ]

laravel-helpers's People

Contributors

mojopollo avatar

Watchers

 avatar

laravel-helpers's Issues

Allow $priority to have case insensitive matches

Right now if you set priority like so:

$priority = [
  [
    'city' => 'Walking the Plankton'
  ],
  [
    'name' => 'Burger Town'
  ],
];

"Walking the Plankton" and "Burger Town" must match exactly

so

"walking the plankton" and "burger town"

will not match and thus not be moved to top of the array

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.