kanel/mapper

此包的最新版本(1.1)没有可用的许可信息。

简单地将JSON结构映射到PHP类

1.1 2017-05-16 20:42 UTC

This package is not auto-updated.

Last update: 2024-09-15 03:39:34 UTC


README

简单地将JSON结构映射到PHP类

build

#工作原理

  • 将数组、对象或JSON字符串映射到对象

        $data = ['testOne' => 1, 'testTwo' => 2, 'testThree' => 3]
        $myClass = new MyClass(); 
        $mapper = new Mapper();
        $mapper->map($data, $myClass);
        print_r($myClass);
        
        Where MyClass is: 
        
        class MyClass {
           protected $testOne;
           public $testTwo;
           private $testThree;
        }
        
        The result is :
        
        Object
        (
            [testOne] => 1
            [testTwo] => 2
            [testThree] => 3
        )
    
    
    • 第一个参数($from)可以是数组、对象或字符串
    • 如果第一个参数是字符串但不是有效的JSON,则抛出InvalidDataException异常
    • 如果第二个参数不是对象,则抛出InvalidDataException异常
    • 如果属性(当$from是对象时)或键(当$from是数组时)不存在,则不会创建它。
  • 在目标类中创建不存在的属性

    $options = new Options();
    $options->createNewProperty(true);
    $mapper = new Mapper($options);
    $mapper->map($from, $myClass);
    
  • 转换属性

    • 在映射过程中,您可以转换数组键或对象属性

    • 示例

      • 映射数组

        ['test_one' => 1] 
        

        到对象

            class Test { 
               protected $testOne;
            }
        

        其中数组键'test_one'对应于属性$testOne;

      • 或者映射对象

            class A { 
               protected $testA;
            }
        

        到对象

            class B { 
               protected $testB;
            }
        

        其中类A的属性$testA对应于类B的属性$testB

    为此,您需要使用转换器

    有4个内置转换器

    • CamelCaseTransformer:将snake或pascal case转换为camel case

      attribute-name => attributeName
      attribute_name => attributeName
      attribute name => attributeName
      Attribute_NAME => attributeName
      AttributeName  => attributeName
      attributeName  => attributeName
      attribute_firstName => attributeFirstname
      
    • SnakeCaseTransformer:将pascal或camel case转换为snake case

      attributeName => attribute_name
      attribute => attribute
      
    • PascalCaseTransformer:将snake或camel case转换为pascal case

      attribute-name => AttributeName
      attribute_name => AttributeName
      attribute name => AttributeName
      Attribute_NAME => AttributeName
      AttributeName  => AttributeName
      attributeName  => AttributeName
      attribute_firstName => AttributeFirstname
      
    • CustomTransformer:允许添加自己的转换器

    您可以为所有属性/键或每个属性/键指定转换器

    示例

    • 将CamelCase Transformer应用于所有属性/键

      $options = new Options();
      $options->setTransformer(new CamelCaseTransformer());
      $mapper = new Mapper($options);
      
      $class = new MyClassExample(); //Contains only one propery named $testOne;
      $data = ['test_one' => 'Hellow World'];
      
      $mapper->map($data, $class);
      
      Result :  
      
      print_r($class);
      
      Object
      (
          [testOne] => 'Hellow World'
      )
      
    • 将Custom Transformer应用于所有属性/键

      $customTransformer = new CustomTransformer();
      $customTransformer->setTransformer(function($property) {
          if ($property == 'test_one') {
              return 'name';
          }
          
          return $property; //very important or else only test_one will be mapped
      });
      $options = new Options();
      $options->setTransformer($customTransformer);
      $mapper = new Mapper($options);
      
      $class =  new MyClassExample(); //Having two Properties $name and $phone
      $data = ['test_one' => 'Jhon doe', 'phone' => 1234];
      $mapper->map($data, $class);
      
      Result :  
      
      print_r($class);
      
      Object
      (
          [name] => 'Jhon doe',
          [phone] => 1234
      )
      
    • 应用不同的转换器

      $customTransformer = new CustomTransformer();
      $customTransformer->setTransformer(function($property) {
         return 'name';
      });
      $options = new Options();
      $options->addPropertyTransformer('test_one', $customTransformer);
      $options->addPropertyTransformer('phone_number', new CamelCaseTransformer());
      $mapper = new Mapper($options);
      
      $class =  new MyClassExample(); //Having two Properties $name, $phoneNumber
      $data = ['test_one' => 'Jhon doe', 'phone_number' => 1234];
      $mapper->map($data, $class);
      
      Result :  
      
      print_r($class);
      
      Object
      (
          [name]  => 'Jhon doe',
          [phoneNumber] => 1234
      )