1<?php 2 3/** 4 * @see https://github.com/laminas/laminas-validator for the canonical source repository 5 * @copyright https://github.com/laminas/laminas-validator/blob/master/COPYRIGHT.md 6 * @license https://github.com/laminas/laminas-validator/blob/master/LICENSE.md New BSD License 7 */ 8 9namespace Laminas\Validator; 10 11use Laminas\ServiceManager\ServiceManager; 12 13class StaticValidator 14{ 15 /** 16 * @var ValidatorPluginManager 17 */ 18 protected static $plugins; 19 20 /** 21 * Set plugin manager to use for locating validators 22 * 23 * @param ValidatorPluginManager|null $plugins 24 * @return void 25 */ 26 public static function setPluginManager(ValidatorPluginManager $plugins = null) 27 { 28 // Don't share by default to allow different arguments on subsequent calls 29 if ($plugins instanceof ValidatorPluginManager) { 30 // Vary how the share by default flag is set based on laminas-servicemanager version 31 if (method_exists($plugins, 'configure')) { 32 $plugins->configure(['shared_by_default' => false]); 33 } else { 34 $plugins->setShareByDefault(false); 35 } 36 } 37 static::$plugins = $plugins; 38 } 39 40 /** 41 * Get plugin manager for locating validators 42 * 43 * @return ValidatorPluginManager 44 */ 45 public static function getPluginManager() 46 { 47 if (null === static::$plugins) { 48 static::setPluginManager(new ValidatorPluginManager(new ServiceManager)); 49 } 50 return static::$plugins; 51 } 52 53 /** 54 * @param mixed $value 55 * @param string $classBaseName 56 * @param array $options OPTIONAL associative array of options to pass as 57 * the sole argument to the validator constructor. 58 * @return bool 59 * @throws Exception\InvalidArgumentException for an invalid $options argument. 60 */ 61 public static function execute($value, $classBaseName, array $options = []) 62 { 63 if ($options && array_values($options) === $options) { 64 throw new Exception\InvalidArgumentException( 65 'Invalid options provided via $options argument; must be an associative array' 66 ); 67 } 68 69 $plugins = static::getPluginManager(); 70 71 $validator = $plugins->get($classBaseName, $options); 72 return $validator->isValid($value); 73 } 74} 75