1tomany/get-safely 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

1tomany/get-safely

最新稳定版本:v0.0.1

Composer 安装命令:

composer require 1tomany/get-safely

包简介

A simple library to safely get scalar types from values of unknown origin or type

README 文档

README

This simple library exposes several PHP functions that assist with static analysis by ensuring values of unknown or mixed types are a scalar type.

Installation

composer require 1tomany/get-safely

Functions

get_string(): string

This function returns the $value argument if it is a string or implements the \Stringable interface, otherwise it returns the value of the $default argument. If the $default argument is of type callable, the $value is passed to it and the result of the callable is returned. An exception is thrown if the callable does not return a string.

Arguments

  • mixed $value
  • string|callable $default = ''

get_string_loose(): string

This function is identical to get_string() except the $value argument is first cast to a string if it is a scalar value (boolean, int, float, or string in PHP).

Arguments

  • mixed $value
  • string|callable $default = ''

Examples

<?php

use function OneToMany\Getters\get_string;
use function OneToMany\Getters\get_string_loose;

//
// get_string() Examples
//

// string(0) ""
get_string(null);

// string(3) "Vic"
get_string('Vic');

// string(4) "nope"
get_string(null, 'nope');

// string(8) "callable"
get_string(null, fn ($v) => 'callable');

// string(3) "Vic"
get_string(new class('Vic') implements \Stringable {
    function __construct(public readonly string $name)
    {
    }

    public function __toString(): string
    {
        return $this->name;
    }
}, 'Neil');

//
// get_string_loose() Examples
//

// string(0) ""
get_string_loose(null);

// string(1) "1"
get_string_loose(true);

// string(0) ""
get_string_loose(false);

// string(2) "10"
get_string_loose(10);

// string(4) "3.14"
get_string_loose(3.14);

// string(3) "Vic"
get_string_loose([], 'Vic');

Credits

License

The MIT License

统计信息

  • 总下载量: 20
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 3
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-07-10

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固