PHP code example of jstewmc / php-helpers

1. Go to this page and download the library: Download jstewmc/php-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/ */

    

jstewmc / php-helpers example snippets


Num::val('1/2');          // returns (float) 0.5
Num::val('1,000');        // returns (int) 1000
Num::val('one hundred');  // returns (int) 100

Num::val('two million, ninety-seven thousand, one hundred and fifty-two');  
// returns (int) 2,097,152

Num::roundTo(7, 10);  // returns 10
Num::ceilTo(7, 10);   // returns 10
Num::floorTo(7, 10);  // returns 0

Num::bound(1, 10);  
// returns 10, because 1, the number, is less than 10, the lower bound

Num::bound(10, 1, 5);
// returns 5, because 10, the number, is greater than 5, the upper bound

Num::normalize(1, 10);   // returns 0.1
Num::normalize(5, 10);   // returns 0.5
Num::normalize(10, 10);  // returns 1

Num::isNumeric(1);             // returns true
Num::isNumeric('1/2');         // returns true
Num::isNumeric('1 1/2');       // returns true
Num::isNumeric('one hundred'); // returns true
Num::isNumeric('foo');         // returns false

Num::isInt('1,000');  // returns true
Num::isInt(1000);     // returns true

Num::isId('foo');           // returns false
Num::isId(1.5);             // returns false
Num::isId(1);               // returns true
Num::isId(1, 'tinyint');    // returns true
Num::isId(999, 'tinyint');  // returns false (too big)

Num::isZero(0);      // returns true
Num::isZero('0');    // returns true
Num::isZero(false);  // returns false

Num::almostEqual(0.2, 0.2);  // returns true
Num::almostEqual(0.2, 0.3);  // returns false

Str::password(8);
// returns a string like 'jNb^3#L@'

Str::password(8, ['upper' => 8]);   
// returns a string like 'NBDRATCV', with exactly eight upper-case characters

Str::password(16, ['number' => 8]);  
// returns a string like '*9f8F6b4F3f1:0/9', with at least eight numbers

Str::truncate('Lorem ipsum inum', 10);  
// returns 'Lorem...', because the "u" in "ipsum" is the 10th character and
// the space after "Lorem" is the closest break character

Str::truncate('Lorem ipsum inum', 15);  
// returns 'Lorem ipsum...', because the "u" in "inum" is the 15th character

Str::truncate('Lorem ipsum inum', 99);  
// returns 'Lorem ipsum inum', because the string is shorter than the limit

Str::endsWith('foo', 'o');     // returns true
Str::iEndsWith('foo', 'O')     // returns true
Str::startsWith('foo', 'f');   // returns true
Str::iStartsWith('foo', 'F');  // returns true

Str::strtocamelcase('Hello world');   // returns "helloWorld"
Str::strtocamelcase('H3LLO WORLD!');  // returns "helloWorld"
Str::strtocamelcase('hello_world');   // returns "helloWorld"

Str::splitOnFirstAlpha('123 foo');  // returns ['123', 'foo']
Str::splitOnFirstAlpha('123');      // returns ['123']
Str::splitOnFirstAlpha('foo');      // returns ['foo']

Str::strtobytes('1K');  // returns 1024
Str::strtobytes('1M');  // returns 1,048,576

Str::isBool('foo');  // returns false
Str::isBool('yes');  // returns true

Boolean::val(true);   // returns true
Boolean::val('on');   // returns true
Boolean::val('yes');  // returns true

$array = ['foo' => 1, 'bar' => 2, 'baz' => 3];

Arr::filterByKeyPrefix($array, 'b');  // returns ['bar' => 2, 'baz' => 2]

$array = ['foo' => 1, 'bar' => 2, 'baz' => 3];

Arr::filterByKey($a, function ($k) {
	return substr($k, 0, 1) === 'b';  
});
// returns ['bar' => 2, 'baz' => 2]

$arrays = [['foo' => 2], ['foo' => 3], ['foo' => 1]];

