2008-04-26 08:31:52 +02:00
|
|
|
<?php
|
|
|
|
|
2016-06-15 06:03:16 +02:00
|
|
|
namespace SilverStripe\ORM;
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Core\Object;
|
2016-06-15 06:03:16 +02:00
|
|
|
|
2008-04-26 08:31:52 +02:00
|
|
|
/**
|
|
|
|
* A class that combined as a boolean result with an optional list of error messages.
|
|
|
|
* This is used for returning validation results from validators
|
|
|
|
*/
|
|
|
|
class ValidationResult extends Object {
|
|
|
|
/**
|
|
|
|
* Boolean - is the result valid or not
|
|
|
|
*/
|
|
|
|
protected $isValid;
|
|
|
|
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-26 08:31:52 +02:00
|
|
|
/**
|
|
|
|
* Array of errors
|
|
|
|
*/
|
|
|
|
protected $errorList = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new ValidationResult.
|
2016-08-19 00:51:35 +02:00
|
|
|
* By default, it is a successful result. Call $this->error() to record errors.
|
|
|
|
*
|
|
|
|
* @param bool $valid
|
|
|
|
* @param string $message
|
2008-04-26 08:31:52 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function __construct($valid = true, $message = null) {
|
2008-10-08 22:40:11 +02:00
|
|
|
$this->isValid = $valid;
|
2008-04-26 08:31:52 +02:00
|
|
|
if($message) $this->errorList[] = $message;
|
2009-09-18 05:02:19 +02:00
|
|
|
parent::__construct();
|
2008-04-26 08:31:52 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-26 08:31:52 +02:00
|
|
|
/**
|
|
|
|
* Record an error against this validation result,
|
2015-12-09 22:19:23 +01:00
|
|
|
* @param string $message The validation error message
|
|
|
|
* @param int $code An optional error code string, that can be accessed with {@link $this->codeList()}.
|
|
|
|
* @return $this
|
2008-04-26 08:31:52 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function error($message, $code = null) {
|
2008-04-26 08:31:52 +02:00
|
|
|
$this->isValid = false;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-26 08:31:52 +02:00
|
|
|
if($code) {
|
|
|
|
if(!is_numeric($code)) {
|
|
|
|
$this->errorList[$code] = $message;
|
|
|
|
} else {
|
2012-09-26 23:34:00 +02:00
|
|
|
user_error("ValidationResult::error() - Don't use a numeric code '$code'. Use a string."
|
|
|
|
. "I'm going to ignore it.", E_USER_WARNING);
|
2008-04-26 08:31:52 +02:00
|
|
|
$this->errorList[$code] = $message;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$this->errorList[] = $message;
|
|
|
|
}
|
2014-01-08 23:03:36 +01:00
|
|
|
|
|
|
|
return $this;
|
2008-04-26 08:31:52 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-26 08:31:52 +02:00
|
|
|
/**
|
|
|
|
* Returns true if the result is valid.
|
2014-01-08 23:03:36 +01:00
|
|
|
* @return boolean
|
2008-04-26 08:31:52 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function valid() {
|
2008-04-26 08:31:52 +02:00
|
|
|
return $this->isValid;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-26 08:31:52 +02:00
|
|
|
/**
|
|
|
|
* Get an array of errors
|
2014-01-08 23:03:36 +01:00
|
|
|
* @return array
|
2008-04-26 08:31:52 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function messageList() {
|
2008-04-26 08:31:52 +02:00
|
|
|
return $this->errorList;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an array of error codes
|
2014-01-08 23:03:36 +01:00
|
|
|
* @return array
|
2008-04-26 08:31:52 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function codeList() {
|
2008-04-26 08:31:52 +02:00
|
|
|
$codeList = array();
|
|
|
|
foreach($this->errorList as $k => $v) if(!is_numeric($k)) $codeList[] = $k;
|
|
|
|
return $codeList;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-26 08:31:52 +02:00
|
|
|
/**
|
|
|
|
* Get the error message as a string.
|
2014-01-08 23:03:36 +01:00
|
|
|
* @return string
|
2008-04-26 08:31:52 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function message() {
|
2008-04-26 08:31:52 +02:00
|
|
|
return implode("; ", $this->errorList);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-26 08:31:52 +02:00
|
|
|
/**
|
|
|
|
* Get a starred list of all messages
|
2014-01-08 23:03:36 +01:00
|
|
|
* @return string
|
2008-04-26 08:31:52 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function starredList() {
|
2008-04-26 08:31:52 +02:00
|
|
|
return " * " . implode("\n * ", $this->errorList);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-26 08:31:52 +02:00
|
|
|
/**
|
|
|
|
* Combine this Validation Result with the ValidationResult given in other.
|
|
|
|
* It will be valid if both this and the other result are valid.
|
|
|
|
* This object will be modified to contain the new validation information.
|
2014-01-08 23:03:36 +01:00
|
|
|
*
|
2016-08-19 00:51:35 +02:00
|
|
|
* @param ValidationResult $other the validation result object to combine
|
|
|
|
* @return $this
|
2008-04-26 08:31:52 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function combineAnd(ValidationResult $other) {
|
2008-04-26 08:31:52 +02:00
|
|
|
$this->isValid = $this->isValid && $other->valid();
|
|
|
|
$this->errorList = array_merge($this->errorList, $other->messageList());
|
2014-01-08 23:03:36 +01:00
|
|
|
|
|
|
|
return $this;
|
2008-04-26 08:31:52 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
|
|
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|