Added status and source build fields validation.

This commit is contained in:
Dmitry Khomutov 2018-03-12 19:51:41 +07:00
commit d3a390d3f8
No known key found for this signature in database
GPG key ID: EC19426474B37AAC
5 changed files with 59 additions and 2 deletions

View file

@ -2,6 +2,7 @@
namespace PHPCensor\Model\Base;
use PHPCensor\Exception\HttpException\ValidationException;
use PHPCensor\Model;
class Build extends Model
@ -40,6 +41,28 @@ class Build extends Model
'user_id' => 0,
];
/**
* @var array
*/
protected $allowedStatuses = [
self::STATUS_PENDING,
self::STATUS_RUNNING,
self::STATUS_SUCCESS,
self::STATUS_FAILED,
];
/**
* @var array
*/
protected $allowedSources = [
self::SOURCE_UNKNOWN,
self::SOURCE_MANUAL_WEB,
self::SOURCE_MANUAL_CONSOLE,
self::SOURCE_PERIODICAL,
self::SOURCE_WEBHOOK,
self::SOURCE_WEBHOOK_PULL_REQUEST,
];
/**
* @return integer
*/
@ -132,6 +155,8 @@ class Build extends Model
/**
* @param integer $value
*
* @throws ValidationException
*
* @return boolean
*/
public function setStatus($value)
@ -139,6 +164,12 @@ class Build extends Model
$this->validateNotNull('status', $value);
$this->validateInt('status', $value);
if (!in_array($value, $this->allowedStatuses, true)) {
throw new ValidationException(
'Column "status" must be one of: ' . join(', ', $this->allowedStatuses) . '.'
);
}
if ($this->data['status'] === $value) {
return false;
}
@ -443,12 +474,20 @@ class Build extends Model
/**
* @param integer $value
*
* @throws ValidationException
*
* @return boolean
*/
public function setSource($value)
{
$this->validateInt('source', $value);
if (!in_array($value, $this->allowedSources, true)) {
throw new ValidationException(
'Column "source" must be one of: ' . join(', ', $this->allowedSources) . '.'
);
}
if ($this->data['source'] === $value) {
return false;
}