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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
<?php
namespace oroboros\core\abstracts\libraries\validation;
/*
* The MIT License
*
* Copyright 2016 Brian Dayhoff <brian@mopsyd.me>.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/**
* Description of AbstractValidator
*
* @author Brian Dayhoff <brian@mopsyd.me>
*/
abstract class AbstractValidationWorker
extends \oroboros\core\abstracts\libraries\worker\AbstractWorker
implements \oroboros\core\interfaces\api\ValidationApi
{
const CLASS_SCOPE = \oroboros\core\interfaces\api\ClassScopeApi::CLASS_SCOPE_WORKER_LIBRARY;
const API = '\\oroboros\\core\\interfaces\\api\\ValidationApi';
const WORKER_TYPE = false;
private $_schema = NULL;
private $_state;
private $_valid_states = array();
/**
* <Nulled validation step>
* Silences validation. Overrule in a child class for conditional validation.
* @param mixed $data
* @return boolean (always true)
*/
public function validate($data, array $schema) {
return TRUE;
}
/**
* <Default schema setter>
* @param \stdClass $schema
*/
protected function _setSchema(\stdClass $schema) {
$this->_schema = $schema;
}
/**
* <Default schema getter>
* returns either the entire schema (if $part=null),
* the specified schema index, or false if specified and not found.
* @param string $part
* @return mixed
*/
protected function _schema($part = NULL) {
return ((isset($part)) ? ((isset($this->_schema->{(string) $part})) ? $this->_schema->{(string) $part} : FALSE) : $this->_schema);
}
protected function _state($state = NULL) {
//return the current state if no parameter is specified
if (!isset($state)) {
return $this->_state;
}
//only causes failure on state change operation
if (empty($this->_valid_states)) {
throw new \OroborosInstallerException("[WARNING] Valid worker states must be registered before the state can be changed.", 1605);
}
//check if attempting to pass an invalid state
if (!in_array($state, $this->_valid_states)) {
throw new \OroborosInstallerException('[ERROR] Invalid worker state: ' . (string) $state . '!', 1655);
}
$this->_state = $state;
$this->_onStateChange();
}
/**
* Registers a list of valid states.
* @param array|object $states Must be an array or iterable object
* @return void
*/
protected function _registerStates($states) {
foreach ($states as $key => $value) {
if (!in_array($value, $this->_valid_states)) {
$this->_valid_states[] = $value;
}
}
}
/**
* Removes all current valid states. Existing state will remain frozen while
* no valid states are present, but will not otherwise change.
*/
protected function _clearStates() {
$this->_valid_states = array();
}
/**
* Null state change.
* Overrule this method in a child class to
* activate state pattern awareness as needed.
* @return void
*/
protected function _onStateChange() {
}
}