public function filterArray(array $arr, string $filterParameter, callable $filterCallback) : array
public function sortArray($arr, $filterParam) : array // <- { // ... }
/** * Class ArrayForSorting * , - , . */ class ArrayForSorting { /** * . * * @var array */ public $arrayForSorting; /** * @construct */ public function __construct($arrayForSorting) { $this->arrayForSorting = $arrayForSorting; } } /** * Class UserSortArray * , : , , . */ class UserSortArray { /** * . * * @var object */ public $availableSortingMethods; /** * . * * @param ArrayForSorting $sortArray , . * * @throws UserSortArrayException . */ public function insertSort(ArrayForSorting &$sortArray) { if (false === isset($availableSortMethods->insertMethod)) { throw new UserSortArrayException('Insert method for user array sort is not available.'); } return uasort($sortArray->arrayForSorting, $availableSortMethods->bubbleMethod); } }
class ArraySorter { public function sortArray(array &$sortArray, $userCallback) { // , // , // false - -1. if (false === $this->validateArray($sortArray)) { return []; } return uasort($sortArray, $userCallback); } private function validateArray($array) { if (!isset($array) || false === is_array($array)) { return false; } return true; } }
if (null !== $param && '' !== $param) { return false; // [], '', // throw new Exception(__CLASS__ . __FUNCTION__ . ": Expected integer, got sting"); }
<?php namespace traits; /** * Trait Validator * . */ trait Validator { /** * . * * @param array $validationParams . * : '' => . * 'not_empty' -- * (.. , , ). * : * [ * 'integer' => 123, * 'string not_empty' => 'hello world!', * 'array' => [ ... ], * ] * * @return bool true . * * @throws \Exception . */ public function validate($validationParams) { // , . $this->validateArray($validationParams); foreach ($validationParams as $type => $value) { $methodName = 'validate' . ucfirst($type); // validateInteger $isEmptinessValidation = false; if ('not_empty' === substr($type, -9)) { $methodName = 'validate' . ucfirst(substr($type, 0, -9)); $isEmptinessValidation = true; } if (false === method_exists($this, $methodName)) { throw new \Exception("Trait 'Validator' does not have method '{$methodName}'."); } // true, , . $this->{$methodName}($value, $isEmptinessValidation); } return true; } /** * . * * @param string $string . * @param bool $isValidateForEmptiness . * * @return bool . */ public function validateString($string, $isValidateForEmptiness) { $validationRules = is_string($string) && $this->validateForSetAndEmptiness($string, $isValidateForEmptiness); if (false === $validationRules) { $this->throwError('string', gettype($string)); } return true; } /** * . * * @param boolean $bool . * * @return bool . */ public function validateBoolean($boolean, $isValidateForEmptiness = false) { $validationRules = isset($boolean) && is_bool($boolean); if (false === $validationRules) { $this->throwError('boolean', gettype($boolean)); } return true; } /** * . * * @param string $array . * @param bool $isValidateForEmptiness . * * @return bool . */ public function validateArray($array, $isValidateForEmptiness) { $validationRules = is_array($array) && $this->validateForSetAndEmptiness($array, $isValidateForEmptiness); if (false === $validationRules) { $this->throwError('array', gettype($array)); } return true; } /** * . * * @param string $object . * @param bool $isValidateForEmptiness . * * @return bool . */ public function validateObject($object, $isValidateForEmptiness) { $validationRules = is_object($object) && $this->validateForSetAndEmptiness($object, $isValidateForEmptiness); if (false === $validationRules) { $this->throwError('object', gettype($object)); } return true; } /** * . * * @param string $integer . * @param bool $isValidateForEmptiness . * * @return bool . */ public function validateInteger($integer, $isValidateForEmptiness) { $validationRules = is_int($integer) && $this->validateForSetAndEmptiness($integer, false); if (false === $validationRules) { $this->throwError('integer', gettype($integer)); } return true; } /** * , . * * @param string $parameter . * @param bool $isValidateForEmptiness (, , ) . * * @return bool . */ private function validateForSetAndEmptiness($parameter, $isValidateForEmptiness) { $isNotEmpty = true; if (true === $isValidateForEmptiness) { $isNotEmpty = false === empty($parameter); } return isset($parameter) && true === $isNotEmpty; } /** * . * * @param string $expectedType * @param string $gotType * * @throws \Exception . */ private function throwError($expectedType, $gotType) { $validatorMethodName = ucfirst($expectedType) . 'Validator'; // integer -> IntegerValidator throw new \Exception("Parse error: {$validatorMethodName} expected type {$expectedType}, got {$gotType}"); } }
namespace models; use traits; /** * Class Notebook * . */ class Notebook { use \traits\Validator; /** * ID . * * @var string */ private $_uid; /** * @construct */ public function __construct() { $this->_uid = $this->generateUniqueIdentifier(); } /** * ID . * * @return string */ public function getNotebookUID() { // validate() // 'primitiveName' => $primitiveValue. // , // . $this->validate([ 'string not_empty' => $this->_uid, ]); return $this->_uid; } /** * ID . * * @return string */ private function generateUniqueIdentifier() { $uniqueIdentifier = bin2hex(openssl_random_pseudo_bytes(40)); // . $this->validate([ 'string not_empty' => $uniqueIdentifier, ]); return $uniqueIdentifier; } }
<?php namespace models; use traits; /** * Class Pen * . */ class Pen { use \traits\Validator; /** * . * * @var double */ private $remainingAmountOfInk; /** * @construct */ public function __construct() { $this->remainingAmountOfInk = 100; } /** * . * * @param string $message . * * @return void * * @throws ValidatorException . */ public function drawMessage($message) { $this->validate([ 'string' => $message, ]); if (0 > $this->remainingAmountOfInk) { echo 'Ink ended'; // } echo 'Pen writes message: ' . $message . '<br>' . PHP_EOL; $this->remainingAmountOfInk -= 1; } /** * . * * @return integer */ public function getRemainingAmountOfInk() { $this->validate([ 'double' => $this->remainingAmountOfInk, ]); return $this->remainingAmountOfInk; } }
// autoload.php - . /** * Class Autoloader * . */ class AutoLoader { /** * . * * @param string $className . */ public static function loadClasses($className) { $dir = dirname(__FILE__); $sep = DIRECTORY_SEPARATOR; require_once("{$dir}{$sep}{$className}.php"); } } // . spl_autoload_register([ 'AutoLoader', 'loadClasses' ]); // ------------------------------------ // index.php include_once("autoload.php"); use models as m; $pen = new m\Pen(); $pen->drawMessage('hi habrahabr'); // Pen writes message: hi habrahabr $message = [ 'message' => 'hi im message inside array', ]; try { $pen->drawMessage($message); // ValidatorException } catch (\Exception $e) { echo 'exception was throwed during validation of message <br>' . PHP_EOL; }
Pen writes message: hi habrahabr exception was throwed during validation of message
Source: https://habr.com/ru/post/329360/
All Articles