goldenscarab/modulus-helpers

Laravel和Modulus的不同助手

v1.0.4 2023-03-15 09:50 UTC

README

Laravel和Modulus的不同助手

必需

  • Composer
  • PHP >= 7.2
  • Laravel >= 5.8

安装

使用composer要求这个包。

composer require goldenscarab/modulus-helpers

Laravel 5.8使用自动加载文件,因此不需要您手动添加ServiceProvider。

可用函数

转换

Html

Image

List

Str

Array

Time

Url

Divers

使用

carbonize()

carbonize()函数递归地将日期转换为集合内的Carbon实例

$collection = collect(['id' => 23, 'date' => '12/06/1980']);

$collection_carbonized = carbonize($collection);

// Mixed::class([id => 23, date => \Carbon\Carbon::class])

collectize()

collectize()函数将数组转换为集合内的Collection实例

$collection = collect(['id' => 23, 'collect' => [1, 2, 3, 4]]);

$collection_collectized = collectize($collection);
// Mixed::class([id => 23, collect => Collection::class([1, 2, 3, 4])])

objectize()

objectize()函数将集合的关联数组转换为stdClass

$collection = collect(['id' => 23, 'object' => ['key' => 'foo', 'value' => 'far']]);

$collection_objectized = objectize($collection);
// Mixed::class([id => 23, object => stdClass($key='foo', $value='far')])

csv_to_collection()

csv_to_collection()函数从CSV字符串返回stdClass集合

$csv  = col1, col2, col3 . PHP_EOL;
$csv .= value1, value2, value3;

$collection_objects = csv_to_collection($csv, ',');
// Collection::class([col1 => value1, col2 => value2, col3 => value3])

array_to_string()

array_to_string()函数将数组转换为字符串

$array = ['id', 'name', 'value'];

$string = array_to_string($array);
// "['id', 'name', 'value']"

string_to_float()

string_to_float()函数解析字符串为浮点数

$float = string_to_float('2,45');
// 2.45

$float = string_to_float('42.745');
// 42.745

float_to_money()

float_to_money函数将浮点数转换为带有€符号的货币数值

$money = float_to_money(1234.56);
// 1 234,56 €

published_html()

published_html()函数返回对应发布状态的HTML

$html = published_html(0);
// '<i class="fa fa-eye-slash text-warning"></i>'

$html = published_html(1);
// '<i class="fa fa-globe text-success"></i>'

boolean_html()

boolean_html()函数返回对应布尔状态的HTML

$html = boolean_html(false);
// '<i class="fa fa-times text-danger"></i>'

$html = boolean_html(true);
// '<i class="fa fa-check text-success"></i>'

optimize_image()

optimize_image()函数将文件路径中的图像进行转换和优化

optimize_image('path/to/image.png', $width = 1920, $height = null, $type = 'jpg', $quality = 80);

make_thumb_image()

make_thumb_image()函数将文件路径中的图像转换为缩略图

$path_image_thumb = make_thumb_image('path/to/image.png', $width = 1920, $height = null, $type = 'jpg', $quality = 80);
// "path/to/image_thumb.jpg"

col_sort()

col_sort()函数在视图列表中显示列排序图标

$html = col_sort('name');
// "<span class="sort desc ml-1"><i class="fa fa-sort-amount-desc"></i></span>"

pos_sort()

pos_sort()函数显示用于拖放排序列的视觉元素

$html = pos_sort($item_id = 1, $item_position = 9);
// '<div class="position-wrap">'
// '...'
// '</div>'

str_singularize_fr()

str_singularize_fr()函数将复数字符串转换为单数(法语)

$string = str_singularize_fr("chevaux");
// "cheval"

str_indent()

str_indent()函数缩进字符串

$string  = '<div>';
$string .= '    <p>lorem</p>';
$string .= '</div>';

$string = str_indent($string, 1);
// "    <div>"
// "        <p>lorem</p>"
// "    </div>"

str_args_to_array()

str_args_to_array()函数将参数字符串转换为具有类型约束的参数数组

$array = str_args_to_array("lorem, 2, 45.78, true, 2,47, foo");
// array('lorem', 2, 45.78, true, '2.47', 'foo');

sum_times()

sum_times()函数将格式为hh:mm:ss的字符串数组相加,并显示格式化的总和

$total = sum_times(['12:45:30', '6:12:10'], $segment = 3);
// "18:57:40"

gte_time()

gte_time()函数比较两个时间字符串(较大的或等于)

$total = gte_time("12:50:10", "14:20:05");
// false

current_query_to_array()

current_query_to_array()函数返回当前URL参数的数组

$query = current_query_to_array();
// ['page' => 1254, 'order' => 'desc', 'token' => null]

current_query_to_string()

current_query_to_string()函数返回当前URL参数,不包括空的参数

$query = current_query_to_string();
// ?page=1254&order=desc'

current_route_belongs()

current_route_belongs()函数检查当前URL(包括子URL)是否属于某个路由

$belongs = current_route_belongs('page.list');
// false

current_parent_route_belongs()

函数 current_parent_route_belongs() 检查当前 URL(包括子 URL)是否属于父路由(段 -1)

$belongs = current_parent_route_belongs('page.category.list');
// false

calc_increase_percent()

函数 calc_increase_percent() 计算两个值之间的增长百分比

$increase = calc_increase_percent(130, 160);
// 23.076923077

mixte_get()

函数 mixte_get() 从混合源获取内容,使用“.”表示法进行高级获取

可以传递参数来调用方法

$data = ['products' => ['desk' => ['price' => 100]]];

$price = mixte_get($data, 'products.desk.price', $default = null);
// 100

mixte_get($collection, "where('name', 'toto').first()");
// stdClass::class(name='toto')

mixte_get_multi()

函数 mixte_get_multi() 从一个键数组中获取来自源的数据数组

可以传递参数来调用方法

$source = [
    'content' => [
        'key'   => "my-key",
        'value' => "my-value"
    ]
]
$getters = ['content.key', 'content.value'];

$price = mixte_get_multi($source, $getters, $default = null);
// ["my-key", "my-value"]

public_folder_to_urls()

函数 public_folder_to_urls() 从一个相对的公共路径中获取文件 URL 集合

$urls = public_folder_to_urls("upload/folder/");
// Collection(['http://domain.tld/upload/folder/images1.png', 'http://domain.tld/upload/folder/images2.png'])

is_indexed_array()

函数 is_indexed_array() 测试一个数组是否为索引数组或关联数组

is_indexed_array(['foo', 'bar']);
// true

is_indexed_array(['foo' => 'bar', 'boo' => 'tite']);
// false

array_contains()

函数 array_contains() 测试一个数组是否包含指定的多个值

array_contains(['foo', 'boo'], ['foo', 'bar', 'boo']);
// true

array_contains(['foo', 'tuu'], ['foo', 'bar', 'boo']);
// false

time_to_float()

函数 time_to_float() 将 HH:MM:SS 类型的字符串转换为浮点数

time_to_float('1:23:45');
// 1.3958333333333

安全

如果您发现任何与安全相关的问题,请发送电子邮件到 contact@goldenscarab.fr 而不是使用问题跟踪器。

致谢

许可协议

MIT 许可协议(MIT)。有关更多信息,请参阅 许可文件