alexrili/vephar

Small abstraction of api requests and transform responses in collections

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

This package is auto-updated.

Last update: 2024-04-15 04:19:56 UTC


README

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

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

Install

Via Composer

  composer require alexrili/vephar

Start vephar

Vephar provides you a simple method called response_to_object out of the box.

$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);

OR you can call like this

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);

OR by calling static method called toObject

use Hell\Vephar\Response;

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

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)
$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)
  }
}

Usage (Custom way)

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

Important:When you use your own contracts you need to explicitly say to if vephar should or not make somthings like going deeper or not into your nested arrays or change the pattern of you attribute names to camelCase etc.

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);

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.