<?php
namespace App\Validator;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use App\Validator\ContainsWhitespace;
use App\Validator\ContainsIsArray;
use App\Validator\ContainsUniqueInClass;
use App\Validator\ContainsExistValueInClass;
use App\Validator\ContainsChoiceArray;
use App\Validator\ContainsArrayList;
// use Digicore\EcommerceBundle\Repo\ProductRepo;
class Validator
{
public $validator;
public function __construct(ValidatorInterface $validator)
{
$this->validator = $validator;
}
public function validate(array $parameters, $request)
{
$errorMessages = null;
$constraintsArray = [];
$violationsArray = [];
foreach ($parameters as $key => $param) {
$violationsArray[$key] = !is_array($request->get($key))?trim($request->get($key)):$request->get($key);
$constraintsArray[$key] = [];
$arrayParam = explode('|', $param);
foreach ($arrayParam as $node) {
$arrNode = explode(':', $node);
$functionName = $arrNode[0];
if($functionName == 'image' || $functionName == 'imageArray' || $functionName == 'imageArrayRequired'
|| $functionName == 'file' || $functionName == 'fileArray'){
$violationsArray[$key] = $request->files->get($key);
}
if (method_exists($this, $functionName)){
array_push($constraintsArray[$key], $this->$functionName($key, isset($arrNode[1]) ? $arrNode[1] : null));
}
}
}
$constraints = new Assert\Collection(['fields' => $constraintsArray]);
$violations = $this->validator->validate($violationsArray, $constraints);
if (count($violations) > 0) {
$accessor = PropertyAccess::createPropertyAccessor();
foreach ($violations as $violation) {
$errorMessages = $violation->getMessage();
break;
}
}
return $errorMessages;
}
function required($key, $params = null){
return new Assert\NotBlank(
["message" => 'validate.required.'.$key]
);
}
function email($key, $params = null){
return new Assert\Email(
["message" => 'validate.email']
);
}
function length($key, $params = null){
$options = [
"minMessage" => 'validate.length.min.'.$key,
"maxMessage" => 'validate.length.max.'.$key
];
if($params){
$arrParam = explode(',', $params);
foreach ($arrParam as $key => $value) {
if( ($key % 2) == 0 ){
$options[$value] = $arrParam[($key + 1)];
}
}
}
return new Assert\Length($options);
}
// function exists($key, $params = null){
// if ($params) {
// $arrParam = explode(',', $params);
// $options = [
// "message" => 'validate.class.by.'.$arrParam[1].'.'.$arrParam[0],
// "class" => $arrParam[0],
// "field" => $arrParam[1],
// "value" => $arrParam[2]
// ];
// }
// return new ContainsClassByField($options);
// }
function choice($key, $params = null){
$options = [
"message" => 'validate.choice.'.$key,
];
if($params){
$arrParam = explode(',', $params);
$options["choices"] = $arrParam;
}
return new Assert\Choice($options);
}
function positive($key, $params = null){
$options = [
"message" => 'validate.positive.'.$key,
];
return new Assert\Positive($options);
}
function numeric($key, $params = null){
$options = [
"type" => 'numeric',
"message" => 'validate.numeric.'.$key,
];
return new Assert\Type($options);
}
function count($key, $params = null){
$options = [
"minMessage" => 'validate.count.min.'.$key,
"maxMessage" => 'validate.count.max.'.$key,
"exactMessage" => 'validate.count.exact.'.$key,
];
if($params){
$arrParam = explode(',', $params);
foreach ($arrParam as $key => $value) {
if( ($key % 2) == 0 ){
$options[$value] = $arrParam[($key + 1)];
}
}
}
return new Assert\Count($options);
}
function image($key, $params = null){
$options = [
"maxSizeMessage" => 'validate.image.maxSize.'.$key,
"mimeTypesMessage" => 'validate.image.mimeTypes.'.$key
];
if($params){
$arrParam = explode(',', $params);
foreach ($arrParam as $key => $value) {
if( ($key % 2) == 0 ){
if($value == "mimeTypes"){
$options[$value] = explode("#", $arrParam[($key + 1)]);
}else{
$options[$value] = $arrParam[($key + 1)];
}
}
}
}
return new Assert\Image($options);
}
function array($key){
$options = [
'type' => 'array',
'message' => 'validate.'.$key.'.is.{{ type }}',
];
return new Assert\Type($options);
}
function string($key){
$options = [
'type' => 'string',
'message' => 'validate.'.$key.'.is.{{ type }}',
];
return new Assert\Type($options);
}
function alnum($key){
$options = [
'type' => 'alnum',
'message' => 'validate.'.$key.'.is.{{ type }}',
];
return new Assert\Type($options);
}
function imageArray($keyName, $params = null){
$options = [
"maxSizeMessage" => 'validate.image.maxSize.'.$keyName,
"mimeTypesMessage" => 'validate.image.mimeTypes.'.$keyName
];
if($params){
$arrParam = explode(',', $params);
foreach ($arrParam as $key => $value) {
if( ($key % 2) == 0 ){
if($value == "mimeTypes"){
$options[$value] = explode("#", $arrParam[($key + 1)]);
}else{
$options[$value] = $arrParam[($key + 1)];
}
}
}
}
return new Assert\All([new Assert\Image($options)]);
}
function fileArray($keyName, $params = null){
$options = [
"maxSizeMessage" => 'validate.image.maxSize.'.$keyName,
"mimeTypesMessage" => 'validate.image.mimeTypes.'.$keyName
];
if($params){
$arrParam = explode(',', $params);
foreach ($arrParam as $key => $value) {
if( ($key % 2) == 0 ){
if($value == "mimeTypes"){
$options[$value] = explode("#", $arrParam[($key + 1)]);
}else{
$options[$value] = $arrParam[($key + 1)];
}
}
}
}
return new Assert\All([new Assert\File($options)]);
}
function file($key, $params = null){
$options = [
"maxSizeMessage" => 'validate.image.maxSize.'.$key,
"mimeTypesMessage" => 'validate.image.mimeTypes.'.$key
];
if($params){
// dd($key, $params);
$arrParam = explode(',', $params);
foreach ($arrParam as $key => $value) {
if( ($key % 2) == 0 ){
if($value == "mimeTypes"){
$options[$value] = explode("#", $arrParam[($key + 1)]);
}else{
$options[$value] = $arrParam[($key + 1)];
}
}
}
}
return new Assert\File($options);
}
function date($key, $params = null){
return new Assert\Date(
["message" => 'validate.date.'.$key]
);
}
function confirm($key, $params){
$arrParam = explode(',', $params);
$options = [
'message' => 'validate.confirm.'.$key,
// 'propertyPath' => $arrParam[0],
'value' => $arrParam[0]
];
// dd(new Assert\EqualTo($options));
return new Assert\EqualTo($options);
}
function whitespace($key, $params) {
$options = [
'message' => 'validate.whitespace.'.$key,
];
return new ContainsWhitespace($options);
}
// function arrayProduct($key) {
// $options = [
// 'fields' => [
// 'id' => [$this->required('id'), $this->existsValue('id', ProductRepo::PRODUCT_CLASS_NAME)],
// 'count' => [$this->required('count'), $this->numeric('count'), $this->positive('count')],
// ],
// 'missingFieldsMessage' => 'product.{{ field }}.missing'
// ];
// return new Assert\All([
// new Assert\Collection($options),
// ]);
// }
/**
* Check if value already existed in a field in a class.
* Format: unique:Class
*/
function unique($key, $class = null) {
$options = [
'message' => 'validate.unique.'.$key,
];
if ($class) {
$options['class'] = $class;
$options['field'] = $key;
}
return new ContainsUniqueInClass($options);
}
/**
* Check if value valid with regex expression .
* Format: regex:expression
*/
function regex($key, $expression = null) {
return new Assert\Regex([
'message' => 'validate.regex.'.$key,
'pattern' => '/'. $expression . '/'
]);
}
/**
* Check if value is valid with a field in Class
* Format: existsValue:Class<-field>
*/
function existsValue($key, $classField) {
$options = [
'message' => 'validate.value-not-exist.'.$key,
];
if ($classField) {
$array = explode('-', $classField);
$options['class'] = $array[0];
$options['field'] = count($array) == 2 ? $array[1] : $key;
}
return new ContainsExistValueInClass($options);
}
/**
* Format: choiceArray:option1,option2
*/
function choiceArray($key, $params) {
$options = [
'message' => 'validate.array-value-not-valid.'.$key,
];
$options['options'] = $params;
return new ContainsChoiceArray($options);
}
function arrayList($key, $params) {
$options = [
'message' => 'validate.array-value-not-valid.'.$key,
];
return new ContainsArrayList($options);
}
}