dashifen/repository 问题修复 & 功能扩展

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

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

dashifen/repository

Composer 安装命令:

composer require dashifen/repository

包简介

An object with read-only protected properties from which other objects can be extened.

README 文档

README

Repository (noun): a place, building, or receptacle where things are or may be stored.

This package defines an object, AbstractRepository, that provides read-only access to its protected properties using __get(). If there are protected properties that need to remain hidden from external scopes, you can specify a list that won't be returned in that way.

Installation

composer install dashifen/repository

Usage

There are two ways to go here:

  1. Extend the AbstractRepository object.
  2. Extend the Repository object.

If you extend the abstract object, you'll be forced to implement three methods:

  1. getHiddenPropertyNames - returns an array of properties that should remain inaccessable via the arrow operator,
  2. getCustomPropertyDefaults - sets more complex default values than can be set during property declaration,
  3. getRequiredProperties - returns a list of properties that must have values after object instantiation.

The Repository object has already implemented these methods; they each return empty arrays.

Construction

The constructor for a Repository takes an associative array of data such that the indices are the names of properties and the array values will be set as the values for the listed properties. If you write a setter, it'll be called with the values for validation purposes.

The constructor's array argument's indices can either be in the expected camel case for the object's properties or in kabob case as in HTML attributes. Thus, an index of start-date would be "linked" to the startDate property.

Getters

Typically, because a Repository exposes protected properties, getting them with the arrow operator is the way to go. But, if you want to transform the internal representation of a property for external scopes, you can define a getter for a property that performs a transformation and returns its results. For example, converting a date from YYYY-MM-DD format into MM/DD/YYYY for display on-screen.

Getters must be in the form of "get" . ucfirst($propertyName). So, the startDate property would have a getter of getStartDate(). Getters can, themselves, be protected if you want to hide them from external scopes and rely on the __get() implementation and the arrow operator for access.

Setters

Repositories do not implement __set(), so you have to write them yourself. By default, the AbstractRepository object will use setters within it's __construct() method. So, if you extend that object, you must create a setter for each of your properties that are expected to be used by that constructor, i.e. those properties referenced by the constructor's array parameter.

Like getters, they must be in the format of "set" . ucfirst($propertyName). Thus, the setter for startDate must be setStartDate(). If you implement setters, they will be called from the Repository's constructor when it iterates over it's array argument. Because they're called from the constructor, they can be protected or private to create an object with read-only properties after construction.

Example

class Foo extends AbstractRepository {
    protected $bar;
    protected $baz;
    protected $bing;
    
    protected function getHiddenPropertyNames(): array 
    {
        return ["baz"];
    }
    
    protected function getCustomPropertyDefaults(): array 
    {
        return ["bing" => strtotime('Y-m-d h:i:s')];
    }
    
    protected function getRequiredProperties(): array 
    {
        return ["bar"];
    }

    protected function setBar(string $bar): void 
    {
        $this->bar = $bar;   
    }
    
    protected function getBar(): string 
    {
        return ucfirst($this->bar);
    }
}

$foo = new Foo(["bar" => "apple"]);

echo $foo->bar;         // echos "Apple" because of the getBar() getter
echo $foo->baz;         // throws RepositoryException (baz is hidden)
echo $foo->bing;        // echos current timestamp (based on custom default value)

$oof = new Foo([]);     // throws RepositoryException (bar is required)

Array-able

There is a toArray method for Repositories to extract property names and values for non-hidden properties of the object.

JsonSerializable

Repositories implement the JsonSerializable interface. Therefore, you can encode them and non-hidden protected properties will be included in the JSON string that action produces.

Iterator

Repositories implement the Iterator interface. This allows you to use them in a foreach loop. Using the Foo object defined in our example above ...

$foo = new Foo(['bar' => 'Hello, World!']);

foreach ($foo as $field => $value) {
    echo "$field: $value" . PHP_EOL;
}

... would produce ...

bar: Hello, World!
bing: <timestamp>

... skipping the baz property because it's hidden.

dashifen/container

This object is a new name for the old dashifen/container object. To avoid any confusion relating to the shared name with the PSR-11 Container Interface, I've simply changed the name from container to repository. All future work on this object will occur here. If you're still using the old object, I'd recommend switching your code and using this one instead.

dashifen/repository 适用场景与选型建议

dashifen/repository 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.41k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 07 月 04 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: WTFPL
  • 更新时间: 2019-07-04