# Helpers

We have build a package with some helpers that are usefull in almost every project. We use this package in all our projects. For ourself and for our customers. If you miss anything, create an issue and will will add it as soon as possible.

# Installation

composer require marshmallow/helpers
1

# CSV

This helper will make generating .csv files extremly ease. Please check the examples below. You can use the store method to store it in a location of your choosing. You can use the download method if you just want to download the file. If you wish to store and download the generated .csv file you can use the storeAndDownload method.

# Simple usage example

$headers = ['Column 1', 'Column 2', 'Column 3'];
$data = [
	[1, 2, 3],
	[4, 5, 6],
];

return CSV::headers($headers)
	->data($data)
	->setFilename('generated-csv-1234')
	->storeAndDownload();
1
2
3
4
5
6
7
8
9
10

# Use a collection as data attribute

You can use a collection as your data attribute. By default it will map all the data in the collection sequence. If you wish to edit the collection data, you can add a callback method to the data method. Please note that this will give you every row as an array value.

use Marshmallow\HelperFunctions\Facades\CSV;

CSV::headers($headers)
        ->data($data, function (array $row) {
            return [
                $row['name'],
                $row['slug'],
                $row['created_at'],
            ];
        })
        ->storeAndDownload();
1
2
3
4
5
6
7
8
9
10
11

# Store and download calls

The following methods are available for storing and downloading the generated csv file.

$csv->download();
$csv->store();
$csv->storeAndDownload();
$csv->stream();
1
2
3
4

# Str

The Str helper extends the helper from Laravel. So you have all the methods available in the Laravel helper available as well. Check the Laravel documentation (opens new window) for all the available methods.

# Str::join()

This method is super handy for concatenating strings separated by a comma, but use another value for the last item. Checkout the example below.

Str::join([
  'Marshmallow',
  'Stef van Esch',
  'Mr Mallow'
]);

// Marshmallow, Stef van Esch and Mr Mallow
1
2
3
4
5
6
7

# Str::random()

We have added on the default Str::random() of Laravel. We've added a second parameter which is an array of characters that should be ignored. We also have build in a couple of presets like lowercase which will make sure the random string won't contain any lowercase characters.

Str::random($limit = 16, $ignore = [
	/**
	 * Custom items
	 */
  	'A','B', 'C', 'D',

  	/**
  	 * Presets
  	 */
	'lowercase', 	// Will ignore all lowercase characters.
	'uppercase',	// Will ignore all uppercase characters.
	'letters',		// Will ignore all letters.
	'numbers',		// Will ignore all numbers.
	'similar',		// Will ignore all numbers and letters that have been marked as similar.
]);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

# Str::cleanPhoneNumber()

Str::cleanPhoneNumber()
1

# Str::phonenumberWithCountryCode()

Return a cleaned phonenumber with the country code. You can choose to return the phonenumber with a + or 00 at the start of the phonenumber.

Str::phonenumberWithCountryCode('0628998954')
// response: +31628998954

Str::phonenumberWithCountryCode('0031628998954', $country_code = '31', $use_plus_instead_of_zeros = false)
// response: 0031628998954
1
2
3
4
5

# Str::numbersOnly()

Str::numbersOnly()
1

# Str::numbersAndLettersOnly()

Str::numbersAndLettersOnly()
1

# Str::readmore()

Str::readmore($string, $lenght_first_part, $return_this_part = null)
1

# Str::paragraphsAsArray()

Str::paragraphsAsArray($string)
1

# Str::getFirstParagraph()

Str::getFirstParagraph($string, $number_of_paragraphs = 1, $return_array = false)
1

# Str::getAllButFirstParagraph()

Str::getAllButFirstParagraph($string, $number_of_paragraphs_to_skip = 1, $return_array = false)
1

# Migrations

We have a trait available that gives you some extra options when creating migrations. Add the MigrationHelper trait to you migration to make use of these options.

use Marshmallow\HelperFunctions\Traits\MigrationHelper;

class CreateProductTable extends Migration
{
    use MigrationHelper;
1
2
3
4
5

# Create a column only if it doesnt exist.

This method was added because when a database that already has a products table and later will use our Product package, the migrations will through error's.

$this->createColumnIfDoesntExist(
    'products', 'deleted_at', function (Blueprint $table) {
        $table->softDeletes();
    }
);
1
2
3
4
5

# URL

# URL::isInternal()

URL::isInternal($url)
1

# URL::isCurrent()

URL::isCurrent($url)
1

# URL::buildFromArray()

URL::buildFromArray($array)
1

# URL::isNova()

URL::isNova($request)
1

# URL::isNotNova()

URL::isNotNova($request)
1

# Arrayable

# Arrayable::storeInFile();

This method will store a pretty array in a file. With this method is possible to generate config files.

Arrayable::storeInFile(array $array, string $file_location);
1

# Builder

# Builder::published()

BuilderHelper::published will filter on database columns if something is published.

public function scopePublished (Builder $builder)
{
	BuilderHelper::published($builder, $valid_from_column, $valid_till_column);
}
1
2
3
4

# ReviewHelper

For the review stars you can call ReviewHelper::ratingToStars(4.5). By default the ReviewHelper will think you are using a max rating of 5, support half star rating and return a string of FontAwesome icons. You can overule this behaviour by;

# Customise

Create the config file config/review.php and specify your needs:

return [
    'max_rating' => 10,
    'full_star' => '+ ',
    'half_star' => '* ',
    'empty_star' => '- ',
];
1
2
3
4
5
6

Or you can provide the same config array as a second parameter to the ratingToStars method like so;

ReviewHelper::ratingToStars(4.5, [
    'max_rating' => 10,
    'full_star' => '+ ',
    'half_star' => '* ',
    'empty_star' => '- ',
])
1
2
3
4
5
6

# Helper functions

percentage(47, App\Post::get()); // 63.829787234043
1

# Grouper

Grouper is a super handy helper when you need to split your queries results in to groups. We use this with blogs a lot. The first row might have 3 results, the second row will have 1 result, en the third row will have 3 items again. Please see the example below.

use Marshmallow\HelperFunctions\Facades\Collection;

$grouper = Collection::createGrouper(Blog::get(), $structure_array = [
	'first' => 3,
	'second' => 1,
	'third' => 2,
]);
1
2
3
4
5
6
7

As you can see in the example, the structure_array has a name as the key. This is done so when you are looping through your groups, you are able the test which group you are currently looping..

foreach ($grouper as $group) {
	if ($group->is('first')) {
		// Add your template for 3 items
	}

	if ($group->is('second')) {
		// Add your template for 1 item
	}
}
1
2
3
4
5
6
7
8
9

Looping through the group items works just like you expect when working with collections.

foreach ($grouper as $group) {
  	foreach ($group as $blog) {
    	//
  	}
}
1
2
3
4
5