承接 atomsk/polyprops 相关项目开发

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

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

atomsk/polyprops

Composer 安装命令:

composer require atomsk/polyprops

包简介

A lightweight library for property validation and object-to-array/JSON conversion using Symfony Validator and traits.

README 文档

README

PHP Pipeline Status Downloads Latest Version

How to install

composer require atomsk/polyprops

Configuration

Polyprops uses Symfony Validator under the hood. It works out of the box with PHP attributes for validation constraints.

⚠️ IMPORTANT

For optimal performance and security:

  • Use protected properties for your data. The Validates trait uses __set to trigger validation automatically when a protected property is modified from outside (via magic setter).
  • Public properties are only validated during factory creation (fromArray, fromJson). If you modify a public property directly after the object is created, no automatic validation will occur.
  • Avoid overriding __set unless you manually call $this->validate() within it, as it may break the automatic validation mechanism.

Example

Basic Usage

<?php

use atomsk\Polyprops\Traits\Validates;
use atomsk\Polyprops\Traits\AsArray;
use atomsk\Polyprops\Contracts\Validatable;
use Symfony\Component\Validator\Constraints as Assert;

class User implements Validatable
{
    use Validates, AsArray;

    #[Assert\NotBlank]
    #[Assert\Email]
    protected string $email;

    #[Assert\Length(min: 8)]
    protected string $password;
    
    // Public properties are only validated on creation
    #[Assert\Range(min: 18)]
    public int $age;
}

// Creation via Factory (validates immediately)
$user = User::fromArray([
    'email' => 'user@example.com',
    'password' => 'secret123',
    'age' => 25
]);

// Updating a protected property (triggers automatic validation via __set)
$user->email = 'new-email@example.com'; 

// Converting back to array
$data = $user->toArray();

JSON Support

<?php

use atomsk\Polyprops\Traits\AsJson;
use atomsk\Polyprops\Contracts\Validatable;
use Symfony\Component\Validator\Constraints as Assert;

class Product implements Validatable
{
    use AsJson, Validates;

    #[Assert\NotBlank]
    protected string $sku;
}

$product = Product::fromJson('{"sku": "ABC-123"}');
echo $product->toJson();

Standalone Trait Usage

You can use AsArray or AsJson without the Validates trait if you only need the conversion features without validation.

<?php

use atomsk\Polyprops\Traits\AsArray;

class SimpleUser
{
    use AsArray;

    public string $name;
    public string $role;
}

// Works even without the Validates trait or Validatable contract
$user = SimpleUser::fromArray(['name' => 'Alice', 'role' => 'admin']);
$data = $user->toArray();

The Importance of the Validatable Contract

Automatic validation during factory creation (fromArray, fromJson) only occurs if your class implements the Validatable contract. If you use the traits without the contract, you must call validate() manually.

<?php

use atomsk\Polyprops\Traits\Validates;
use atomsk\Polyprops\Traits\AsArray;
use Symfony\Component\Validator\Constraints as Assert;

// Missing "implements Validatable"
class IncompleteUser
{
    use AsArray, Validates;

    #[Assert\Email]
    public string $email;
}

// No validation occurs here even if the email is invalid!
$user = IncompleteUser::fromArray(['email' => 'not-an-email']);

// You must validate manually if the contract is missing
// $user->validate(); 

Manual Validation & Error Handling

You can also trigger validation manually and handle errors using the ObjectValidationException.

<?php

use atomsk\Polyprops\Traits\Validates;
use atomsk\Polyprops\Exceptions\ObjectValidationException;
use Symfony\Component\Validator\Constraints as Assert;

class Contact
{
    use Validates;

    #[Assert\NotBlank]
    public string $name;

    #[Assert\Email]
    public string $email;
}

$contact = new Contact();
$contact->name = 'John';
$contact->email = 'invalid-email';

try {
    $contact->validate();
} catch (ObjectValidationException $e) {
    // Get all error messages grouped by property
    $errors = $e->errorsBag();
    
    /*
    [
        "email" => ["This value is not a valid email address."]
    ]
    */
}

AI & Automation Guidelines

This repository defines guidelines for AI-assisted work. First, read AI.md (single source of truth):

License

MIT - Open source, free to use and modify.

atomsk/polyprops 适用场景与选型建议

atomsk/polyprops 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 172 次下载、GitHub Stars 达 1, 最近一次更新时间为 2026 年 02 月 12 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-02-12