alexrili/vephar

Small abstraction of api requests and transform responses in collections

v1.0.1 2020-02-26 22:40 UTC

This package is auto-updated.

Last update: 2021-12-29 03:23:50 UTC


README

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

vephar vephar is a simple library that transform you arrays in collections/resources.

Note: vephar uses illuminate/support under the hood. If you are using laravel or lumen framework, make sure that you version is >= 5.5 until 6.x, if you're not using any of this frameworks or this this dependency(illuminate/support) , than you have nothing to worry.

Install

Via Composer

$ composer require alexrili/vephar

Usage (Automatic way)

The vephar will transform your arrays(and the nested as well) in collections of resources automatically. Every index of your array will be a new attribute of your new collection/resource(object).

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)
$vephar = new Response();
$vephar->make($array);

# Using static methods
// Note: both of these methods check if you are passing a array of collection or a single resource. They will be resolved by themself.
Response::resource($array); 
Response::collection($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)
  }
}

Usage (Custom way)

The vephar also allows you to assign your own collection and resource contracts to it.

namespace Hell\Vephar\Fake;  

use Hell\Vephar\Contracts\CollectionContract; 

class CustomCollection extends CollectionContract  
{  
}
namespace Hell\Vephar\Fake;
  
use Hell\Vephar\Contracts\ResourceContract;  

class CustomResource extends ResourceContract  
{  
	
	/**  
	* @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;  
	}  
  }
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)
$vephar = new Response(CustomResourceClass::class, CustomCollectionClass:class);
$vephar->make($array);

# Using static methods
// Note: both of these methods check if you are passing a array of collection or a single resource. They will be resolved by themself.
Response::resource($array, CustomResourceClass::class); 
Response::collection($array CustomResourceClass::class, CustomCollection::class);

In this case the response will be your own custom classes

#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"
}

Change log

Please see CHANGELOG for more information on what has changed recently.

Testing

$ composer test

Contributing

Please see CONTRIBUTING and CODE_OF_CONDUCT for details.

Security

If you discover any security related issues, please email alexrili instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.