Giter VIP home page Giter VIP logo

php-object-mapping's Introduction

php-object-mapping

说明

PHP对象映射,可以将array/object映射为类对象。可做参数的传递、Request参数映射、Entity、Bean等。并可根据定义的映射对象生成API文档。

示例

class TestMapping extends BaseMapping
{
    /** @var int 总数 */
    public int $total;

    public ?TestItem $item = null;

    /** @var \Xbyter\PhpObjectMappingTests\Data\TestItem[] 列表对象 */
    public array $list = [];

    /** @var string 受保护的值,不会被设置 */
    protected string $protected_key = 'protected value';
}

class TestItem extends BaseMapping
{
    /** @var string|null 标题 */
    public ?string $title = null;

    /**
     * @var string|null 会返回装饰过的值
     * @return TitleDecorator
     */
    public ?string $decorate_title = null;
}

//装饰类
class TitleDecorator implements DecoratorInterface
{

    public function decorate($value)
    {
        return sprintf("this is a decorated value: %s", $value);
    }
}

//映射对象,多维数组可用 TestMapping::fromList([[...], [...]]);
$testMapping = TestMapping::fromItem([
    'total' => 10,
    'item'  => [
        'title' => 'a'
    ],
    'list'  => [
        [
            'title' => 'b',
            'decorate_title' => 'c',
        ]
    ],
    'protected_key' => '受保护的字段不会被赋值',
]);

//转为数组
$array = $testMapping->toArray();

var_dump($testMapping->total); //10
var_dump($testMapping->item->title); //a
var_dump($testMapping->list[0]->title); //b
var_dump($testMapping->list[0]->decorate_title); //this is a decorated value: c
var_dump($testMapping->protected_key); //Error : Cannot access protected property $protected_key

php-object-mapping's People

Contributors

xbyter avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.