PHP code example of kint / vo

1. Go to this page and download the library: Download kint/vo 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/ */

    

kint / vo example snippets




namespace VO;

use ValueObject\ValueObject;

/**
 * @method string getName Gets name.
 * @method string getEmail Gets email.
 */
class SecretAgent extends ValueObject
{
     protected function getRules()
     {
        return [
            'name' => ['NotBlank', 'Length' => ['min' => 5]],
            'email' => ['NotBlank', 'Email', 'Length' => ['min' => 15]],
        ];
     }
}

$secretAgent = new VO\SecretAgent(['name' => 'Bond', 'email' => '[email protected]']);
// Now you can use magic methods and get values from your VO.
$secretAgentName = $secretAgent->getName();
$secretAgentEmail = $secretAgent->getEmail();
// Also you can pass this VO as parameter.
$controller->doSomethingWithSecretAgent($secretAgent);

use VO\SecretAgent;
use ValueObject\Exception\ValidationException;

try {
    $secretAgent = new SecretAgent(['name' => 'Bond', 'email' => 'error']);
} catch (ValidationException $e) {
    $errors = $e->getMessages();
}

class SecretAgentController
{
    public function indexAction(array $postData)
    {
        (new SecretAgentService())->doSomethingWithSecretAgent(new VO\SecretAgent($postData));
    }
}

class SecretAgentService
{
    public function doSomethingWithSecretAgent(VO\SecretAgent $secretAgent)
    {
        (new SecretAgentModel())->update($secretAgent);
    }
}

class SecretAgentModel
{
    public function update(VO\SecretAgent $secretAgent)
    {
        $secretAgentName = $secretAgent->getName();
        $secretAgentEmail = $secretAgent->getEmail();
        // Update model.
    }
}