2021-03-09 18:02:07 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Rules;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Contracts\Validation\Rule;
|
|
|
|
|
|
|
|
|
|
class Cotationsxml implements Rule
|
|
|
|
|
{
|
|
|
|
|
private $schema_errors = "Le fichier n'est pas conforme au schéma XSD : ";
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a new rule instance.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function __construct()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Determine if the validation rule passes.
|
|
|
|
|
*
|
2023-08-21 15:24:32 +02:00
|
|
|
* @param string $attribute
|
2021-03-09 18:02:07 +01:00
|
|
|
* @param mixed $value
|
2023-08-21 15:24:32 +02:00
|
|
|
* @return boolean
|
2021-03-09 18:02:07 +01:00
|
|
|
*/
|
|
|
|
|
public function passes($attribute, $value)
|
|
|
|
|
{
|
2023-08-21 15:24:32 +02:00
|
|
|
// file is stored, now validate the schema
|
|
|
|
|
$xsd = storage_path('app/cotations/Jeusselin.xsd');
|
|
|
|
|
$domDocument = new \DOMDocument();
|
2021-03-09 18:02:07 +01:00
|
|
|
$domDocument->load($value->path());
|
|
|
|
|
// Enable user error handling
|
|
|
|
|
libxml_use_internal_errors(true);
|
|
|
|
|
if (!$domDocument->schemaValidate($xsd)) {
|
|
|
|
|
$errors = libxml_get_errors();
|
|
|
|
|
foreach ($errors as $error) {
|
|
|
|
|
$this->schema_errors .= $error->message;
|
|
|
|
|
}
|
|
|
|
|
libxml_clear_errors();
|
|
|
|
|
return false;
|
|
|
|
|
} else {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the validation error message.
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function message()
|
|
|
|
|
{
|
|
|
|
|
return $this->schema_errors;
|
|
|
|
|
}
|
|
|
|
|
}
|