Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/xbyter/php-object-mapping
PHP对象映射,可以将array/object映射为类对象。可做参数的传递、Request参数映射、Entity、Bean等。并可根据定义的映射对象生成API文档。
https://github.com/xbyter/php-object-mapping
bean entity json-mapper json-mapping mapping object-fill object-mapper object-mapping request-mapping
Last synced: 6 days ago
JSON representation
PHP对象映射,可以将array/object映射为类对象。可做参数的传递、Request参数映射、Entity、Bean等。并可根据定义的映射对象生成API文档。
- Host: GitHub
- URL: https://github.com/xbyter/php-object-mapping
- Owner: xbyter
- License: mit
- Created: 2023-07-06T09:09:13.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2023-07-07T10:27:20.000Z (over 1 year ago)
- Last Synced: 2024-12-10T09:16:28.909Z (about 1 month ago)
- Topics: bean, entity, json-mapper, json-mapping, mapping, object-fill, object-mapper, object-mapping, request-mapping
- Language: PHP
- Homepage:
- Size: 14.6 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# php-object-mapping
# 说明
PHP对象映射,可以将array/object映射为类对象。可做参数的传递、Request参数映射、Entity、Bean等。并可根据定义的映射对象生成API文档。# 示例
```php
class TestMapping extends BaseMapping
{
/** @var int 总数 */
public int $total;public ?TestItem $item = null;
/** @var \Xbyter\PhpObjectMappingTests\Data\TestItem[] 列表对象 */
public array $list = [];/** @var string 受保护的值,不会被设置 */
protected string $protected_key = 'protected value';
}class TestItem extends BaseMapping
{
/** @var string|null 标题 */
public ?string $title = null;/**
* @var string|null 会返回装饰过的值
* @return TitleDecorator
*/
public ?string $decorate_title = null;
}//装饰类
class TitleDecorator implements DecoratorInterface
{public function decorate($value)
{
return sprintf("this is a decorated value: %s", $value);
}
}//映射对象,多维数组可用 TestMapping::fromList([[...], [...]]);
$testMapping = TestMapping::fromItem([
'total' => 10,
'item' => [
'title' => 'a'
],
'list' => [
[
'title' => 'b',
'decorate_title' => 'c',
]
],
'protected_key' => '受保护的字段不会被赋值',
]);//转为数组
$array = $testMapping->toArray();var_dump($testMapping->total); //10
var_dump($testMapping->item->title); //a
var_dump($testMapping->list[0]->title); //b
var_dump($testMapping->list[0]->decorate_title); //this is a decorated value: c
var_dump($testMapping->protected_key); //Error : Cannot access protected property $protected_key
```