efureev/uphp

u.php useful fn collection & snippets

2.0.2 2018-02-22 08:00 UTC

This package is auto-updated.

Last update: 2024-03-29 03:57:38 UTC


README

utilites4php

GitHub version Build Status Dependency Status Code Climate Test Coverage

Run Tests Example

run all test: phpunit run test unit: phpunit tests/BaseTest

Send the coverage data to Code Climate

CODECLIMATE_REPO_TOKEN= {token} ./vendor/bin/test-reporter

Functions

uFile

  • getExt Return File Extension getExt($filename)
uFile::getExt('/var/log/error.log');
uFile::size('/var/log/error.log');
uFile::size('/var/log/error.log', false);
uFile::sizeFormat(323423);

uList

  • rangeList Return range integer list rangeList($from, $to, $step = 1)
uList::rangeList(1,4); // 1,2,3,4
uList::rangeList(1,50, 10); // 10,20,30,40,50
uList::rangeList(10, 7); // 10,9,8,7

uTime

  • timeList Return time list timeList($minutes = 30)
uTime::timeList();
uTime::timeList(60);

uColor

  • hex2RGB HEX to RGB hex2RGB($hexStr, $returnAsString = false, $separator = ',')
uColor::hex2RGB('00FF00', 1) // '0,255,0'
uColor::hex2RGB('000', 1) // '0,0,0'
uColor::hex2RGB('000', 1,'|') // '0|0|0'
uColor::hex2RGB('000') // ['red' => 0,'green' => 0,'blue' => 0]

uArray

  • isAssociative Associative array or not uArray::isAssociative($array, $allStrings = true)

  • isIndexed Indexed array or not uArray::isAssociative($array, $allStrings = true)

  • clean Remove empty values from array: FALSE, 0, '0', '', null uArray::clean(array $array)

  • removeValue Remove from array by value uArray::removeValue(array $array, $arg, $arg2, ..n)

  • exists Checks if the given key exists in the array by a string representation uArray::exists($key, $array)

$data = [
    'k0' => 'v0',
    'k1' => [
        'k1-1' => 'v1-1'
    ],
    'complex_[name]_!@#$&%*^' => 'complex',
    'k2' => 'string'
];
Arrays::exists('k0', $data); // returns: true
Arrays::exists('k9', $data); // returns: false
Arrays::exists('[k1][k1-1]', $data); // returns: true
Arrays::exists('[k1][k1-2]', $data); // returns: false
Arrays::exists('["complex_[name]_!@#$&%*^"]', $data); // returns: true
Arrays::exists('[k2][2]', $data); // returns: false
  • save Save element to the array by a string representation uArray::save($key, &$array, $value, $replace = true)
$data = [
    'k2' => 'string'
];

Arrays::save('k0', $data, 'v0'); // returns: true, save as 'k0' => 'v0'
Arrays::save('[k1][k1-1]', $data, 'v1-1'); // returns: true, save as 'k1' => ['k1-1' => 'v1-1']
Arrays::save('[k2][2]', $data, 'p'); // returns: false, can't save value to string

// Broken key names
Arrays::save('k3[', $data, 'v3'); // returns: false, can't save, bad syntax
Arrays::save('["k4["]', $data, 'v4'); // returns: true, save as 'k4[' => 'v4'
Arrays::save('"k4["', $data, 'v4'); // returns: false, can't save, bad syntax

// Append
Arrays::save('k5', $data, []); // returns: true, create array 'k5' => []
Arrays::save('k5[]', $data, 'v5-0'); // returns: true, append value to exists array 'k5' => [ 'v5-0' ]
Arrays::save('k6[k6-1][]', $data, 'v6-1-0'); // returns: true, save as 'k6' => [ 'k6-1' => [ 'v6-1-0' ] ]

// Replace if not exists
Arrays::save('k2', $data, 'something', false); // returns false, value not replaced because value is exists
  • delete Delete element from the array by a string representation uArray::delete($key, &$array)
$data = [
    'k0' => 'v0',
    'k1' => [
        'k1-1' => 'v1-1'
    ],
    'complex_[name]_!@#$&%*^' => 'complex'
];

Arrays::delete('k0', $data); // returns: true, delete element from array
Arrays::delete('k9', $data); // returns: false

Arrays::delete('[k1][k1-1]', $data); // returns: true, delete element from array
Arrays::delete('[k1][k1-2]', $data); // returns: false

Arrays::delete('["complex_[name]_!@#$&%*^"]', $data); // returns: true, delete element from array
  • get Get element of the array by a string representation uArray::get($key, $array, $default = null, $ignoreString = true)
$data = [
    'k0' => 'v0',
    'k1' => [
        'k1-1' => 'v1-1'
    ],
    'complex_[name]_!@#$&%*^' => 'complex',
    'k2' => 'string'
];

Arrays::get('k0', $data); // returns: 'v0'
Arrays::get('k9', $data, '0'); // returns: '0', key isn't exists in array

Arrays::get('[k1][k1-1]', $data); // returns: 'v1-1'
Arrays::get('[k1][k1-2]', $data, 'default'); // returns: 'default', key isn't exists in array

Arrays::get('["complex_[name]_!@#$&%*^"]', $data); // returns: 'complex'

Arrays::get('[k2][2]', $data); // returns: null, key isn't exists in array

// If you want get a symbol from string value, you may switch off option $ignoreString = false
Arrays::get('[k2][2]', $data, null, false); // returns: 'r'
Arrays::get('[k2][null]', $data, null, false); // returns: null, offset isn't exists in string