takemo101/simple-vm 问题修复 & 功能扩展

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

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

takemo101/simple-vm

最新稳定版本:v0.1.4

Composer 安装命令:

composer require takemo101/simple-vm

包简介

Simple ViewModel Object

README 文档

README

Testing PHPStan PHPStan

The Simple VM allows you to simply create a ViewModel object.
By using the ViewModel object, you can convey appropriate data to the View.
Enjoy!

How to use

Please use as follows

Basic

use Takemo101\SimpleVM\ViewModel;

/**
 * Create a class that inherits from the ViewModel class
 */
class TestViewModel extends ViewModel
{
    // Set methods and property names that are not reflected in this property
    protected array $__ignores = [
        'd',
    ];

    /**
     * constructor
     *
     * @param string $a
     * @param integer $b
     */
    public function __construct(
        public string $a,
        private int $b,
    ) {
        // public property is reflected in View
    }

    /**
     * public method is reflected in View
     *
     * @return string
     */
    public function c(): string
    {
        return 'C';
    }

    /**
     * @return string
     */
    public function d(): string
    {
        return 'D';
    }

    /**
     * Set the initial value to the __data method
     *
     * @return mixed[]
     */
    public function __data(): array
    {
        return [
            'e' => 'E',
        ];
    }
}

$model = new TestViewModel('A', 2);

var_dump($model->toArray());
// array(3) {
//   'e' =>
//   string(1) "E"
//   'a' =>
//   string(1) "A"
//   'c' =>
//   string(1) "C"
// }

PHP Attribute

You can use the PHP Attribute class.

use Takemo101\SimpleVM\ViewModel;
use Takemo101\SimpleVM\Attribute\{
    Ignore,
    ChangeName,
};

/**
 * Create a class that inherits from the ViewModel class
 */
class TestAttributeViewModel extends ViewModel
{
    /**
     * Not output when Ignore class is set in a property or method
     *
     * @param string $a
     */
    public function __construct(
        #[Ignore]
        public string $a,
    ) {
        // public property is reflected in View
    }

    /**
     * Setting the ChangeName class on a property or method will rename the data
     *
     * @return string
     */
    #[ChangeName('cc')]
    public function c(): string
    {
        return 'C';
    }
}

$model = new TestAttributeViewModel('A');

var_dump($model->toArray());
// array(3) {
//   'cc' =>
//   string(1) "C"
// }

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-02-17