Download the PHP package hsldymq/velcro without Composer

On this page you can find all versions of the php package hsldymq/velcro. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package velcro

Velcro

tests codecov

简介

这个库帮助你用更少的代码自动将关联数组的值附给对象的属性.

什么场景下我会需要这种东西?

当你需要从接口请求参数来创建DTO; 当你需要通过对象的形式来管理配置; 抑或者从外部数据源得到的数据恢复成对象时。

从这些预先定义好的数据恢复时,往往就需要大量无趣的组装操作,通常还伴随着一些前置条件逻辑,比如判断字段是否存在,类型是否需要转换。

用代码生成工具是一种减少人力的方法。 而这个库是另一种方法,它让你只需要定义类属性所关联的字段,剩下的组装操作由库来完成。

要求

PHP >= 8.0

安装

使用

基本用法 (Basic)

这里展示一个最基本的用法.

你至少需要用到两个类, \Archman\Velcro\DataModel\Archman\Velcro\Field

前者作为基类被你的数据模型类继承, 后者利用PHP 8 Attribute特性,用来关联你的类属性与其对应数组键名.

简短的几句声明之后,你就可以把数组作为构造参数传给Foo构造函数了,Foo内部在帮你做了类似这样的事情:

类型匹配

尽管内部帮你解决了赋值的问题, 但是它不会帮你匹配类型,更不会自动帮你转换类型, 所以当你的类属性和数据字段的类型不一致时,会抛出异常,因为Velcro的类都是以strict_types模式定义的.

数据转换器 (Converter)

然而你定义的类属性,可能是任意类型. 同时你的数据可能不是来自于程序内部,有可能是http请求/响应体,或者外部配置/存储,异步消息等,双方的类型可能并不匹配.

此时,你需要用到数据转换器, 把来源数据转换成其对应属性的类型, 这样你可按照使用上更舒服的方式去定义类的属性.

使用数据转换器 (Using Converter)

当我们从第三方接口获得数据中包含了一个时间戳的字段,比如$data['time'],而在我们的代码中需要以DateTimeImmutable类型来使用, 与其我们手动的编写转换代码, 我们可以这样定义

Velcro会先使用DateTimeConverter帮你把时间戳转换成 DateTimeImmutable 类型再赋给对应的属性.

Velcro中预先定义了一些的转换器,用来应对不同的场景.

嵌套DataModel

你可以使用ModelConverterModelListConverter这两个转换器,实现数据模型的嵌套.

通过实现ConverterInterface接口,你可以实现自己的数据转换器

只读属性 (Readonly Property)

你可能需要你的模型属性是只读的, 但是只读属性在PHP 8.0下并非一个语言特性, 因此你可以对类或属性标记RO来达到目的

因为PHP 8.1开始增加了readonly关键字,所以当你的环境使用8.1以及之后的版本,你可以抛弃RO直接使用readonly关键来标记只读属性来达到相同的目的

私有属性

在上面的例子中,都是使用public属性进行演示. 但实际上Velcro同样能赋值给protected和private属性


All versions of velcro with dependencies

PHP Build Version
Package Version
Requires php Version ^8.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package hsldymq/velcro contains the following files

Loading the files please wait ....