1: <?php
2: /**
3: * This file contains the validator factory class.
4: *
5: * @package Core
6: * @subpackage Validation
7: * @version SVN Revision $Rev:$
8: *
9: * @author Murat Purc <murat@purc.de>
10: * @copyright four for business AG <www.4fb.de>
11: * @license http://www.contenido.org/license/LIZENZ.txt
12: * @link http://www.4fb.de
13: * @link http://www.contenido.org
14: */
15:
16: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
17:
18: /**
19: * Validator factory
20: *
21: * @package Core
22: * @subpackage Validation
23: */
24: class cValidatorFactory {
25: /**
26: * Instantiates and returns the validator. Sets also validators default options.
27: *
28: * Each validator can be configured through CONTENIDO $cfg configuration variable.
29: * Example for email validator:
30: * <pre>
31: * $cfg['validator']['email'] = array(
32: * // List of top level domains to disallow
33: * 'disallow_tld' => array('.test', '.example', '.invalid', '.localhost'),
34: * // List of hosts to disallow
35: * 'disallow_host' => array('example.com', 'example.org', 'example.net'),
36: * // Flag to check DNS records for MX type
37: * 'mx_check' => false,
38: * );
39: * </pre>
40: *
41: * @param string $validator Validator to get
42: * @param array $options Options to use for the validator. Any passed option
43: * overwrites the related option in global validator configuration.
44: * @throws cInvalidArgumentException If type of validator is unknown or not available
45: * @return cValidatorAbstract
46: */
47: public static function getInstance($validator, array $options = array()) {
48: global $cfg;
49:
50: $className = 'cValidator' . ucfirst(strtolower($validator));
51: $fileName = ucfirst(strtolower($validator)) . '.class.php';
52: if (class_exists($className)) {
53: $obj = new $className();
54: } else {
55: $path = str_replace('\\', '/', dirname(__FILE__)) . '/';
56: if (!cFileHandler::exists($path . $fileName)) {
57: throw new cInvalidArgumentException("The class file of Contenido_Validator couldn't included by cValidatorFactory: " . $validator . "!");
58: }
59:
60: require_once($path . $fileName);
61: if (!class_exists($className)) {
62: throw new cInvalidArgumentException("The class of Contenido_Validator couldn't included by uriBuilderFactory: " . $validator . "!");
63: }
64:
65: $obj = new $className();
66: }
67:
68: $cfgName = strtolower($validator);
69:
70: // Merge passed options with global configured options.
71: if (isset($cfg['validator']) && isset($cfg['validator'][$cfgName]) && is_array($cfg['validator'][$cfgName])) {
72: $options = array_merge($cfg['validator'][$cfgName], $options);
73: }
74: $obj->setOptions($options);
75:
76: return new $obj;
77: }
78:
79: }
80: