All Projects → sebastiaanluca → php-helpers

sebastiaanluca / php-helpers

Licence: MIT license
An extensive set of PHP helper functions and classes.

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to php-helpers

Kind Of
Get the native JavaScript type of a value, fast. Used by superstruct, micromatch and many others!
Stars: ✭ 268 (+892.59%)
Mutual labels:  string, object, array
utils.js
👷 🔧 zero dependencies vanilla JavaScript utils.
Stars: ✭ 14 (-48.15%)
Mutual labels:  string, object, array
prototyped.js
Some common Typescript prototypes
Stars: ✭ 22 (-18.52%)
Mutual labels:  string, object, array
type-reverse
🦄 Lightweight reverse utility around strings, arrays, numbers and more.
Stars: ✭ 30 (+11.11%)
Mutual labels:  string, array
js-explorer
Find the method you need without digging through the docs, directly on the command line!
Stars: ✭ 287 (+962.96%)
Mutual labels:  object, array
rxjs-ninja
RxJS Operators for handling Observable strings, numbers, booleans and more
Stars: ✭ 68 (+151.85%)
Mutual labels:  string, array
Mlib
Library of generic and type safe containers in pure C language (C99 or C11) for a wide collection of container (comparable to the C++ STL).
Stars: ✭ 321 (+1088.89%)
Mutual labels:  string, array
Android interviews
🚀Everything you need to know to find a android job. 算法 / 面试题 / Android 知识点 🔥🔥🔥 总结不易,你的 star 是我最大的动力!
Stars: ✭ 510 (+1788.89%)
Mutual labels:  string, array
Cracking The Coding Interview
Solutions for Cracking the Coding Interview - 6th Edition
Stars: ✭ 35 (+29.63%)
Mutual labels:  string, array
Blitz
Android Library: Set self-updating string with relative time in TextView (e.g. 5 minutes ago)
Stars: ✭ 217 (+703.7%)
Mutual labels:  string, datetime
Algo Tree
Algo-Tree is a collection of Algorithms and data structures which are fundamentals to efficient code and good software design. Creating and designing excellent algorithms is required for being an exemplary programmer. It contains solutions in various languages such as C++, Python and Java.
Stars: ✭ 166 (+514.81%)
Mutual labels:  string, array
Data-Structure-Algorithm-Programs
This Repo consists of Data structures and Algorithms
Stars: ✭ 464 (+1618.52%)
Mutual labels:  string, array
Stringfication
🔨 Make all objects to String!
Stars: ✭ 33 (+22.22%)
Mutual labels:  string, object
is-string
Is this value a JS String object or primitive? This module works cross-realm/iframe, and despite ES6 @@toStringTag.
Stars: ✭ 17 (-37.04%)
Mutual labels:  string, object
DS ALGO
Data Structures and algorithms
Stars: ✭ 20 (-25.93%)
Mutual labels:  string, array
obj-to-table
Create a table from an array of objects
Stars: ✭ 15 (-44.44%)
Mutual labels:  string, object
DataTypes
Built-in data types
Stars: ✭ 34 (+25.93%)
Mutual labels:  string, object
Competitive Programming
Programming👨‍💻 Questions on BinarySearch💻, LeetCode💻, CodeChef💻, Codeforces💻,DSA 450
Stars: ✭ 188 (+596.3%)
Mutual labels:  string, array
Golang Combinations
Golang library which provide an algorithm to generate all combinations out of a given string array.
Stars: ✭ 51 (+88.89%)
Mutual labels:  string, array
js-deep-sort-object
Simple module to sort objects recursively by its keys
Stars: ✭ 19 (-29.63%)
Mutual labels:  object, array

An extensive set of PHP helper functions and classes

Latest stable release Software license Build status Total downloads Total stars

Read my blog View my other packages and projects Follow @sebastiaanluca on Twitter Share this package on Twitter

Table of contents

Requirements

  • PHP 8 or higher

How to install

Via Composer:

