예제 #1
0
 /**
  * @param string $generator_slug
  * @param \JsonLoader\Object|array $value
  * @param array $args {
  *
  * @type string|boolean $generator_class
  * }
  */
 function register_generator($generator_slug, $value, $args = array())
 {
     $args = Util::parse_args($args, array('generator_class' => false, 'element_slug' => false, 'property_name' => Util::underscorify($generator_slug)));
     $generator_slug = Util::dashify($generator_slug);
     if (is_array($value)) {
         if (!$args['generator_class']) {
             /**
              * Get the class name of first element, if an array;
              */
             if (count($value) && is_object(reset($value))) {
                 if (!$args['element_slug']) {
                     $error_msg = "No 'element_slug' defined for generator {$generator_slug} in %s.";
                     Util::log_error(sprintf($error_msg, get_class($this)));
                 }
                 $args['generator_class'] = $this->get_generator_class($args['element_slug'], Util::get_namespace($this));
             } else {
                 $args['generator_class'] = null;
             }
         }
     }
     $generator_class = $args['generator_class'];
     unset($args['generator_class']);
     if ($value instanceof Object) {
         do {
             if ($generator_class && class_exists($generator_class)) {
                 break;
             }
             $namespace = Util::get_namespace($value);
             $try_class1 = "\\{$namespace}\\{$generator_class}";
             if (class_exists($try_class1)) {
                 /**
                  * We must test for \ in class because we can get false positives otherwise
                  */
                 $generator_class = $try_class1;
                 break;
             }
             $try_class2 = $this->get_generator_class($generator_slug, $namespace);
             if (class_exists($try_class2)) {
                 $generator_class = $try_class2;
                 break;
             }
             Util::log_error("None of theses generator classes exist for generator slug \"{$generator_slug}\":" . " [{$generator_class}], [{$try_class1}] nor [{$try_class1}].");
         } while (false);
     }
     if (is_array($value)) {
         $this->generators[$generator_slug] = new ArrayGenerator($generator_class, $value, $this, array('property_name' => Util::underscorify($args['element_slug'])));
     } else {
         if (get_class($this) === ltrim($generator_class, '\\')) {
             /**
              * This is an array element and the generator is defining itself
              */
             /**
              * @var Object $value
              */
             $this->initialize($value, $this);
             $this->set_args($args);
         } else {
             $this->generators[$generator_slug] = new $generator_class($value, $this, $args);
         }
     }
 }