mawebdk/simple-datatypes 问题修复 & 功能扩展

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

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

mawebdk/simple-datatypes

最新稳定版本:4.0.0

Composer 安装命令:

composer require mawebdk/simple-datatypes

包简介

Simple datatypes with value restrictions.

README 文档

README

Simple datatypes with value restrictions.

Use these classes instead of PHP's own internal datatypes to ensure values in parameters are within defined limitations.

Usage of MAInteger

Create a class extending MAInteger and implement method isValidValue() returning whether the given integer value is valid or not. Throw an MAIntegerException if unable to decide if the given integer value is valid or not.

Example of an integer class with integers in the range 1-65535 as the defined limitations.

class MyIntegerClass extends MAInteger
{
    public static function isValidValue(int $value): bool
    {
        return (($value >= 1) && ($value <= 65535));
    }
}

Check if a given integer value is valid for the created class.

if (MyIntegerClass::isValidValue(value: $value)):
    // $value is a valid value for MyIntegerClass.
else:
    // $value is not a valid value for MyIntegerClass.
endif; 

Constructors and usage. Note: The method createFromValue will return null if the given value is null.

try {
    $myInteger1 = new MyIntegerClass(value: 123);
    $myInteger2 = MyIntegerClass::createFromValue(value: 456);
    $myInteger3 = MyIntegerClass::createFromValue(value: null);
} catch (MAIntegerException $e) {
    // Error handling.
}

echo $maInteger1->value;   // 123
echo $maInteger2->value;   // 456.
echo $maInteger3;          // null

MAInteger implements Stringable with the primary purpose to simplify logging and generate exception messages.

$myInteger = new MyIntegerClass(value: 123);

echo (string)$maInteger;   // MyIntegerClass{"value": 123}

Usage of MAString

Create a class extending MAString and implement method isValidValue() returning whether the given string value is valid or not. Throw an MAStringException if unable to decide if the given string value is valid or not.

Example of a string class for strings with a length between 1 and 100.

class MyStringClass extends MAString
{
    public static function isValidValue(string $value): bool
    {
        return (strlen($value) >= 1) && (strlen($value) <= 100);
    }
}

Check if a given string value is valid for the created class.

if (MyStringClass::isValidValue(value: $value)):
    // $value is a valid value for MyStringClass.
else:
    // $value is not a valid value for MyStringClass.
endif; 

Constructors and usage. Note: The method createFromValue will return null if the given value is null.

try {
    $myString1 = new MyStringClass(value: 'Hello');
    $myString2 = MyStringClass::createFromValue(value: 'World);
    $myString3 = MyStringClass::createFromValue(value: null);
} catch (MAStringException $e) {
    // Error handling.
}

echo $maString1->value;   // Hello
echo $maString2->value;   // World
echo $maString3           // null

MAString implements Stringable with the primary purpose to simplify logging and generate exception messages.

$myString = new MyStringClass(value: 'Hello World');

echo (string)$maString;   // MyStringClass{"value": "Hello World"}

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: BSD-2-Clause
  • 更新时间: 2025-02-24

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固