americanreading/object-normalizer 问题修复 & 功能扩展

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

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

americanreading/object-normalizer

最新稳定版本:v1.1.0

Composer 安装命令:

composer require americanreading/object-normalizer

包简介

Normalize fields on objects

README 文档

README

Features

  • Required fields must be present on the output object, or normalize throws an exception.
  • Optional fields are added to the output object, if present.
  • Default values can be supplied for fields.
  • Aliases allow you to map field names on the input object to field names on the output object.
  • Modifier callables alter the values for fields.
  • Finalizer callable allows an additional modification after the object is normalized

Example

<?php

use AmericanReading\ObjectNormalizer\ObjectNormalizer;

// Begin with an array or object that contains fields we don't want and fields
// with the wrong names.
$input = [
    "id" => 12852,
    "show" => "Bob's Burger",
    "character" => "Gene Belcher",
    "favoriteColor" => "blue"
];

// Create a normalizer.
$normalizer = new ObjectNormalizer();

// Provide the names of the fields the final object should contain.
$normalizer->setRequiredFields(["name", "series")

// List other names for a field that may appear on the input object.
$normalizer->setAlias("name", ["character", "person"])

// Normalize using the settings provided above.
$output = $normalizer->normalize($input);

print_r($output);

/*
    stdClass Object
    (
        [name] => Gene Belcher
        [show] => Bob's Burger
    )
*/

Extended Example

<?php

use AmericanReading\ObjectNormalizer\ObjectNormalizer;

$normalizer = new ObjectNormalizer();
$normalizer
    ->setRequiredFields(["name", "show"])
    ->setOptionalFields(["instrument",  "favoriteColor", "favoriteAnimal"])
    ->setAlias("name", "character")
    ->setDefault("favoriteColor", "blue")
    ->setModifier("favoriteColor", function ($value) {
        if ($value == "green") {
            $value = "red";
        }
        return $value;
    })
    ->setFinalizer(function ($obj) {
        // Add an additional field, only for Gene.
        if ($obj->name === "Gene Belcher") {
            $obj->costume = "Beefsquatch";
        }
        return $obj;
    });

$gene = [
    "id" => 12852,
    "show" => "Bob's Burgers",
    "character" => "Gene Belcher",
    "instrument" => "Casio Keyboard"
];

$normalGene = $normalizer->normalize($gene);
print_r($normalGene);

/*
    stdClass Object
    (
        [name] => Gene Belcher
        [show] => Bob's Burgers
        [instrument] => Casio Keyboard
        [favoriteColor] => blue
    )
*/

$tina = [
    "id" => 12853,
    "show" => "Bob's Burgers",
    "character" => "Tina Belcher",
    "favoriteColor" => "green",
    "favoriteAnimal" => "Horse"
];

$normalTina = $normalizer->normalize($tina);
print_r($normalTina);

/*
    stdClass Object
    (
        [name] => Tina Belcher
        [show] => Bob's Burgers
        [favoriteColor] => red
        [favoriteAnimal] => Horse
    )
*/

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-06-05

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固