hyperf-view/builder/Abstracts/AbstractMapper.php

65 lines
1.3 KiB
PHP

<?php
/**
* MineAdmin is committed to providing solutions for quickly building web applications
* Please view the LICENSE file that was distributed with this source code,
* For the full copyright and license information.
* Thank you very much for using MineAdmin.
*
* @Author X.Mo<root@imoi.cn>
* @Link https://gitee.com/xmo/MineAdmin
*/
declare (strict_types = 1);
namespace Builder\Abstracts;
use Hyperf\Context\Context;
use Builder\Traits\MapperTrait;
use Builder\BaseModel;
/**
* Class AbstractMapper
* @package Builder\Abstracts
*/
abstract class AbstractMapper
{
use MapperTrait;
/**
* @var BaseModel
*/
public $model;
abstract public function assignModel();
public function __construct()
{
$this->assignModel();
}
/**
* 把数据设置为类属性
* @param array $data
*/
public static function setAttributes(array $data)
{
Context::set('attributes', $data);
}
/**
* 魔术方法,从类属性里获取数据
* @param string $name
* @return mixed|string
*/
public function __get(string $name)
{
return $this->getAttributes()[$name] ?? '';
}
/**
* 获取数据
* @return array
*/
public function getAttributes(): array
{
return Context::get('attributes', []);
}
}