PHP code example of alexrili / vephar

1. Go to this page and download the library: Download alexrili/vephar library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

alexrili / vephar example snippets


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

use Hell\Vephar\Response;

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

use Hell\Vephar\Response;

#can be a response from api request
$array = [
	"title" => "my title",  
	"EMAIL" => "[email protected]",  
	"nested_Array" => [  
		"address" => "street 10",  
		"postal_code" => 1234  
	],  
	"true_array" => [  
		123,  
		10,  
		15  
	]
]
# Instantiating (recommended)
$response = response_to_object($array)

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" => "[email protected]",  
	"nested_Array" => [  
		"address" => "street 10",  
		"postal_code" => 1234  
	],  
	"true_array" => [  
		123,  
		10,  
		15  
	]
]

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