lyhty/support

为您的Laravel项目添加一些非常方便的辅助工具

v2.2.0 2024-09-21 19:24 UTC

This package is auto-updated.

Last update: 2024-09-21 19:25:32 UTC


README

Latest Stable Version PHP Laravel Total Downloads License

此包为您的Laravel项目提供了一些额外的、方便使用的辅助工具。

安装

使用Composer安装此包

composer require lyhty/support

功能

辅助工具

  • class_uses_trait($class, $trait, bool $recursive = true): bool
    • 返回一个布尔值,表示给定的类是否使用了给定的特质。
  • array_depth(array $array)
    • 返回一个整数,表示给定数组的最大深度。
  • class_implements_interface($class, $interface): bool
    • 返回一个布尔值,表示给定的类是否实现了给定的接口。
  • class_extends($class, $parent): bool
    • 返回一个布尔值,表示给定的类是否扩展了给定的父类。
  • set_type($value, $type)
    • settype的别名,允许将非变量作为参数。
  • trim_spaces(string $string): string
    • 从字符串中去除空格。
  • not_null($var): bool
    • !is_null
  • get_bool($value): bool
    • 从给定的值获取布尔值。接受字符串true/false。
  • class_namespace(string $className): string
    • 获取给定类的命名空间。
  • ___(array $keys, array $replace = [], array $numbers = [], string $locale = null, string $glue = ' '): string
    • 翻译给定消息并将它们连接起来。

发现类

这基本上是从 Illuminate\Foundation\Events\DiscoverEvents 复制的,但更加通用。

示例

use Lyhty\Support\Discovery;

$all = Discovery::within('app\Models')->toArray();
// ["App\Models\User", "App\Models\BlogPost", "App\Models\Concerns\Taggable", "App\Models\Contracts\BlogWriter"]

$classes = Discovery::classesWithin('app\Models')->toArray();
// ["App\Models\User", "App\Models\BlogPost"]

$traits = Discovery::traitsWithin('app\Models')->toArray();
// ["App\Models\Concerns\Taggable"]

$interfaces = Discovery::interfacesWithin('app\Models')->toArray();
// ["App\Models\Contracts\BlogWriter"]

$usingClasses = Discovery::usesWithin('app\Models', 'App\Models\Concerns\Taggable')->toArray();
// ["App\Models\BlogPost"]

$implementingClasses = Discovery::implementsWithin('app\Models', 'App\Models\Contracts\BlogWriter')->toArray();
// ["App\Models\User"]

$extendingClasses = Discovery::extendsWithin('app\Models', 'Illuminate\Database\Eloquent\Model')->toArray();
// ["App\Models\User", "App\Models\BlogPost"]

许可证

Lyhty Support 是开源软件,使用 MIT 许可证授权。详情请参阅MIT 许可证