composer require sebastiaanluca/php-helpers

All function helpers will be enabled by default (if those functions haven't already been defined). Class helpers are enabled per-case when used.

You can find more info on how to use a helper and what there requirements are in their respective section (see the table of contents above for an overview).

Global helper functions

rand_bool

Randomly return true or false.

rand_bool();

// true

str_wrap

Wrap a string with another string.

str_wrap('foo', '*');

// "*foo*"

is_assoc_array

Check if an array is associative.

Performs a simple check to determine if the given array's keys are numeric, start at 0, and count up to the amount of values it has.

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

Expand a flat dotted array into a multi-dimensional associative array.

If a key is encountered that is already present and the existing value is an array, each new value will be added to that array. If it's not an array, each new value will override the existing one.

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

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

array_without

Get the array without the given values.

Accepts either an array or a value as parameter to remove.

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

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

// ["mercedes"]
array_without(['one', 'two', 'three'], 'two');

// ["one", "three"]

array_pull_value

Pull a single value from a given array.

Returns the given value if it was successfully removed from the source array or null if it was not found.

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

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

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

array_pull_values

Pull an array of values from a given array.

Returns the values that were successfully removed from the source array or an empty array if none were found.

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

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

array_hash

Create a unique string identifier for an array.

The identifier will be entirely unique for each combination of keys and values.

array_hash([1, 2, 3]);

// "262bbc0aa0dc62a93e350f1f7df792b9"
array_hash(['hash' => 'me']);

// "f712e79b502bda09a970e2d4d47e3f88"

object_hash

Create a unique string identifier for an object.

Similar to array_hash, this uses serialize to stringify all public properties first. The identifier will be entirely unique based on the object class, properties, and its values.

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

object_hash(new ValueObject);

// "f39eaea7a1cf45f5a0c813d71b5f2f57"

has_public_method

Check if a class has a certain public method.

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

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

// true

has_public_method(new Hitchhiker, 'answer');

// true

carbon

Create a Carbon datetime object from a string or return a new object referencing the current date and time.

Requires the nesbot/carbon package.

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

Create a temporary file.

Returns an array with the file handle (resource) and the full path as string.

The temporary file is readable and writeable by default. The file is automatically removed when closed (for example, by calling fclose() on the handle, or when there are no remaining references to the file handle), or when the script ends.

See for more information.

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"
]
*/

Class helpers

Enum trait

The primary use of the Enum trait is to enable you to store all cases of a specific type in a single class or value object and have it return those with a single call.

This can be useful for instance when your database uses integers to store states, but you want to use descriptive strings throughout your code (i.e. enums). It also allows you to refactor these elements at any time without having to waste time searching your code for any raw values (and probably miss a few, introducing new bugs along the way).

Retrieving elements

Returns an array of element keys and their values.

<?php

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",
]
*/

Retrieving element keys

Returns all the keys of the elements in an enum.

<?php

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",
]
*/

Retrieving constant values

Returns all the values of the elements in an enum.

<?php

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",
]
*/

ProvidesClassInfo trait

The ProvidesClassInfo trait provides an easy-to-use getClassDirectory() helper method that returns the directory of the current class.

<?php

namespace Kyle\Helpers;

use SebastiaanLuca\PhpHelpers\Classes\ProvidesClassInfo;

class MyClass
{
    use ProvidesClassInfo;

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

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

MethodHelper

A static class helper to help you figure out the visibility/accessibility of an object's methods.

<?php

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

License

This package operates under the MIT License (MIT). Please see LICENSE for more information.

Change log

Please see CHANGELOG for more information what has changed recently.

Testing

composer install
composer test

Contributing

Please see CONTRIBUTING and CODE OF CONDUCT for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

About

My name is Sebastiaan and I'm a freelance back-end developer specializing in building custom Laravel applications. Check out my portfolio for more information, my blog for the latest tips and tricks, and my other packages to kick-start your next project.

Have a project that could use some guidance? Send me an e-mail at [email protected]!

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].