承接 sebastiaanluca/php-helpers 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

sebastiaanluca/php-helpers

最新稳定版本:3.0.0

Composer 安装命令:

composer require sebastiaanluca/php-helpers

包简介

An extensive set of PHP helper functions and classes.

README 文档

README

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 hello@sebastiaanluca.com 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 hello@sebastiaanluca.com!

sebastiaanluca/php-helpers 适用场景与选型建议

sebastiaanluca/php-helpers 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 19.56k 次下载、GitHub Stars 达 27, 最近一次更新时间为 2018 年 07 月 22 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「helpers」 「array」 「object」 「class」 「datetime」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 sebastiaanluca/php-helpers 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 sebastiaanluca/php-helpers 我们能提供哪些服务?
定制开发 / 二次开发

基于 sebastiaanluca/php-helpers 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 19.56k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 27
  • 点击次数: 11
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 27
  • Watchers: 2
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-07-22