Repository URL to install this package:
|
Version:
1.0.0 ▾
|
<?php
/**
* Created by PhpStorm.
* User: danilo
* Date: 28/07/16
* Time: 13:47
*/
namespace Modules\Core\Validators\Sanitize;
abstract class ValidatorSanitize
{
/**
* Data to sanitize
*
* @var array
*/
protected $data;
/**
* Execute sanitizer data
*
* @param array $input
* @return array
*/
public function sanitize(array $input)
{
$this->data = array_map(function ($val) {
return !is_array($val) ? trim($val) : $val;
}, $input);
$this->handler();
return $this->data;
}
/**
* Handler sanitize
*
* @return array
*/
abstract protected function handler();
/**
* Only numbers
*
* @param $val
* @return $this
*/
protected function onlyDigits($field)
{
$this->data[$field] = preg_replace("/\D/", "", $this->data[$field]);
return $this;
}
/**
* Boolean format
*
* @param $val
* @return $this
*/
protected function bolean($field)
{
$this->data[$field] = isset($this->data[$field]) ? true : false;
return $this;
}
/**
* Date format
*
* @param $field
* @return $this
*/
protected function dateFormat($field)
{
if (!empty($this->data[$field]) and preg_match("/^(0[1-9]|[1-2][0-9]|3[0-1])\/(0[1-9]|1[0-2])\/([0-9]{4})$/", $this->data[$field])) {
$this->data[$field] = \DateTime::createFromFormat('d/m/Y', $this->data[$field])->format('Y-m-d');
}
return $this;
}
}