Пример #1
0
 /**
  * Cast parameters
  *
  * Takes the provided parameters from the request, and attempts to cast them
  * to objects, if the prototype defines any as explicit object types
  * 
  * @param  Reflection $reflectionMethod 
  * @param  array $params 
  * @return array
  */
 protected function _castParameters($reflectionMethod, array $params)
 {
     $prototypes = $reflectionMethod->getPrototypes();
     $nonObjectTypes = array('null', 'mixed', 'void', 'unknown', 'bool', 'boolean', 'number', 'int', 'integer', 'double', 'float', 'string', 'array', 'object', 'stdclass');
     $types = array();
     foreach ($prototypes as $prototype) {
         foreach ($prototype->getParameters() as $parameter) {
             $type = $parameter->getType();
             if (in_array(strtolower($type), $nonObjectTypes)) {
                 continue;
             }
             $position = $parameter->getPosition();
             $types[$position] = $type;
         }
     }
     if (empty($types)) {
         return $params;
     }
     foreach ($params as $position => $value) {
         if (!isset($types[$position])) {
             // No specific type to cast to? done
             continue;
         }
         $type = $types[$position];
         if (!class_exists($type)) {
             // Not a class, apparently. done
             continue;
         }
         if ($value instanceof $type) {
             // Already of the right type? done
             continue;
         }
         if (!is_array($value) && !is_object($value)) {
             // Can't cast scalars to objects easily; done
             continue;
         }
         // Create instance, and loop through value to set
         $object = new $type();
         foreach ($value as $property => $defined) {
             $object->{$property} = $defined;
         }
         $params[$position] = $object;
     }
     return $params;
 }