承接 rfx/cast 相关项目开发

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

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

rfx/cast

Composer 安装命令:

composer require rfx/cast

包简介

Cast / convert a stdClass or any other class to another class

README 文档

README

Minimum PHP version: 7.4.0

ABOUT

Allows you to cast a stdClass (or any other class) to some other class.

INSTALL

composer require rfx/cast

USAGE

Problem

So you received a beautiful object that you want to use

stdClass Object
(
    [name] => home
    [at] => stdClass Object
        (
            [x] => 4
            [y] => 5
        )
)

However:

  • Your IDE hates it
  • All static analysis tools hate it
  • No autocompletion
  • No type checks

Solution

declare(strict_types=1);
// Create class(es) that define(s) those properties
use rfx\Type\Cast;
class Point {
    public int $x;
    public int $y;
}

class Location {
    public string $name;
    public Point $at;
}

function getLocation(): Location {
    // Get your object (from a json source, some external lib, ...).
    // As demonstration we create one here from an array.
    $obj = (object)['name' => 'home', 'at' => ['x' => 4, 'y' => 5]];
    // Cast it (this works recursively, e.g. Location contains a Point object)
    return Cast::as($obj, Location::class);
}

$l = getLocation();

Result

Location Object
(
    [name] => home
    [at] => Point Object
        (
            [x] => 4
            [y] => 5
        )
)
  • Your IDE loves you again
  • All static analysis tools will help you find problems
  • Autocompletion, type checks, etc. work again

Speed

If performance is important and:

  • you are casting many objects of the same type
  • all your objects are shallow (no nesting)
  • all the properties are scalar types

then you can use the faster alternative, which is as performant as a normal constructor:

declare(strict_types=1);
// Create a proper class that defines those properties
use rfx\Type\Cast;
class Point {
    public string $name;
    public int $x;
    public int $y;
}

/** @return Point[] */
function getPoints(): array {
    // Get your objects (from a json source, some external lib, ...).
    // As demonstration we create a million from an array.
    $obj = (object)['name' => 'P1', 'x' => 4, 'y' => 5];
    $objs = array_fill(0, 1000000, $obj);
    // Create a factory
    $cf = new Cast(Point::class);
    // Cast them all
    return array_map([$cf, 'cast'], $objs);
}

$p = getPoints();

LIMITATIONS

This section needs to be written, and some future plans added.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: LGPL-2.1-only
  • 更新时间: 2022-05-16

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固