PHP code example of dmt-software / gtin-validator

1. Go to this page and download the library: Download dmt-software/gtin-validator 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/ */

    

dmt-software / gtin-validator example snippets



use DMT\GTIN\Validator\GTIN;
use Symfony\Component\Validator\Validator\ValidatorInterface;

class Product
{
    #[GTIN(size: 13, message: 'Invalid EAN code.')]
    public string $ean;
}

$product = new Product();
$product->ean = '49923315534218';

/** @var ValidatorInterface $validator */
$errors = $validator->validate($product);

if (count($errors) > 0) {
    foreach ($errors as $error) {
        echo $error->getMessage() . ' ' . $error->getCause();
    }
}

// prints: Invalid EAN code. Number length is not correct.