PHP code example of sebastiaanluca / php-helpers

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

    

sebastiaanluca / php-helpers example snippets


rand_bool();

// true

str_wrap('foo', '*');

// "*foo*"

is_assoc_array(['color' => 'blue', 'age' => 31]);

// true

is_assoc_array([0 => 'blue', 7 => 31]);

// true

is_assoc_array(['blue', 31]);

// false

is_assoc_array([0 => 'blue', 1 => 31]);

// false

array_expand(['products.desk.price' => 200]);

/*
[
    "products" => [
        "desk" => [
            "price" => 200,
        ],
    ],
]
*/

$cars = ['bmw', 'mercedes', 'audi'];
$soldOut = ['audi', 'bmw'];

$inStock = array_without($cars, $soldOut);

// ["mercedes"]

array_without(['one', 'two', 'three'], 'two');

// ["one", "three"]

$source = ['A', 'B', 'C'];

$removed = array_pull_value($source, 'C');

// $removed = "C"
// $source = ["A", "B"]

$source = ['A', 'B', 'C'];
$removed = array_pull_values($source, ['A', 'B']);

// $removed = ["A", "B"]
// $source = ["C"]

array_hash([1, 2, 3]);

// "262bbc0aa0dc62a93e350f1f7df792b9"

array_hash(['hash' => 'me']);

// "f712e79b502bda09a970e2d4d47e3f88"

class ValueObject {
    public $property = 'randomvalue';
}

object_hash(new ValueObject);

// "f39eaea7a1cf45f5a0c813d71b5f2f57"

class Hitchhiker {
    public function answer() {
        return 42;
    }
}

has_public_method(Hitchhiker::class, 'answer');

// true

has_public_method(new Hitchhiker, 'answer');

// true

carbon('2017-01-18 11:30');

/*
Carbon\Carbon {
    "date": "2017-01-18 11:30:00.000000",
    "timezone_type": 3,
    "timezone": "UTC",
}
*/

carbon();

/*
Carbon\Carbon {
    "date": "2017-10-27 16:18:00.000000",
    "timezone_type": 3,
    "timezone": "UTC",
}
*/

temporary_file();

/*
[
    "file" => stream resource {
        timed_out: false
        blocked: true
        eof: false
        wrapper_type: "plainfile"
        stream_type: "STDIO"
        mode: "r+b"
        unread_bytes: 0
        seekable: true
        uri: "/tmp/phpxm4bcZ"
        options: []
    }
    "path" => "/tmp/phpxm4bcZ"
]
*/



use SebastiaanLuca\PhpHelpers\Classes\Enum;

class UserStates
{
    use Enum;

    public const REGISTERED = 'registered';
    public const ACTIVATED = 'activated';
    public const DISABLED = 'disabled';
}

UserStates::enums();

// or

(new UserStates)->enums();

/*
[
    "REGISTERED" => "registered",
    "ACTIVATED" => "activated",
    "DISABLED" => "disabled",
]
*/



use SebastiaanLuca\PhpHelpers\Classes\Enum;

class UserStates
{
    use Enum;

    public const REGISTERED = 'registered';
    public const ACTIVATED = 'activated';
    public const DISABLED = 'disabled';
}

UserStates::keys();

/*
[
    "REGISTERED",
    "ACTIVATED",
    "DISABLED",
]
*/



use SebastiaanLuca\PhpHelpers\Classes\Enum;

class UserStates
{
    use Enum;

    public const REGISTERED = 'registered';
    public const ACTIVATED = 'activated';
    public const DISABLED = 'disabled';
}

UserStates::values();

/*
[
    "registered",
    "activated",
    "disabled",
]
*/



namespace Kyle\Helpers;

use SebastiaanLuca\PhpHelpers\Classes\ProvidesClassInfo;

class MyClass
{
    use ProvidesClassInfo;

    public function __construct()
    {
        var_dump($this->getClassDirectory());
    }
}

// "/Users/Kyle/Projects/php-helpers"



class SomeClass
{
    private function aPrivateMethod() : string
    {
        return 'private';
    }

    protected function aProtectedMethod() : string
    {
        return 'protected';
    }

    public function aPublicMethod() : string
    {
        return 'public';
    }
}

MethodHelper::hasMethodOfType($class, 'aPrivateMethod', 'private');

// true

MethodHelper::hasProtectedMethod($class, 'aProtectedMethod');

// true

MethodHelper::hasPublicMethod($class, 'aPublicMethod');

// true

MethodHelper::hasProtectedMethod($class, 'aPrivateMethod');

// false

MethodHelper::hasPublicMethod($class, 'invalidMethod');

// false
bash
composer