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