$match = AMatch::runMatch($params) ->doc_id(0, '<') // ->subject_id(0, '!=') // ; $result = $match->stopMatch(); if (!$result) { die(var_export($match->matchComments(), true)); // }
$params = array( 'subject_id' => '64', 'parent_id' => -32, 'delimeter' => '-4.645E+32', 'title' => 'New document', 'links' => array(13, '-16', 24), 'email' => 'someuser@mail.dom', ); $params_bad = array( 'subject_id' => '64.43', 'parent_id' => array(), 'delimeter' => '-4.x6E.32', 'title' => new stdClass(), 'links' => array(0, array(0, array(0)), 0), 'email' => 'someuser!@mail.dom', );
function result(AMatch $match) { echo PHP_EOL; echo $match->stopMatch() ? 'Dance!' : 'Cry!' ; echo PHP_EOL; var_export($match->matchResults()); echo PHP_EOL; var_export($match->matchComments()); echo PHP_EOL; var_export($match->matchCommentsConditions()); }
$match = AMatch::runMatch($params, AMatch::FLAG_SHOW_GOOD_COMMENTS)->delimeter('', 'float'); // result($match); $match = AMatch::runMatch($params_bad, AMatch::FLAG_SHOW_GOOD_COMMENTS)->delimeter('', 'float'); result($match);
// Dance! array ( 'delimeter' => 103, ) array ( 'delimeter' => 'OK. Expected parameter type is valid', ) array ( 'delimeter' => array ( 0 => '', 1 => 'float', ), ) // Cry! array ( 'delimeter' => 3, ) array ( 'delimeter' => 'Expected parameter type is not valid', ) array ( 'delimeter' => array ( 0 => 'float', 1 => 'float', ), )
function mapping(AMatch $match) { // $errors_mapping = array( AMatchStatus::KEY_TYPE_NOT_VALID => 'invalid_type', AMatchStatus::KEY_CONDITION_NOT_VALID => 'invalid_data', AMatchStatus::KEY_NOT_EXISTS => 'required', ); $results = $match->matchResults(); // $comments = $match->matchComments(); // $comments_conditions = $match->matchCommentsConditions(); // $output = array(); foreach ($results as $param => $result) { $error = array_key_exists($result, $errors_mapping) ? $errors_mapping[$result] : 'other_errors'; // , $comment = $param . ': ' . $comments[$param]; if (isset($comments_conditions[$param]) && !empty($comments_conditions[$param][0])) { $comment .= ' (' . $comments_conditions[$param][0] . ')'; // } $output[$error][] = $comment; } var_export($output); } $match = AMatch::runMatch($params_bad, AMatch::FLAG_SHOW_GOOD_COMMENTS | AMatch::FLAG_DONT_STOP_MATCHING) ->title('', 'string') // string ->parent_id('', 'int') // string ->ineedkey() // ->subject_id(1, '>') // "1" ->delimeter('', 'blabla') // ; mapping($match);
array ( 'invalid_type' => array ( 0 => 'title: Expected parameter type is not valid (string)', 1 => 'parent_id: Expected parameter type is not valid (int)', ), 'required' => array ( 0 => 'ineedkey: Expected parameter does not exist in the array of parameters', ), 'invalid_data' => array ( 0 => 'subject_id: Condition is not valid (1)', ), 'other_errors' => array ( 0 => 'delimeter: Condition is unknown', ), )
... ->title('', 'string', 'Incorrect document title. Please, read FAQ.') ...
array ( 'other_errors' => array ( 0 => 'title: Incorrect document title. Please, read FAQ. (string)', 1 => 'delimeter: Condition is unknown', ),
class AMatchRussian extends AMatchStatus { protected function _fillComments() { parent::_fillComments(); // , $this->_result_comments[self::KEY_NOT_EXISTS] = ', . '; $this->_result_comments[self::KEY_CONDITION_NOT_VALID] = ' , '; $this->_result_comments[self::CONDITION_IS_UNKNOWN] = ''; } } $match = AMatch::runMatch($params_bad, AMatch::FLAG_SHOW_GOOD_COMMENTS | AMatch::FLAG_DONT_STOP_MATCHING, new AMatchRussian()) // ... , mapping ; mapping($match);
array ( 'other_errors' => array ( 0 => 'title: Incorrect document title. Please, read FAQ. (string)', 1 => 'delimeter: ', ), 'invalid_type' => array ( 0 => 'parent_id: Expected parameter type is not valid (int)', ), 'required' => array ( 0 => 'ineedkey: , . ', ), 'invalid_data' => array ( 0 => 'subject_id: (1)', ), )
$match->data(array($this, 'callbackMethod'), 'callback');
// param([mixed $callback_argument], [callable|callable $callback]) // // param([callable|string $callback], 'callback') // : ->param($callback_property, 'MyClass::myFunc') ->param($callback_property, 'MyClass->myFunc') ->param($callback_property, array($my_obj, 'myFunc')) ->param($callback_property, array('MyClass', 'myFunc'))
function matchCallbacks($params) { $match = AMatch::runMatch($params, AMatch::FLAG_DONT_STOP_MATCHING) ->parent_id('/^-?\d+$/', 'AMatchString::pregMatch') // ->title(12, 'AMatchString::length') // ->email('([\w-]+@([\w-]+\.)+[\w-]+)', 'AMatchString::isEmail') // email ( ) ->links(AMatchArray::FLAG_EMPTY_SOME_ELEMENT, 'AMatchArray::isNotEmpty') // : - ; result($match); } matchCallbacks($params); matchCallbacks($params_bad);
// Dance! array ( ) array ( ) array ( ) // Cry! array ( 'parent_id' => 'str3', 'title' => 'str5', 'email' => 'str4', 'links' => 'arr8', ) array ( 'parent_id' => 'The string does not match the regular expression', 'title' => 'String required', 'email' => 'Incorrect email', 'links' => 'At least one element of the array must be non-empty', ) array ( 'parent_id' => array ( 0 => '/^-?\\d+$/', 1 => 'AMatchString::pregMatch', ), 'title' => array ( 0 => 12, 1 => 'AMatchString::length', ), 'email' => array ( 0 => 'someuser!@mail.dom', 1 => 'AMatchString::isEmail', ), 'links' => array ( 0 => NULL, 1 => 'AMatchArray::isNotEmpty', ), )
$match = AMatch::runMatch(array $associative_array, bitmask $flags, AMatchStatus $obj) ->_([___], []) ->…; $match->stopMatch(); // $match->matchResults(); // ( , ) $match->matchComments(); // $match->matchCommentsConditions(); //
Source: https://habr.com/ru/post/150039/
All Articles