承接 gowork/values 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

gowork/values

Composer 安装命令:

composer require gowork/values

包简介

PHP primitive types value objects.

README 文档

README

Values is a library to wrap PHP's primitive types into clean, immutable and more user-friendly objects.

Build Status License Latest Stable Version Maintainability

Installation

It works on PHP >=8.0. This library is available on Composer/Packagist as gowork/values. To install it execute:

composer require gowork/values ^0.6

or manually update your composer.json with:

{
    (...)
    "require": {
        "gowork/values": "^0.6"
    }
    (...)
}

and run composer install or composer update afterwards. If you are not using Composer, download sources from GitHub and load them as required. However, using Composer is highly recommended.

Usage

Currently available implementations are:

ArrayValue

Object equivalent of PHP native indexed array. It contains implementation of most array_* functions as object method.

Example:

<?php

use GW\Value\Wrap;

$arrayValue = Wrap::array(['a', 'b', 'c', 'a', 'd', 'f'])
    ->map(function (string $value): string {
        return strtoupper($value)
    })
    ->map('strtolower')
    ->filter(function (string $value): bool {
        return $value !== 'd';
    })
    ->sort(function (string $a, string $b): int {
        return $a <=> $b;
    })
    ->shuffle()
    ->reverse()
    ->unique()
    ->diff(Wrap::array(['d', 'f']))
    ->intersect(Wrap::array(['a', 'b', 'c']))
    ->join(Wrap::array(['g', 'h', 'i']))
    ->unshift('j')
    ->shift($j)
    ->push('l')
    ->pop($l)
    ->slice(0, 6)
    ->each(function (string $value): void {
        echo $value;
    });

$count = $arrayValue->count();

$reduced = $arrayValue->reduce(
    function (string $reduced, string $value): string {
        return $reduced . $value;
    },
    ''
);

$stringValue = $arrayValue->implode(', ');

if (isset($arrayValue[0])) {
    $first = $arrayValue[0];
}

$first = $arrayValue->first();

foreach ($arrayValue as $item) {
    echo $item;
}

AssocValue

Object equivalent of PHP associative array. It has all the methods of ArrayValue with few minor differences and few additions.

<?php

use \GW\Value\Wrap;

$assocValue = Wrap::assocArray(['a' => 1, 'b' => 2, 'c' => 3, 'x' => 0])
    ->with('d', 4)
    ->without('a', 'b')
    ->withoutElement(0)
    ->merge(Wrap::assocArray(['e' => 5, 'f' => 6]));

$keys = $assocValue->keys();

$withMappedKeys = $assocValue->mapKeys(function (string $key): string {
    return strtoupper($key);
});

$aValue = $assocValue->get('a', $default = 1);
$hasA = $assocValue->has('a');

$associativeArray = $assocValue->toAssocArray();
$indexedArray = $assocValue->toArray();

StringValue

Object equivalent of PHP primitive string. It contains implementation of most str*/mb_str* functions as object method.

<?php

use GW\Value\Wrap;

$stringValue = Wrap::string('just example string')
    ->trim()
    ->trimRight()
    ->trimLeft()
    ->lower()
    ->upper()
    ->lowerFirst()
    ->upperFirst()
    ->upperWords()
    ->padLeft(50, '-')
    ->padRight(100, '-')
    ->padBoth(200, '-')
    ->replace('no', 'yes')
    ->replacePattern('/\s/', '-')
    ->replacePatternCallback('/[\-]+/', function (array $match): string {
        return '-';
    })
    ->truncate(140)
    ->substring(0, 100)
    ->stripTags();

$hasExample = $stringValue->contains('example');
$firstA = $stringValue->position('a');
$lastA = $stringValue->positionLast('a');
$stringLength = $stringValue->length();
$primitiveString = $stringValue->toString();
$onlyLetters = $stringValue->isMatching('/^[a-z]+$/');
$stringsArray = $stringValue->explode(' ');

StringsArray

Object wrapping array of strings. It has all methods of ArrayValue and StringValue. Calling a method inherited from StringValue means is same as calling this method on each StringValue element contained in StringsArray.

<?php

use \GW\Value\Wrap;
use \GW\Value\StringValue;

$stringsArray = Wrap::stringsArray(['one', '  two ', '<b>three</b>'])
    // StringValue
    ->trim()
    ->stripTags()
    ->padLeft(16)
    // ArrayValue
    ->unique()
    ->each(function (StringValue $value): void {
        echo $value->toString();
    });

IterableValue

Object wrapping iterable.

<?php

use \GW\Value\Wrap;

$range = function (int $start, int $end) {
    for ($i = $start; $i <= $end; $i++) {
        yield $i;
    }
};

$stringsArray = Wrap::iterable($range(0, 10))
    ->join(Wrap::iterable($range(400, 440000)))
    ->slice(10, 20)
    ->filter(function (int $value): bool {
        return $value % 2 === 0;
    })
    ->map(function (int $value): int {
        return $value + 2;
    })
    ->each(function (int $value): void {
        echo $value . "\n";
    });

Documentation

For full methods reference and more examples see here.

Contributing

Want to contribute? Perfect! Submit an issue or Pull Request and explain what would you like to see in GW/Value.

License

MIT license. See LICENSE file in the main directory of this repository.

gowork/values 适用场景与选型建议

gowork/values 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 36.83k 次下载、GitHub Stars 达 33, 最近一次更新时间为 2017 年 11 月 20 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「type」 「Value Object」 「primitive」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 36.83k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 34
  • 点击次数: 13
  • 依赖项目数: 1
  • 推荐数: 1

GitHub 信息

  • Stars: 33
  • Watchers: 3
  • Forks: 5
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-11-20