Пример #1
0
 /**
  * @covers Opt_Cdf_Lexer
  * @covers Opt_Cdf_Parser
  * @covers Opt_Cdf_Loader::load
  * @covers Opt_Cdf_Loader::_addDefinition
  */
 public function testMultilineComments2()
 {
     $locator = $this->getMock('Opt_Cdf_Locator_Interface', array('getElementLocation'));
     $locator->expects($this->once())->method('getElementLocation')->will($this->returnValue(array()));
     $this->_loader->load(self::PATH . 'comments_multiline.cdf');
     $this->assertEquals('Opt_Format_Objective', get_class($this->_manager->getFormat('bar', 'joe', $locator)));
 }
Пример #2
0
 /**
  * @covers Opt_Cdf_Manager::getFormat
  * @covers Opt_Cdf_Manager::addFormat
  * @covers Opt_Cdf_Manager::setLocality
  * @covers Opt_Cdf_Manager::clearLocals
  * @expectedException Opt_NoMatchingFormat_Exception
  */
 public function testDisablingLocalDefinitions()
 {
     $locator = $this->getMock('Opt_Cdf_Locator_Interface', array('getElementLocation'));
     $locator->expects($this->exactly(2))->method('getElementLocation')->will($this->returnValue(array()));
     $this->_obj->setLocality(Opt_Cdf_Manager::AS_LOCAL);
     $this->_obj->addFormat('foo', 'bar', 'Array', array());
     $this->_obj->addFormat('joe', 'goo', 'Array', array());
     $this->assertTrue($this->_obj->getFormat('foo', 'bar', $locator) instanceof Opt_Format_Array);
     $this->_obj->clearLocals();
     $this->_obj->getFormat('joe', 'goo', $locator);
 }
Пример #3
0
 /**
  * Loads the specified CDF file.
  *
  * @param string $filename The CDF file name.
  */
 public function load($filename)
 {
     // If the file has already been loaded, skip the parsing process.
     if (in_array($filename, $this->_loaded)) {
         return;
     }
     $this->_loaded[] = $filename;
     // Initialize the parser and lexer and parse everything.
     $lexer = new Opt_Cdf_Lexer($filename);
     $parser = new Opt_Cdf_Parser($this);
     $this->_definitions = array();
     while ($lexer->yylex()) {
         if ($lexer->token != 'w') {
             $parser->doParse($lexer->token, $lexer->value);
         }
     }
     $parser->doParse(0, 0);
     // Now register everything in the manager
     foreach ($this->_definitions as $definition) {
         foreach ($definition[0] as $group) {
             $last = reset($group);
             array_shift($group);
             // Concatenate the list for the locator
             $fullyQualifiedPath = array();
             foreach ($group as $item) {
                 if ($item[0] !== null && $item[1] !== null) {
                     $fullyQualifiedPath[] = $item[0] . '#' . $item[1];
                 } elseif ($item[0] != null) {
                     $fullyQualifiedPath[] = '#' . $item[1];
                 } else {
                     $fullyQualifiedPath[] = $item[0] . '#';
                 }
             }
             // Add the format definition to the manager
             if (isset($definition[1]['data-format'])) {
                 $this->_manager->addFormat($last[0], $last[1], $definition[1]['data-format'], $fullyQualifiedPath);
             }
         }
     }
 }
Пример #4
0
 /**
  * Allows to export the data format configuration to the compiler.
  *
  * @param Array $list An associative array of pairs "variable => format description"
  */
 public function addFormats(array $globalCdf, array $localCdf, array $globalDefs, array $localDefs)
 {
     $manager = $this->getCdfManager();
     if (sizeof($globalCdf) > 0 || sizeof($localCdf) > 0) {
         // Initialize the loader
         if ($this->_cdfLoader === null) {
             $this->_cdfLoader = new Opt_Cdf_Loader($manager);
         }
         $manager->setLocality(Opt_Cdf_Manager::AS_GLOBAL);
         foreach ($globalCdf as $cdf) {
             $this->_cdfLoader->load($this->_tpl->cdfDir . $cdf);
         }
         foreach ($localCdf as $cdf) {
             $manager->setLocality($cdf);
             $this->_cdfLoader->load($this->_tpl->cdfDir . $cdf);
         }
         $this->_cdfManager->setLocals($localCdf);
     }
     // Now manual definitions...
     $manager->setLocality(Opt_Cdf_Manager::AS_GLOBAL);
     foreach ($globalDefs as $item => &$idList) {
         foreach ($idList as $id => $format) {
             $this->_cdfManager->addFormat($item, $id, $format, array());
         }
     }
     $manager->setLocality(Opt_Cdf_Manager::AS_LOCAL);
     foreach ($localDefs as $item => &$idList) {
         foreach ($idList as $id => $format) {
             $this->_cdfManager->addFormat($item, $id, $format, array());
         }
     }
 }
Пример #5
0
 /**
  * Performs a data format type casting. If the data format does not match
  * the suggested one, it locates the data format class and executes the
  * conversion action. Returns the modified code.
  *
  * @param Opt_Cdf_Manager $manager The CDF manager object.
  * @param string $code The code that may be casted
  * @param string $actual The actual data format
  * @param string $suggested The suggested data format
  * @return string
  */
 public static function cast(Opt_Cdf_Manager $manager, $code, $actual, $suggested)
 {
     if ($actual == $suggested) {
         return $code;
     }
     // Type casting goes here.
     if ($actual instanceof Opt_Format_Class) {
         $className = get_class($actual);
         $modified = $className::cast($suggested, $code, $actual);
     } else {
         $className = $manager->getFormatClass($actual);
         $modified = $className::cast($suggested, $code);
     }
     if ($modified === null) {
         throw new Opt_FormatCasting_Exception($actual, $suggested);
     }
     return $modified;
 }