Arr::sortByField($arrays, 'foo');          
// returns [['foo' => 1], ['foo' => 2], ['foo' => 3]]

Arr::sortByField($arrays, 'foo', 'desc');  
// returns [['foo' => 3], ['foo' => 2], ['foo' => 1]]

// define a example class (for the purposes of this example, we'll define both a
// public property and a getter method)
class Example
{
	public $property;

	public function __construct(int $value)
	{
		$this->property = $value;
	}

	public function getProperty(): int
	{
		return $this->property;
	}
}

$objects = [new Example(2), new Example(3), new Example(1)];

Arr::sortByProperty($objects, 'foo');  
// returns (in pseudo-code) [{bar: 1}, {bar: 2}, {bar: 3}]

Arr::sortByMethod($objects, 'getBar');  
// returns (in pseudo-code) [{bar: 1}, {bar: 2}, {bar: 3}]

$values = ['foo', 'bar', 'baz'];

Arr::inArray($values, 'f*');   // returns true, because of the leading "f" in "foo"
Arr::inArray($values, '*z');   // returns true, because of the trailing "z" in "baz"
Arr::inArray($values, '*a*');  // returns true, because of the "a" in "bar" and "baz"

$array1 = ['foo', 'bar', 'baz'];
$array2 = ['bar', 'qux'];

$actual = Arr::diff($array1, $array2);

$expected = [
	['value' => 'foo', 'mask' => -1],  // because "foo" should be deleted
	['value' => 'bar', 'mask' => 0],   // because "bar" should be unchanged
	['value' => 'baz', 'mask' => -1],  // because "baz" should be deleted
	['value' => 'qux', 'mask' => 1]    // because "qux" should be added
];

$actual == $expected; // returns true

$array = ['foo', 'bar', 'baz'];

$actual = Arr::permute($array);

$expected = [
	['foo', 'bar', 'baz'],
	['baz', 'foo', 'bar'],
	['bar', 'foo', 'baz'],
	['foo', 'baz', 'bar'],
	['bar', 'baz', 'foo'],
	['baz', 'bar', 'foo']
];

$actual == $expected;  // returns true

$array1 = [0 => 'foo', 1 => 'bar'];
$array2 = [0 => 'foo', 1 => 'bar', 'baz' => 'qux'];

Arr::isAssoc($array1);  // returns false
Arr::isAssoc($array2);  // returns true, because there is a string key

$values = ['foo' => null, 'bar' => [], 'baz' => 1];

Arr::isEmpty('qux', $values);  
// returns true, because the key "qux" does not exist

Arr::isEmpty('foo', $values);  
// returns true, because the value of key "foo" is null

Arr::isEmpty('bar', $values);  
// returns true, because the value of key "bar" is empty array

Arr::isEmpty('baz', $values);  
// returns false, because the value of key "baz" is not empty

$array = ['foo' => 'bar', 'baz' => 'qux'];

Arr::keyStringReplace('f', 'g', $a);
// returns ['goo' => 'bar', 'baz' => 'qux'], because "f" was replaced with "g"

Arr::keyStringReplace('f', '', $a);   
// returns ['oo' => 'bar', 'baz' => 'qux'], because "f" was replaced with ""

$source = dirname(__FILE__).'/foo';
$destination = dirname(__FILE__).'/bar';

Dir::copy($source, $destination);  // returns true

$directory = dirname(__FILE__).'/foo';

// The $container verifies you don't delete a directory outside of the target
// area accidentally. The path of the directory to be deleted MUST start with
// this path.
$container = dirname(__FILE__);  

Dir::remove($directory, $container);  // returns true

Dir::abs2rel('/path/to/foo/bar/baz', '/path/to');  // returns "foo/bar/baz"
Dir::abs2rel('/path/to/foo/bar/baz', '/path/to/foo');  // returns "bar/baz"
javascript
{
   "/php-helpers": "^0.2"
   }
}