| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- <?php
- namespace cx_newsletter;
- abstract class validator
- implements validator_interface {
- public function __construct(\wpdb $database, table_names $tables) {
- $this->database = $database;
- $this->target = null;
- $this->error = null;
- $this->prevalidate($database, $tables);
- }
- protected abstract function prevalidate(
- \wpdb $database,
- table_names $tables
- ) : void;
- public function check() : self {
- $result = $this->validate();
- $this->error = null;
-
- if ($result !== null) {
- $this->make_error($result);
- }
- return $this;
- }
- protected abstract function validate() : ?string;
- protected function is_loaded() : bool {
- return $this->target !== null;
- }
- protected function get_target() : mixed {
- if (!$this->is_loaded()) {
- throw new \exception('Must load target before get it.');
- }
- return $this->target;
- }
- public function load(mixed $target) : self {
- $this->target = $target;
- return $this;
- }
- protected function make_error(string $content) : void {
- if (!$this->is_valid()) {
- throw new \exception('This validator has already error.');
- }
- $this->error = $content;
- }
- public function is_valid() : bool {
- return $this->error === null;
- }
- public function error_on() : string {
- if ($this->is_valid()) {
- throw new \exception('Trying to load error on valid object.');
- }
- return $this->error;
- }
- private mixed $target;
- private ?string $error;
- }
|