apsonex/swap-placeholder 问题修复 & 功能扩展

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

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

apsonex/swap-placeholder

最新稳定版本:1.0.1

Composer 安装命令:

composer require apsonex/swap-placeholder

包简介

Swap Placeholder

README 文档

README

Package: apsonex/swap-placeholder A simple and flexible PHP package to dynamically swap out placeholders in strings using resource data and resolver classes.

🚀 Features

  • Define your own placeholder format (e.g. __PLACEHOLDER__)
  • Attach resource models (like Lead, Company, etc.)
  • Implement your own resolver logic
  • Easily swap placeholders in strings for dynamic content generation

📦 Installation

composer require apsonex/swap-placeholder

🧠 Usage Example

\Apsonex\SwapPlaceholder\SwapPlaceholder::make()
    ->fresh()
    ->placeholderIdentity('__')
    ->addResource(Lead::class, Lead::first())
    ->sources([
        PlaceholderResolver\LeadPlaceholderResolver::class,
    ])
    ->targetString('Some string with __LEAD_NAME__, __LEAD_EMAIL__, __COMPANY_NAME__ & __COMPANY_EMAIL__')
    ->handle()
    ->output();

🧩 Creating a Placeholder Resolver

Each resolver class should extend the BasePlaceholderResolver and implement the PlaceholderResolverContract.

<?php

namespace App\Support\PlaceholderResolver;

use App\Models\Lead;
use Apsonex\SwapPlaceholder\BasePlaceholderResolver as Base;
use Apsonex\SwapPlaceholder\PlaceholderResolverContract;

class LeadPlaceholderResolver extends Base implements PlaceholderResolverContract
{
    protected string $resolverId = Lead::class; // or any unique string ID

    // Optionally override methods to define how placeholders are resolved
}

✅ Testing Example

it('swap_placeholder_works', function () {
    $output = Apsonex\SwapPlaceholder\SwapPlaceholder::make()
        ->fresh()
        ->placeholderIdentity('__')
        ->addResource(Lead::class, new Lead)
        ->addResource(Company::class, new Company)
        ->sources([
            \Tests\Fixtures\Resolvers\LeadPlaceholderResolver::class,
            \Tests\Fixtures\Resolvers\CompanyPlaceholderResolver::class,
        ])
        ->targetString('Some string with __LEAD_NAME__, __LEAD_EMAIL__, __COMPANY_NAME__ & __COMPANY_EMAIL__')
        ->handle()
        ->output();

    expect(
        $output === 'Some string with John Doe, email@example.com, Abc Inc. & info@company.com'
    )->toBeTrue();
});

📄 License

MIT © Apsonex Inc.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-07-30