PHP code example of bytepath / shared

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

    

bytepath / shared example snippets


    public function rules($rules): self;

    public function validate($data, ?Closure $callback): ValidationResult;

$rules = [
  "name" => "meric",
];

$rules = [
  "name" => "meric",
];

$userData = [
  "name" => "andrew",
  "age"  => 12,
  "zip" => "90210" // No rule for this 
];

return $validator->rules($rules)->validate($userData, function($validatedData) {
   // zip will be filtered out of the $validatedData array

   // Returns a theoretical user object containing the validated data
   return App\Models\User::create($validatedData);
});

    abstract protected function checkData($data, $rules): ValidationResult;

    protected function passed($data = []): PassedValidation
    protected function failed($errors = []): FailedValidation

// Pulled from the validate() example from earlier in this doc
$result = $validator->rules($rules)->validate($data, function($validatedData) {
   return App\Models\User::create($validatedData);
});

// Transforms the "User" into JSON.
$result->transform(function($data) {
 return json_encode($data);
});

// Returns {"name": "andrew", "age": 12}
$shouldBeJSON = $result->getData();

$failed = $validator->rules($rules)->validate($data, $fn);  // Validation failed here returning a FailedValidation object

$failed->passes(); // False

$failed->transform(function($data) {
   return json_encode($data);   // This function will NOT be ran on FailedValidation so it's safe to assume we have validated data here
});

class SomeService
{
    public function __construct(protected Validator $validator)
    {}

    /**
     * Creates a validator, then uses that validator to validate the data 
     * and create a new user
    */
    public function createUser($data): ValidationResult
    {
        return $this->getCreateValidator()->validate($data, function($validatedData) {
            return App\Models\User::create($validatedData);
        });
    }

    /**
     * Creates a validator by passing it a list of rules 

class SomeController
{
    public function __construct(protected SomeService $service)
    {}

    public function createUserReturnJSON($data)
    {
      /**
        If validation succeeds,we will create a new user and convert it to JSON

        If validation fails, we return a FailedValidation object that contains
        a list of validation errors you can provide to the user. You could
        process these errors here, or catch them in a middleware of some
        sort
      */
      return $this->service->createUser($data)->transform(function($data){
          // This callback will only run if validation was successful
          return json_encode($data);
      });
    }
}