arraypress/wp-cast-utils
Composer 安装命令:
composer require arraypress/wp-cast-utils
包简介
A lightweight WordPress library for type casting and data conversion
关键字:
README 文档
README
A lightweight WordPress library for smart type casting and data conversion. Focuses on genuinely useful conversions with special handling for WordPress-specific formats.
Features
- 🎯 Focused API: Only the conversions that add real value
- 🔧 WordPress-Ready: Handles serialized data, JSON, and CSV
- ⚡ Smart Conversions: Intelligent array and number detection
- 🛡️ Type Safety: Predictable return types
- 📊 Meta Support: Direct casting of WordPress meta values
Requirements
- PHP 7.4 or later
- WordPress 5.0 or later
Installation
composer require arraypress/wp-cast-utils
Basic Usage
Type Casting
use ArrayPress\CastUtils\Cast; Boolean conversion (handles 'yes'/'no', 'true'/'false', etc.) Cast::to_bool( 'yes' ); true Cast::to_bool( 'false' ); false Cast::to_bool( 1 ); true Smart number conversion (preserves int vs float) Cast::to_number( '123' ); 123 (int) Cast::to_number( '123.45' ); 123.45 (float) Cast::to_number( '-67', true ); 67 (absolute)
Array Conversions
Smart array conversion - tries multiple formats Cast::to_array( 'a,b,c' ); ['a', 'b', 'c'] (CSV) Cast::to_array( '{"x":1,"y":2}' ); ['x' => 1, 'y' => 2] (JSON) Cast::to_array( 'a:2:{i:0;s:1:"a";i:1;s:1:"b";}' ); ['a', 'b'] (serialized) Array to CSV Cast::to_csv( ['apple', 'banana', 'cherry'] ); 'apple,banana,cherry' Cast::to_csv( ['red', 'green', 'blue'], '|' ); 'red|green|blue'
JSON Conversion
JSON encoding with WordPress functions Cast::to_json( ['name' => 'John', 'age' => 30] ); '{"name":"John","age":30}' Pretty JSON for debugging Cast::to_json( ['name' => 'John'], true ); { "name": "John" }
WordPress Meta Casting
Cast meta values directly to the type you need $featured = Cast::meta( 'post', $post_id, 'featured', 'bool' ); $tags = Cast::meta( 'post', $post_id, 'tags', 'array' ); $views = Cast::meta( 'post', $post_id, 'view_count', 'number' ); Works with options too $settings = Cast::meta( 'option', 'my_plugin_settings', '', 'array' );
API Reference
Core Methods
| Method | Description | Returns |
|---|---|---|
to_bool($value) |
Convert to boolean with smart string handling | bool |
to_number($value, $absolute = false) |
Smart int/float detection | int|float |
to_array($value) |
Smart array conversion (JSON, CSV, serialized) | array |
to_json($value, $pretty = false) |
Convert to JSON string | string|null |
to_csv($array, $separator = ',') |
Convert array to CSV string | string |
Meta Method
| Method | Description | Returns |
|---|---|---|
meta($type, $id, $key, $cast_type, $default = null) |
Cast WordPress meta to type | mixed |
Meta Types Supported:
'post'- Post meta'user'- User meta'term'- Term meta'option'- WordPress options
Cast Types:
'array'- Smart array conversion'bool'- Boolean'number'- Smart int/float'int'- Integer'float'- Float'string'- String
Common Use Cases
Form Data Processing
Handle various input formats intelligently $age = Cast::to_number( $_POST['age'] ?? 0 ); $interests = Cast::to_array( $_POST['interests'] ?? '' ); Handles CSV or array $newsletter = Cast::to_bool( $_POST['newsletter'] ?? false );
API Responses
Prepare clean data for JSON APIs $response = [ 'user_id' => Cast::to_number( $user->ID ), 'is_active' => Cast::to_bool( $user_meta['active'] ), 'tags' => Cast::to_array( $user_meta['tags'] ) ]; return wp_send_json( $response );
Plugin Settings
Handle plugin options reliably $enabled = Cast::meta( 'option', 'my_plugin_enabled', '', 'bool', false ); $max_items = Cast::meta( 'option', 'my_plugin_max', '', 'number', 10 ); $allowed_types = Cast::meta( 'option', 'my_plugin_types', '', 'array', [] );
Why This Library?
Unlike generic PHP casting, this library:
- Handles WordPress formats: Serialized data, meta values
- Smart detection: Preserves int vs float types
- Multiple format support: JSON, CSV, serialized in one method
- Clean API: Predictable, easy-to-remember method names
- No exceptions: Safe conversions that won't break your site
Requirements
- PHP 7.4+
- WordPress 5.0+
License
GPL-2.0-or-later
Support
arraypress/wp-cast-utils 适用场景与选型建议
arraypress/wp-cast-utils 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 15 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 09 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「wordpress」 「wp」 「sanitization」 「casting」 「form-data」 「type-conversion」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 arraypress/wp-cast-utils 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 arraypress/wp-cast-utils 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 arraypress/wp-cast-utils 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Simple PHP Request Validator
Data Transfer Objects with Attribute Casting and Date Mutators.
Strictly typed object hydration and value casting.
Cast currency attributes, stored as integer, to floats automatically.
Runn Me! Validation and Sanitization Library
A library that provides a secure way to cast variables in PHP.
统计信息
- 总下载量: 15
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 19
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: GPL-2.0-or-later
- 更新时间: 2025-09-02