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: 92: 93: 94: 95:
<?php
namespace Coast\Validator\Rule;
use Coast\Validator\Rule;
use Coast\File as CoastFile;
class File extends Rule
{
const VALID = 'valid';
const EXISTS = 'exists';
const SIZE = 'size';
const TYPE = 'type';
const READABLE = 'readable';
const WRITABLE = 'writable';
protected $_size = null;
protected $_types = null;
protected $_readable = null;
protected $_writable = null;
public function __construct($size = null, array $types = null, $readable = null, $writable = null)
{
$this->size($size);
$this->types($types);
$this->readable($readable);
$this->writable($writable);
}
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;
}
public function readable($readable = null)
{
if (func_num_args() > 0) {
$this->_readable = $readable;
return $this;
}
return $this->_readable;
}
public function writable($writable = null)
{
if (func_num_args() > 0) {
$this->_writable = $writable;
return $this;
}
return $this->_writable;
}
protected function _validate($value)
{
if (!$value instanceof CoastFile) {
$this->error(self::VALID);
return;
}
if (!$value->exists()) {
$this->error(self::EXISTS);
return;
}
if (isset($this->_size) && $value->size() > $this->_size) {
$this->error(self::SIZE);
}
if (isset($this->_types) && !in_array($value->extName(), $this->_types)) {
$this->error(self::TYPE);
}
if (isset($this->_readable) && $this->_readable == $value->isReadable()) {
$this->error(self::READABLE);
}
if (isset($this->_writable) && $this->_writable == $value->isWritable()) {
$this->error(self::WRITABLE);
}
}
}