定制 nicmart/arrayze 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

nicmart/arrayze

Composer 安装命令:

composer require nicmart/arrayze

包简介

A callback-based decorator that gives array access to values.

README 文档

README

Build Status Coverage Status Scrutinizer Code Quality

Give your values a lazy array interface!

What is Arrayze?

Arrayze gives you an adapter for ArrayAccess and Traversable php interfaces. The adapter is built from a collection of callbacks, that maps the original value to runtime-computed values.

This means that you can easily give your objects or values an array-like interface, specifying how to compute offsets through callbacks.

Example

Let's suppose you have a Person class:

class Person
{
    private $firstName;
    private $lastName;
    private $birthYear;

    public function __construct($firstName, $surname, $birthYear) { ... }

    public function getFirstName() { return $this->firstName; }
    public function getLastName() { return $this->lastName; }
    public function getBirthYear() { return $this->birthYear; }
}

You can then specify a collection of maps:

use NicMart\Arrayze\MapsCollection;

$maps = (new MapsCollection)->registerMaps([
    "first name" =>   function(Person $p) { return $p->getFirstName(); },
    "last name" =>    function(Person $p) { return $p->getFirstName(); },
    "full name" =>    function($_, $x) { return "{$x['first name']} {$x['last name']}"; },
    "age" =>          function(Person $p) { return date("Y") - $p->getBirthYear(); },
    "name and age" => function($_, $x) { return "{$x['full name']}, {$x['age']}" }
]);

With that collection in place, you can now adapt Person instances to the new lazy array interface:

use NicMart\Arrayze\ArrayAdapter;

$nic = new Person("Nicolò", "Martini", 1983);

$arrayzedNic = new ArrayAdapter($nic, $maps);

echo $arrayzedNic["full name"];    // Prints "Nicolò Martini"
echo $arrayzedNic["age"];          // Prints 31
echo $arrayzedNic["name and age"]; // Prints "Nicolò Martini, 31"

ArrayAdapter implements also the Iterator interface, so you can iterate (lazily) through your arrayzed objects:

foreach ($arrayzedNic as $key => $value)
    echo "$key: $value\n";
    
// Prints
// first name: Nicolò
// last name: Martini
// full name: Nicolò Martini
// age: 31
// name and age: Nicolò Martini, 31

Convert to array

You can easily convert your adapted object to a native array with the ArrayAdapter::toArray() method.

Install

The best way to install Arrayze is through composer.

Just create a composer.json file for your project:

{
    "require": {
        "nicmart/arrayze": "~0.1"
    }
}

Then you can run these two commands to install it:

$ curl -s http://getcomposer.org/installer | php
$ php composer.phar install

or simply run composer install if you have have already installed the composer globally.

Then you can include the autoloader, and you will have access to the library classes:

<?php
require 'vendor/autoload.php';

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-06-19

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固