Overview

Packages

  • Jyxo_Beholder
  • Jyxo_Charset
  • Jyxo_Color
  • Jyxo_Css
  • Jyxo_ErrorHandling
  • Jyxo_FirePhp
  • Jyxo_Gettext
    • Parser
  • Jyxo_Html
  • Jyxo_Input
    • Chain
    • Filter
    • Validator
  • Jyxo_Mail
    • Email
    • Parser
    • Sender
  • Jyxo_Rpc
    • Json
    • Xml
  • Jyxo_Shell
  • Jyxo_SpamFilter
  • Jyxo_Spl
  • Jyxo_String
  • Jyxo_Svn
  • Jyxo_Time
  • Jyxo_Timer
  • Jyxo_Webdav
  • Jyxo_XmlReader
  • PHP

Classes

  • Jyxo_Input_Factory
  • Jyxo_Input_Fluent
  • Jyxo_Input_Upload

Exceptions

  • Jyxo_Input_Exception
  • Overview
  • Package
  • Class
  • Tree
  • Deprecated
  1: <?php
  2: 
  3: /**
  4:  * Jyxo PHP Library
  5:  *
  6:  * LICENSE
  7:  *
  8:  * This source file is subject to the new BSD license that is bundled
  9:  * with this package in the file license.txt.
 10:  * It is also available through the world-wide-web at this URL:
 11:  * https://github.com/jyxo/php/blob/master/license.txt
 12:  */
 13: 
 14: /**
 15:  * Jyxo_Input objects factory.
 16:  *
 17:  * @category Jyxo
 18:  * @package Jyxo_Input
 19:  * @copyright Copyright (c) 2005-2011 Jyxo, s.r.o.
 20:  * @license https://github.com/jyxo/php/blob/master/license.txt
 21:  * @author Jakub Tománek
 22:  */
 23: class Jyxo_Input_Factory
 24: {
 25:     /**
 26:      * Filter class names prefix.
 27:      *
 28:      * @var string
 29:      */
 30:     private static $filterPrefix = array(
 31:         'Jyxo_Input_Filter_'
 32:     );
 33: 
 34:     /**
 35:      * Validator class names prefix.
 36:      *
 37:      * @var string
 38:      */
 39:     private static $validatorPrefix = array(
 40:         'Jyxo_Input_Validator_'
 41:     );
 42: 
 43:     /**
 44:      * Returns a particular validator by its name.
 45:      *
 46:      * @param string $name Validator name
 47:      * @param mixed|array $param Validator constructor parameters. In case of a single parameter it can be its value, an array of values otherwise. NULL in case of no parameter.
 48:      * @return Jyxo_Input_ValidatorInterface
 49:      * @throws Jyxo_Input_Exception No validator of the given name could be found
 50:      */
 51:     public function getValidatorByName($name, $param = null)
 52:     {
 53:         if ($name instanceof Jyxo_Input_ValidatorInterface) {
 54:             return $name;
 55:         }
 56: 
 57:         $params = (array) $param;
 58: 
 59:         $className = $this->findClass($name, self::$validatorPrefix);
 60:         if (!$className) {
 61:             throw new Jyxo_Input_Exception(sprintf('Could not found "%s" validator', $name));
 62:         }
 63: 
 64:         return $this->getClass($className, $params);
 65:     }
 66: 
 67:     /**
 68:      * Returns a particular filter by its name.
 69:      *
 70:      * @param string $name Filter name
 71:      * @param mixed|array $param Filter constructor parameters. In case of a single parameter it can be its value, an array of values otherwise. NULL in case of no parameter.
 72:      * @return Jyxo_Input_FilterInterface
 73:      * @throws Jyxo_Input_Exception No filter of the given name could be found
 74:      */
 75:     public function getFilterByName($name, $param = null)
 76:     {
 77:         if ($name instanceof Jyxo_Input_FilterInterface) {
 78:             return $name;
 79:         }
 80: 
 81:         $params = (array) $param;
 82: 
 83:         $className = $this->findClass($name, self::$filterPrefix);
 84:         if (!$className) {
 85:             throw new Jyxo_Input_Exception(sprintf('Could not found "%s" filter', $name));
 86:         }
 87: 
 88:         return $this->getClass($className, $params);
 89:     }
 90: 
 91:     /**
 92:      * Finds a class by its name and possible prefixes.
 93:      *
 94:      * Returns the first found or NULL if no corresponding class was found.
 95:      *
 96:      * @param string $name Class name
 97:      * @param array $prefixes Class prefixes
 98:      * @return string
 99:      */
100:     private function findClass($name, array $prefixes)
101:     {
102:         $className = null;
103:         $name = ucfirst($name);
104:         foreach ($prefixes as $prefix) {
105:             $tempName = $prefix . $name;
106:             if (class_exists($tempName)) {
107:                 $className = $tempName;
108:                 break;
109:             }
110:         }
111:         return $className;
112:     }
113: 
114:     /**
115:      * Creates a class instance with an arbitrary number of parameters.
116:      *
117:      * @param string $className Class name
118:      * @param array $params Parameters array
119:      * @return object
120:      * @throws ReflectionException An error occurred; the class was probably not found
121:      */
122:     private function getClass($className, array $params)
123:     {
124:         $instance = null;
125:         switch (count($params)) {
126:             case 0:
127:                 $instance = new $className();
128:                 break;
129: 
130:             case 1:
131:                 $instance = new $className(reset($params));
132:                 break;
133: 
134:             default:
135:                 $reflection = new ReflectionClass($className);
136:                 $instance = $reflection->newInstanceArgs($params);
137:                 break;
138:         }
139: 
140:         return $instance;
141:     }
142: 
143:     /**
144:      * Registers a new validator prefix.
145:      *
146:      * The underscore at the end is required; e.g. for class "Api_IsInt" the prefix would be "Api" and validator name "IsInt".
147:      *
148:      * @param string $prefix Validator class prefix
149:      */
150:     public static function addValidatorPrefix($prefix)
151:     {
152:         array_unshift(self::$validatorPrefix, $prefix);
153:     }
154: 
155:     /**
156:      * Registers a new filter prefix.
157:      *
158:      * The underscore at the end is required; e.g. for class "Api_ToInt" the prefix would be "Api" a filter name "ToInt".
159:      *
160:      * @param string $prefix
161:      */
162:     public static function addFilterPrefix($prefix)
163:     {
164:         array_unshift(self::$filterPrefix, $prefix);
165:     }
166: }
167: 
Jyxo PHP Library API documentation generated by ApiGen 2.3.0