PHP code example of sefirosweb / laravel-general-helper

1. Go to this page and download the library: Download sefirosweb/laravel-general-helper 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/ */

    

sefirosweb / laravel-general-helper example snippets


$path = pathTemp();
// $path = /var/www/html/storage/tmp

array_group_by(array $array, string $key, bool $onlyFirstValue = false)

$array = [
    [
        'name' => 'pablo',
        'date' => '02'
    ],
    [
        'name' => 'pablo',
        'date' => '03'
    ],
    [
        'name' => 'selena',
        'date' => 'XX'
    ]
];

$arrayAgrouped = array_group_by($array, 'name');
// Returns:
[
    'pablo' => [
        [
            'name' => 'pablo',
            'date' => '02',
        ],
        [
            'name' => 'pablo',
            'date' => '03',
        ]
    ],
    'selena' => [
        [
            'name' => 'selena',
            'date' => 'XX'
        ]
    ]
]

$arrayAgrouped = array_group_by($array, 'name', true);
[
    'pablo' =>
        [
            'name' => 'pablo',
            'date' => '02',
        ],
    'selena' =>
        [
            'name' => 'selena',
            'date' => 'XX'
        ]
]


array_group_by_multidimensional(array $array, array $union_by, bool $onlyFirstValue = false)

$array = [
    [
        'name' => 'pablo',
        'date' => '02'
    ],
    [
        'name' => 'pablo',
        'date' => '03'
    ],
    [
        'name' => 'selena',
        'date' => 'XX'
    ]
];

$arrayAgrouped = array_group_by_multidimensional($array, ['name','date']);
// Returns:
[
    'pablo' => [
        '02' => [ // <--- sub levels, you can add all levels of you need
            [
                'name' => 'pablo',
                'date' => '02',
            ],
        ],
        '03' => [
            [
                'name' => 'pablo',
                'date' => '03',
            ]
        ]
    ],
    'selena' => [
        'XX' => [
            [
                'name' => 'selena',
                'date' => 'XX'
            ]
        ]
    ]
]

$arrayAgrouped = array_group_by_multidimensional($array, ['name','date'], true);
// Returns:
[
    'pablo' => [
        '02' =>
            [ // Only returns the first item in level of 02
                'name' => 'pablo',
                'date' => '02',
            ],

        '03' =>
            [
                'name' => 'pablo',
                'date' => '03',
            ]
    ],
    'selena' => [
        'XX' =>
            [
                'name' => 'selena',
                'date' => 'XX'
            ]
    ]
]


$var = new stdClass();
$var->field = 5;

$array = objectToArray(object $var);
// $array = [
//     'field' => 5;
// ]


saveCsvInServer(array $arrayData, string $fileName, string $delimiter = ';', string $enclosure = '"', bool $latingMode = false, bool $headers = true, bool $utf8_decode = false, bool $enclosureAll = false)

$array = [
    [
        'name' => 'pablo',
        'date' => '02'
    ],
    [
        'name' => 'pablo',
        'date' => '03'
    ],
    [
        'name' => 'selena',
        'date' => 'XX'
    ]
];

$file = saveCsvInServer($array, 'filename');
// Returns a SavedFile model object
$file->id
$file->path
...


$array = [
    [
        'name' => 'pablo',
        'date' => '02'
    ],
    [
        'name' => 'pablo',
        'date' => '03'
    ],
    [
        'name' => 'selena',
        'date' => 'XX'
    ]
];

$file = saveCsvInServer($array, 'filename');
// Returns a SavedFile model object
$file->id
$file->path
...

$resultString = br2nl('hello<br>world');
// $resultString = 'hello\nworld'

$resultString = eliminar_tildes('Camión');
// $resultString = 'Camion'

// Store data
CacheRequest::set('anyKey', $anyData);

// Retrieve data
$retrieveData = CacheRequest::get('anyKey');

// Hot cache data from function, if key not exists then execute the function to generate them
$retrieveData = CacheRequest::remember('anyKey', function () {
    // .. do something
    return 'return any data';
});

// Delete cache
CacheRequest::delete('anyKey');

// Store data
RedisHelper::set('anyKey', $anyData, $timeout);

// Retrieve datacall($function, $key = '', $prod = false, $EX = 86400)
$retrieveData = RedisHelper::get('anyKey');

// Hot cache data from function, if key not exists then execute the function to generate them, for default is not executed in production
$retrieveData = RedisHelper::call(function(){
    // .. do something
    return 'return any data';
}, 'anyKey', $executeInProduction, $timeout);

// Delete cache
RedisHelper::delete('anyKey')

php artisan migrate

php artisan vendor:publish --provider="Sefirosweb\LaravelGeneralHelper\LaravelGeneralHelperServiceProvider"