承接 jojo1981/php-types 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

jojo1981/php-types

最新稳定版本:4.1.0

Composer 安装命令:

composer require jojo1981/php-types

包简介

PHP types library which contains value classes which represent the php types

README 文档

README

Build Status Coverage Status Latest Stable Version Total Downloads License

Author: Joost Nijhuis <jnijhuis81@gmail.com>

This library has some value classes which represent a PHP type. These value classes can be constructed based on the PHP type name or based on a value. There are 2 pseudo type classes: Jojo1981\PhpTypes\MixedType and Jojo1981\PhpTypes\MultiType which will function as composite. Also, an abstract factory class is included: Jojo1981\PhpTypes\AbstractType.

The following types are supported, with their aliases:

  • array
  • bool (boolean)
  • callable (callback)
  • class
  • float (number, real, double)
  • int (integer)
  • iterable
  • null
  • object
  • resource
  • string (text)
  • void

Pseudo types:

  • mixed
  • multi

Installation

Library

git clone https://github.com/jojo1981/php-types.git

Composer

Install PHP Composer

composer require jojo1981/php-types

Basic usage

<?php

require 'vendor/autoload.php';

$integerType = \Jojo1981\PhpTypes\AbstractType::createFromTypeName('int');
$integerType->isAssignableValue('text'); // will return false
$integerType->isAssignableValue(28); // will return true

$floatType1 = \Jojo1981\PhpTypes\AbstractType::createFromValue(5.0);
$floatType1->isAssignableValue(true); // will return false
$floatType1->isAssignableValue('text'); // will return false
$floatType1->isAssignableValue(10); // will return false
$floatType1->isAssignableValue(10.0); // will return true

$floatType2 = \Jojo1981\PhpTypes\AbstractType::createFromTypeName('number');
$floatType2->isAssignableValue(true); // will return false
$floatType2->isAssignableValue('text'); // will return false
$floatType2->isAssignableValue(1); // will return false
$floatType2->isAssignableValue(1.0); // will return true

$stringType = \Jojo1981\PhpTypes\AbstractType::createFromValue('string');
$stringType->isAssignableValue(true); // will return false
$stringType->isAssignableValue('text'); // will return true

// Other types: array, bool, callable, iterable, null, resource, void

// Object | Class types

$objectType = \Jojo1981\PhpTypes\AbstractType::createFromTypeName('object');
$objectType->isAssignableValue(true); // will return false
$objectType->isAssignableValue(new \stdClass()); // will return true
$objectType->isAssignableValue(new \Jojo1981\PhpTypes\TestSuite\Fixture\TestEntity()); // will return true (for all object types)

$classType1 = \Jojo1981\PhpTypes\AbstractType::createFromValue(new \Jojo1981\PhpTypes\TestSuite\Fixture\TestEntity());
$classType1->isAssignableValue(new \stdClass()); // will return false
$classType1->isAssignableValue(new \Jojo1981\PhpTypes\TestSuite\Fixture\TestEntity()); // will return true

$classType2 = \Jojo1981\PhpTypes\AbstractType::createFromTypeName(\Jojo1981\PhpTypes\TestSuite\Fixture\TestEntity::class);
$classType2->isAssignableValue(new \stdClass()); // will return false
$classType2->isAssignableValue(new \Jojo1981\PhpTypes\TestSuite\Fixture\TestEntityBase); // will return false
$classType2->isAssignableValue(new \Jojo1981\PhpTypes\TestSuite\Fixture\TestEntity()); // will return true

$classType3 = \Jojo1981\PhpTypes\AbstractType::createFromTypeName(\Jojo1981\PhpTypes\TestSuite\Fixture\InterfaceTestEntity::class);
$classType3->isAssignableValue(new \stdClass()); // will return false
$classType3->isAssignableValue(new \Jojo1981\PhpTypes\TestSuite\Fixture\TestEntityBase); // will return true
$classType3->isAssignableValue(new \Jojo1981\PhpTypes\TestSuite\Fixture\TestEntity()); // will return true

// Pseudo types
$mixedType = \Jojo1981\PhpTypes\AbstractType::createFromTypeName('mixed');
$mixedType->isAssignableValue(null); // will return true (always)
$mixedType->isPseudoType(); // will return true

$multiType = \Jojo1981\PhpTypes\AbstractType::createFromTypes([$stringType, $integerType]);
$multiType->isAssignableValue(true); // will return false
$multiType->isAssignableValue('text'); // will return true
$multiType->isAssignableValue(28); // will return true
$multiType->isPseudoType(); // will return true

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-01-07