alexrili/vephar

API请求的小抽象,并将响应转换为集合

v2.0.0 2022-12-15 01:18 UTC

This package is auto-updated.

Last update: 2024-09-15 05:13:17 UTC


README

vephar vephar是一个简单的库,可以将您的数组转换为集合/资源(对象)。

Latest Version on Packagist Software License Build Status Coverage Status Quality Score Total Downloads

安装

通过Composer

  composer require alexrili/vephar

开始使用vephar

vephar提供了一个名为response_to_object的简单方法。

$yourArray = [];
// simple way, just pass your array data, and vephar will make the magic :)
$response = response_to_object($yourArray);
// passing your own custom contract classes
$response = response_to_object($yourArray, YourCustomContractClass::class);
// passing your own custom contract classes and your custom collection classes  
$response = response_to_object($yourArray, YourCustomContractClass::class, YourCustomContractCollection::class);

或者您可以这样调用

use Hell\Vephar\Response;

$yourArray = [];
$vephar = new Response();
$response = $vephar->make($yourArray);
$response = $vephar->make($yourArray, YourCustomContractClass::class);
$response = $vephar->make($yourArray, YourCustomContractClass::class, YourCustomContractCollection::class);

或者通过调用静态方法toObject

use Hell\Vephar\Response;

$response = Response::toObject($yourArray);
$response = Response::toObject($yourArray, YourCustomContractClass::class);
$response = Response::toObject($yourArray, YourCustomContractClass::class, YourCustomContractCollection::class);

使用方法(自动方式)

vephar将自动将您的数组(包括嵌套数组)转换为资源集合。您的数组的每个索引将变为新集合/资源(对象)的新属性。

use Hell\Vephar\Response;

#can be a response from api request
$array = [
	"title" => "my title",  
	"EMAIL" => "my@email.com",  
	"nested_Array" => [  
		"address" => "street 10",  
		"postal_code" => 1234  
	],  
	"true_array" => [  
		123,  
		10,  
		15  
	]
]
# Instantiating (recommended)
$response = response_to_object($array)
#Return for collection will be
class Hell\Vephar\Collection#73 (1) {
  protected $items =>
  array(1) {
    [0] =>
    class Hell\Vephar\Resource#71 (4) {
      public $title =>
      string(8) "my title"
      public $email =>
      string(12) "my@email.com"
      public $nestedArray =>
      class Hell\Vephar\Resource#72 (2) {
	    public $address =>
	    string(9) "street 10"
	    public $postalCode =>
	    int(1234)
	  }
      public $trueArray =>
      array(3) {
	    [0] =>
	    int(123)
	    [1] =>
	    int(10)
	    [2] =>
	    int(15)
	  }
    }
  }
}
#return for a single resource will be
class Hell\Vephar\Resource#74 (4) {
  public $title =>
  string(8) "my title"
  public $email =>
  string(12) "my@email.com"
  public $nestedArray =>
  class Hell\Vephar\Resource#72 (2) {
    public $address =>
    string(9) "street 10"
    public $postalCode =>
    int(1234)
  }
  public $trueArray =>
  array(3) {
    [0] =>
    int(123)
    [1] =>
    int(10)
    [2] =>
    int(15)
  }
}

使用方法(自定义方式)

vephar还允许您将自定义集合和资源契约分配给它。

重要:当您使用自己的契约时,您需要明确告诉vephar是否应该深入嵌套数组或更改属性名称模式为驼峰式等。

namespace Hell\Vephar\Fake;  

use Hell\Vephar\Contracts\CollectionContract; 

class CustomCollection extends CollectionContract  
{  
     // This will tell the vephar to goo deeper or not. 
     // The default is false  means should not go. 
    protected $keepDigging = false;
    
    // This will tell the vephar to change your attributes names to camelCase. 
    // The default is false  means will respect whatever you write
    protected $toCamelCase = false;
    
    // This will tell the vephar that you will set the attributes by your self. 
    // The default is false  means will the vephar will put values automatically
    // intou your attributes if they existes on the input data. 
    protected $setters = false;
}
namespace Hell\Vephar\Fake;
  
use Hell\Vephar\Contracts\ResourceContract;  

class CustomResource extends ResourceContract  
{  
	/**  
	* @bool  
	*/  
	protected $setters = true;
	/**  
	* @bool  
	*/  
	protected $keepDigging = true;
	/**  
	* @var  
	*/  
	public $name;  
	
	/**  
	* @var  
	*/  
	public $email;  


	/**  
	* @param mixed $email  
	*/  
	public function setName($name): void  
	{  
		$this->name = $name;  
	}  
	
	/**  
	* @param mixed $email  
	*/  
	public function setEmail($email): void  
	{  
		$this->email = $email;  
	}  
  }
#can be a response from api request
$array = [
	"title" => "my title",  
	"EMAIL" => "my@email.com",  
	"nested_Array" => [  
		"address" => "street 10",  
		"postal_code" => 1234  
	],  
	"true_array" => [  
		123,  
		10,  
		15  
	]
]

$vephar = response_to_object($array, CustomResourceClass::class, CustomCollectionClass:class);

在这种情况下,响应将是您的自定义类

#Return for collection will be
class Hell\Vephar\CustomCollection#73 (1) {
  protected $items =>
  array(1) {
    [0] =>
    class Hell\Vephar\Resource#71 (4) {
      public $name =>
      null(0) null
      public $email =>
      string(12) "my@email.com"
    }
  }
}
#return for a single resource will be
class Hell\Vephar\CustomResource#74 (4) {
  public $name =>
  null(0) null
  public $email =>
  string(12) "my@email.com"
}

变更日志

请参阅CHANGELOG以获取有关最近更改的更多信息。

测试

$ composer test

贡献

请参阅CONTRIBUTINGCODE_OF_CONDUCT以获取详细信息。

安全

如果您发现任何安全相关的问题,请通过电子邮件联系alexrili而不是使用问题跟踪器。

鸣谢

许可证

MIT许可证(MIT)。请参阅许可证文件以获取更多信息。