/** * @group default */ public function testFromArray() { $data = ['name' => 'George', 'description' => 'My cuddly little TemplateInfo', 'usage' => 'I shall hug him and pet him and squeeze him...', 'see' => 'https://www.youtube.com/watch?v=ArNz8U7tgU4', 'license' => 'artistic']; $info = TemplateInfo::fromArray($data); $this->assertEquals('George', $info->getName()); $this->assertEquals($data['description'], $info->getDescription()); $this->assertEquals($data['usage'], $info->getUsage()); $this->assertEquals($data['see'], $info->getSee()); $this->assertEquals(['license' => 'artistic'], $info->getInfo()); $this->assertTrue(isset($info->license)); $this->assertEquals('artistic', $info->license); }
/** * A utility function to load a template from an .ini file which does * _not_ perform static initialization and therefore can be used during * static initialization. * @param string $filename The filename of the .ini file to load. * @param \EVought\vCardTools\Template $fallback A Template to use as a fallback * for undefined fragments. If null, this method will look for '_fallback' * in the .ini file and will use it, *if* a Template by that name is * registered. * @return \EVought\vCardTools\Template * @throws \DomainException If the file does not exist or is not readable. * @throws \DomainException If the key '_template' in the .ini file does * not contain the expected components. * @throws \RuntimeException If an error occurs while reading the .ini file. */ private static function i_fromINI($filename, Template $fallback = null) { assert(!empty($filename), '$filename may not be empty'); if (!is_readable($filename)) { throw new \DomainException('Filename, ' . $filename . 'must exist and be readable'); } $fragments = parse_ini_file($filename); if (false === $fragments) { throw new \RuntimeException('Failed to load INI file ' . $filename); } if (null === $fallback && array_key_exists('_fallback', $fragments) && array_key_exists($fragments['_fallback'], self::$templateRegistry)) { $fallback = self::$templateRegistry[$fragments['_fallback']]; } if (null === $fallback && array_key_exists('_fallback_file', $fragments)) { $fallback = self::i_fromINI($fragments['_fallback_file']); } unset($fragments['_fallback']); unset($fragments['_fallback_file']); if (array_key_exists('_template', $fragments)) { if (!is_array($fragments['_template'])) { throw new \DomainException('_template must be an array and ' . 'should contain informational ' . 'keys and values about the ' . 'Template'); } $info = TemplateInfo::fromArray($fragments['_template']); unset($fragments['_template']); } else { $info = new TemplateInfo(); } $template = new Template($fragments, $fallback, $info); if (null !== $template->getName()) { self::$templateRegistry[$template->getName()] = $template; } return $template; }