承接 andrew-gos/serializer 相关项目开发

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

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

andrew-gos/serializer

Composer 安装命令:

composer require andrew-gos/serializer

包简介

A flexible and extensible serialization library for modern PHP applications

README 文档

README

Latest Stable Version Total Downloads Latest Unstable Version License PHP Version Require

A flexible and extensible serialization library for modern PHP applications.

This library provides a simple yet powerful way to convert complex PHP data structures, including objects and arrays, into various string formats like JSON and XML.

🚀 Key Features

  • Flexible Architecture: Easily add your own custom normalizers and encoders.
  • Object & Array Support: Correctly handles scalars, arrays, and objects.
  • Built-in Encoders: Comes with ready-to-use encoders for JSON and XML.
  • Advanced XML Handling: Automatically manages data duplication and circular references for both arrays and objects.
  • Modern PHP: Built for PHP 8.2+ with strict typing.
  • No Side Effects: The encoders are designed to be "pure" and will not modify your original input data.

🛠️ Installation

The project requires PHP 8.2 or higher.

Install the library via Composer:

composer require andrew-gos/serializer

🏁 Quick Start

The Serializer is highly configurable and requires the registration of normalizers (to convert data into a serializable format) and encoders (to convert that format into a string).

Example 1: Serializing an Array

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

use AndrewGos\Serializer\Encoder\JsonEncoder;
use AndrewGos\Serializer\SerializerFactory;

// Use factory to set default normalizers (for scalars, arrays, etc.) and encoders (for json and xml)
$serializer = SerializerFactory::getDefaultSerializer();
// For array serializer will use default normalizer
$serializer->addEncoder('json', new JsonEncoder());

$data = ['user' => 'John Doe'];
$json = $serializer->serialize($data, 'json'); // Output: {"user":"John Doe"}
echo $json;

Example 2: Serializing an Object

<?php
// ... (use statements)

$serializer = SerializerFactory::getDefaultSerializer();

// Register a custom normalizer for our object.
// It should return an array, a scalar, or another object.
$serializer->addNormalizer(
    stdClass::class,
    function(stdClass $obj) {
        return (array) $obj; // Simply cast the object to an array
    },
);

$data = (object)['user' => 'Jane Doe'];
$json = $serializer->serialize($data, 'json'); // Output: {"user":"Jane Doe"}
echo $json;

🔗 Advanced Serialization: The XML Reference System

A unique feature of the XmlEncoder is its ability to handle complex data structures, including duplication and circular references. This prevents infinite loops and reduces output size.

How It Works

When the XmlEncoder encounters an array or an object, it checks if it has seen the exact same instance before.

  • First Encounter: The data is fully serialized and placed within a <references> block. It is assigned a unique key.
  • Subsequent Encounters: Instead of serializing the data again, a simple <reference key="..."/> tag is inserted.

Reference Handling Nuances: Arrays vs. Objects

The reference system behaves differently for arrays and objects due to how PHP passes them to functions.

Arrays and the "Pass-by-Value" Nuance

In PHP, arrays are passed to functions by value (a shallow copy is created). This causes the XmlEncoder to see an "extra" top-level reference. Example:

$data = ['name' => 'Recursive Array'];
$data['reference_to_self'] = &$data; // A self-reference

$xml = (new XmlEncoder())($data);

Result (Note the 2 entries in <references>):

<root>
  <references>
    <!-- #1: The inner, truly recursive array -->
    <reference key="key_A">
      <array>
        <item key="name"><string>Recursive Array</string></item>
        <item key="reference_to_self"><reference key="key_A"/></item>
      </array>
    </reference>
    <!-- #2: The outer copy created by the pass-by-value call -->
    <reference key="key_B">
      <array>
        <item key="name"><string>Recursive Array</string></item>
        <item key="reference_to_self"><reference key="key_A"/></item>
      </array>
    </reference>
  </references>
  <data><reference key="key_B"/></data>
</root>

Objects and "Pass-by-Reference" Behavior

In PHP, objects are always passed by reference. This means the encoder works with the original instance, and the "extra" reference problem does not occur. Example:

$data = new stdClass();
$data->name = 'Recursive Object';
$data->reference_to_self = &$data; // A self-reference

$xml = (new XmlEncoder())($data);

Result (Note only 1 entry in <references>):

<root>
  <references>
    <reference key="key_A">
      <object>
        <property name="name"><string>Recursive Object</string></property>
        <property name="reference_to_self"><reference key="key_A"/></property>
      </object>
    </reference>
  </references>
  <data><reference key="key_A"/></data>
</root>

This is the expected and correct behavior, allowing the XmlEncoder to reliably handle both data types.

🧪 Testing

To run the test suite, first ensure you have installed the development dependencies:

composer install --dev

Then, run PHPUnit:

./vendor/bin/phpunit tests

🤝 Contributing

Contributions are welcome! Please feel free to submit a pull request or open an issue.

📜 License

This project is licensed under the MIT License. See the LICENSE file for details.

andrew-gos/serializer 适用场景与选型建议

andrew-gos/serializer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 272 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 09 月 03 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 andrew-gos/serializer 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-09-03