touki/populator 问题修复 & 功能扩展

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

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

touki/populator

Composer 安装命令:

composer require touki/populator

包简介

A simple Array-to-Object library

README 文档

README

Build Status

PHP Populator is a simple Array-To-Object library which transforms arrays into a given object with a few additions with Annotations.
This is not a deserializer. If you're looking for a serializer/deserializer, I advise you to use jms/serializer.

The simple goal of this library is to use a lightweight (still object oriented) solution to hydrate an object from a given array without the overkill of a full serializer.
Plus, behaviour can be easily modified (decoupled), and it's unit tested!

# Documentation Summary

Installation

Installation with composer

The easiest way to use this library is to use Composer Just add the following lines into your composer.json

{
    "require": {
        "touki/populator": "~1.0.0"
    }
}

And run

composer update

Setup

Basic setup

This library uses the PSR-0 autoloading mechanism, if you're using composer, there is nothing to do, the class should be autolaoded already.

In order to start using the populator, you just need to create an instance of it.

<?php

use Touki\Populator\Populator;

$populator = new Populator;

?>

However if you're getting an error

[Semantical Error] The annotation "..." does not exist, or could not be auto-loaded.

You should add the following lines at the top of your application.
This allows doctrine to know how to import your annotations from its own autoloader.

<?php

$loader = require '/path/to/app/vendor/autoload.php'; // Composer autoloader

use Doctrine\Common\Annotations\AnnotationRegistry;

AnnotationRegistry::registerLoader(array($loader, 'loadClass'));

?>

And, that's it! You can already already start using the library. See Usage for more informations

Advanced setup

The populator is magically creating two instances in its constructor if none are given.
It needs an instance of HydratorInterface (Will hydrate the object on a given context) and an instance of HydratorContextFactoryInterface (Will create a context based on reflection of a given object)

To reproduce the default behaviour you can do something like this

<?php

use Touki\Populator\Populator;
use Touki\Populator\Hydrator;
use Touki\Populator\HydratorContextFactory;
use Doctrine\Common\Annotations\AnnotationReader;

/**
 * You can instanciate your own as long as it implements HydratorInterface
 */
$hydrator = new Hydrator;

/**
 * You can instanciate your own as long as it implements HydratorContextFactoryInterface
 * The default one accepts Any Doctrine's Annotation Reader (Like FileCacheReader)
 *
 * @see https://github.com/doctrine/annotations/tree/master/lib/Doctrine/Common/Annotations
 */
$factory = new HydratorContextFactory(new AnnotationReader);

$populator = new Populator($hydrator, $factory);

?>

Usage

Simple usage

Say with have a Foo class

<?php

namespace Acme\Model\Foo;

class Foo
{
    protected $bar;
    public $public;
    public $publicWithSetter;

    public function setBar($bar)
    {
        $this->bar = $bar;
    }

    public function getBar()
    {
        return $this->bar;
    }

    public function setPublicWithSetter($var)
    {
        $this->publicWithSetter = $var;
    }
}

$data = array(
    'bar' => 'Foobaz!',
    'public' => 'Public!'
    'publicWithSetter' => 'BySetter'
);

/**
 * You can give either classname or an instance
 */
$foo = new Acme\Model\Foo;
$foo = 'Acme\Model\Foo';

$newFoo = $populator->populate($data, $foo);

echo $newFoo->getBar();         // Foobaz!
echo $newFoo->public;           // Public!
echo $newFoo->publicWithSetter; // BySetter

?>

Annotations

Along examples, we assume each protected property has its setter and its getter

@Populator\Ignore

This annotation skips the setting of the property

<?php

use Touki\Populator\Annotation as Populator;

class Foo
{
    /**
     * @Populator\Ignore
     */
    protected $bar;
}

$data = array(
    'bar' => 'Foobaz!'
);

$newFoo = $populator->populate($data, $foo);

echo $newFoo->getBar(); // NULL

?>

@Populator\Setter

This annotation allows property to define its own class' setter

<?php

use Touki\Populator\Annotation as Populator;

class Foo
{
    /**
     * @Populator\Setter("mySetter")
     */
    protected $bar;

    public function mySetter($value)
    {
        $this->bar = $value;
    }
}

$data = array(
    'bar' => 'Foobaz!'
);

$newFoo = $populator->populate($data, $foo);

echo $newFoo->getBar(); // Foobaz!

?>

@Populator\Alias

This annotation adds an alias to match on a property

<?php

use Touki\Populator\Annotation as Populator;

class Foo
{
    /**
     * @Populator\Alias("bar")
     * @Populator\Alias("another")
     */
    protected $foo;

    public function setFoo($value)
    {
        $this->foo = $value;
    }
}

$data = array(
    'bar' => 'Foobaz!'
);

$newFoo = $populator->populate($data, $foo);

echo $newFoo->getFoo(); // Foobaz!

$data = array(
    'another' => 'Foobaz!'
);

$newFoo = $populator->populate($data, $foo);

echo $newFoo->getFoo(); // Foobaz!

?>

@Populator\Aliases

This annotation sets and replaces aliases

<?php

use Touki\Populator\Annotation as Populator;

class Foo
{
    /**
     * @Populator\Aliases({"bar", "another"})
     */
    protected $foo;

    public function setFoo($value)
    {
        $this->foo = $value;
    }
}

$data = array(
    'bar' => 'Foobaz!'
);

$newFoo = $populator->populate($data, $foo);

echo $newFoo->getFoo(); // Foobaz!

?>

### @Populator\Deep

This annotations lets you have a deeper object

<?php

use Touki\Populator\Annotation as Populator;

class Foo
{
    /**
     * @Populator\Deep("Bar")
     */
    protected $bar;

    public function setBar(Bar $bar)
    {
        $this->bar = $bar;
    }
}

class Bar
{
    protected $baz;

    public function setBaz($bar)
    {
        $this->baz = $baz;
    }
}

$data = array(
    'bar' => array(
        'baz' => 'DeepBaz!'
    )
);

$newFoo = $populator->populate($data, $foo);

echo $newFoo->getBar()->getBaz(); // DeepBaz!

?>

touki/populator 适用场景与选型建议

touki/populator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 556 次下载、GitHub Stars 达 2, 最近一次更新时间为 2013 年 11 月 15 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 touki/populator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 touki/populator 我们能提供哪些服务?
定制开发 / 二次开发

基于 touki/populator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2013-11-15