PHP code example of mojopollo / laravel-helpers

1. Go to this page and download the library: Download mojopollo/laravel-helpers library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mojopollo / laravel-helpers example snippets


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

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

 namespace App\Http\Controllers;

use ArrayHelper;
use FileHelper;
...

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

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

...

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

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

...

string camelCase(string $value)

StringHelper::camelCase('mojo_pollo');

// mojoPollo

string snakeCase(string $value [, string $delimiter = '_'])

StringHelper::snakeCase('mojoPollo');

// mojo_pollo

string replaceFirstMatch(string $search, string $replace, string $subject)

StringHelper::replaceFirstMatch('mojo', 'jojo', 'mojo is a pollo and mojo');

// jojo is a pollo and mojo

string limitByWords(string $str [, int $wordCount = 10])

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

// one two three

mixed random(array $array)

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

// two

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',
//     ],
//   ],
// ]

array castValues(array $originalArray)

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

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

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',
//   ],
// ]

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',
//   ],
// ];

array directoryFiles(string $path)

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

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