sebastiaanluca/php-helpers

该软件包已被废弃,不再维护。未建议替代软件包。

一组广泛的PHP辅助函数和类。

3.0.0 2021-03-11 13:13 UTC

This package is auto-updated.

Last update: 2023-02-06 03:17:39 UTC


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

目录

需求

  • PHP 8 或更高版本

如何安装

通过Composer

composer require sebastiaanluca/php-helpers

所有函数辅助默认启用(如果那些函数尚未定义)。类辅助按需启用。

您可以在各自的章节中找到有关如何使用辅助函数及其要求的信息(请参阅上面的目录以获取概述)。

全局辅助函数

rand_bool

随机返回truefalse

rand_bool();

// true

str_wrap

用另一个字符串包装一个字符串。

str_wrap('foo', '*');

// "*foo*"

is_assoc_array

检查一个数组是否为关联数组。

进行简单检查以确定给定数组的键是否为数字,从0开始,并向上计数到其值的数量。

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

将扁平点数组展开成多维关联数组。

如果遇到已存在的键,并且现有值为数组,则每个新值将被添加到该数组中。如果不是数组,则每个新值将覆盖现有值。

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

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

array_without

获取不包含给定值的数组。

接受数组或要移除的值作为参数。

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

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

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

// ["one", "three"]

array_pull_value

从给定数组中拉出一个值。

如果成功从源数组中移除给定值,则返回该值,如果没有找到,则返回null

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

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

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

array_pull_values

从给定数组中拉出一个值数组。

返回成功从源数组中删除的值,如果没有找到则返回空数组。

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

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

array_hash

为数组创建一个唯一的字符串标识符。

标识符将针对每个键和值的组合完全唯一。

array_hash([1, 2, 3]);

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

// "f712e79b502bda09a970e2d4d47e3f88"

object_hash

为对象创建一个唯一的字符串标识符。

类似于array_hash,这首先使用serialize将所有公共属性转换为字符串。标识符将基于对象类、属性及其值完全唯一。

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

object_hash(new ValueObject);

// "f39eaea7a1cf45f5a0c813d71b5f2f57"

has_public_method

检查一个类是否具有某个公共方法。

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

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

// true

has_public_method(new Hitchhiker, 'answer');

// true

carbon

从一个字符串创建一个Carbon日期时间对象,或者返回一个引用当前日期和时间的新的对象。

需要nesbot/carbon包。

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

创建一个临时文件。

返回一个包含文件句柄(资源)和完整路径的字符串数组。

默认情况下,临时文件可读可写。文件在关闭时(例如,通过在句柄上调用fclose(),或者当没有剩余的文件句柄引用时)或脚本结束时自动删除。

有关更多信息,请参阅

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

类辅助工具

Enum trait

Enum特质的 主要用途是允许你在单个类或值对象中存储特定类型的所有情况,并通过单个调用返回这些情况。

这在数据库使用整数存储状态但你在代码中想使用描述性字符串时很有用(即枚举)。它还允许你在任何时候重构这些元素,而无需浪费时间搜索代码中的任何原始值(并可能错过一些,引入新的错误)。

检索元素

返回一个包含元素键和其值的数组。

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

检索元素键

返回枚举中元素的键。

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

检索常量值

返回枚举中元素的值。

<?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

ProvidesClassInfo特质提供了一个易于使用的getClassDirectory()辅助方法,该方法返回当前类的目录。

<?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

一个静态类辅助工具,帮助你确定对象的方法的可见性/可访问性。

<?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

许可证

此软件包在MIT许可证(MIT)下运行。有关更多信息,请参阅LICENSE

变更日志

请参阅变更日志获取更多关于最近更改的信息。

测试

composer install
composer test

贡献

请参阅贡献指南行为准则以获取详细信息。

安全

如果您发现任何安全相关的问题,请通过电子邮件hello@sebastiaanluca.com联系,而不是使用问题跟踪器。

鸣谢

关于

我叫Sebastiaan,是一名专注于构建自定义Laravel应用程序的自由职业后端开发者。查看我的作品集获取更多信息,我的博客获取最新的技巧和窍门,以及我的其他以启动您的下一个项目。

您有一个需要指导的项目吗?请发送电子邮件至hello@sebastiaanluca.com