1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91:
<?php
namespace Coast\Validator\Rule;
use Coast\Validator\Rule;
class Upload extends Rule
{
const VALID = 'valid';
const ERROR = 'error';
const SIZE = 'size';
const TYPE = 'type';
protected $_size = null;
protected $_types = null;
public function __construct($size = null, $types = null)
{
$this->size($size);
$this->types($types);
}
public function size($size = null)
{
if (func_num_args() > 0) {
$this->_size = $size;
return $this;
}
return $this->_size;
}
public function types($types = null)
{
if (func_num_args() > 0) {
$this->_types = $types;
return $this;
}
return $this->_types;
}
protected function _validate($value)
{
if (!$this->_validateArray($value)) {
$this->error(self::VALID);
return;
}
if ($value['error'] != UPLOAD_ERR_OK) {
switch ($value['error']) {
case UPLOAD_ERR_INI_SIZE:
case UPLOAD_ERR_FORM_SIZE:
$this->error(self::SIZE);
break;
case UPLOAD_ERR_PARTIAL:
case UPLOAD_ERR_NO_FILE:
case UPLOAD_ERR_NO_TMP_DIR:
case UPLOAD_ERR_CANT_WRITE:
case UPLOAD_ERR_EXTENSION:
default:
$this->error(self::ERROR);
break;
}
return;
}
if (isset($this->_size) && $value['size'] > $this->_size) {
$this->error(self::SIZE);
}
if (isset($this->_types) && !in_array(pathinfo((string) $value['name'], PATHINFO_EXTENSION), $this->_types)) {
$this->error(self::TYPE);
}
}
protected function _validateArray($value)
{
if (!is_array($value)) {
return false;
}
return array_keys($value) == [
'name',
'type',
'tmp_name',
'error',
'size',
];
}
}