2008-03-22 20:26:04 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fluent interface for validating the contents of member variables.
|
|
|
|
* This should be immutable. See HTMLPurifier_ConfigSchema_Validator for
|
2008-03-22 21:06:55 +00:00
|
|
|
* use-cases. We name this an 'atom' because it's ONLY for validations that
|
|
|
|
* are independent and usually scalar.
|
2008-03-22 20:26:04 +00:00
|
|
|
*/
|
|
|
|
class HTMLPurifier_ConfigSchema_ValidatorAtom
|
|
|
|
{
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-03-22 21:06:55 +00:00
|
|
|
protected $context, $obj, $member, $contents;
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-03-22 21:06:55 +00:00
|
|
|
public function __construct($context, $obj, $member) {
|
2008-03-22 20:26:04 +00:00
|
|
|
$this->context = $context;
|
|
|
|
$this->obj = $obj;
|
|
|
|
$this->member = $member;
|
|
|
|
$this->contents =& $obj->$member;
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-03-22 20:26:04 +00:00
|
|
|
public function assertIsString() {
|
|
|
|
if (!is_string($this->contents)) $this->error('must be a string');
|
|
|
|
return $this;
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-04-04 21:33:37 +00:00
|
|
|
public function assertIsBool() {
|
|
|
|
if (!is_bool($this->contents)) $this->error('must be a boolean');
|
|
|
|
return $this;
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-04-04 21:33:37 +00:00
|
|
|
public function assertIsArray() {
|
|
|
|
if (!is_array($this->contents)) $this->error('must be an array');
|
|
|
|
return $this;
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-03-22 20:26:04 +00:00
|
|
|
public function assertNotNull() {
|
2008-03-22 21:06:55 +00:00
|
|
|
if ($this->contents === null) $this->error('must not be null');
|
2008-03-22 20:26:04 +00:00
|
|
|
return $this;
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-03-22 20:26:04 +00:00
|
|
|
public function assertAlnum() {
|
2008-03-22 21:06:55 +00:00
|
|
|
$this->assertIsString();
|
2008-03-22 20:26:04 +00:00
|
|
|
if (!ctype_alnum($this->contents)) $this->error('must be alphanumeric');
|
|
|
|
return $this;
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-03-22 20:26:04 +00:00
|
|
|
public function assertNotEmpty() {
|
|
|
|
if (empty($this->contents)) $this->error('must not be empty');
|
|
|
|
return $this;
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-04-04 21:33:37 +00:00
|
|
|
public function assertIsLookup() {
|
|
|
|
$this->assertIsArray();
|
|
|
|
foreach ($this->contents as $v) {
|
|
|
|
if ($v !== true) $this->error('must be a lookup array');
|
|
|
|
}
|
|
|
|
return $this;
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-03-22 20:26:04 +00:00
|
|
|
protected function error($msg) {
|
2008-03-23 01:06:35 +00:00
|
|
|
throw new HTMLPurifier_ConfigSchema_Exception(ucfirst($this->member) . ' in ' . $this->context . ' ' . $msg);
|
2008-03-22 20:26:04 +00:00
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-03-22 20:26:04 +00:00
|
|
|
}
|
|
|
|
|
2008-12-06 09:24:59 +00:00
|
|
|
// vim: et sw=4 sts=4
|