private function encode(Element $element) { $chromosomeLength = count($element->getProperties()); $this->elementId = $element->getId(); $this->elementName = $element->getCssTag(); $newChromosome = new SplFixedArray($chromosomeLength); $chromosomeIndex = 0; foreach ($element->getProperties() as $property) { $newChromosome[$chromosomeIndex] = $property->getRandomValue(); $chromosomeIndex++; } return $newChromosome; }
function decode($id, Individual $individual, Element $element) { $cssCode = "{$element->getCssTag()}#individual{$id} {" . PHP_EOL; $chromosome = $individual->getChromosome()->toArray(); $geneIndex = 0; foreach ($element->getProperties() as $property) { if ($property->getCssName() == "font-family") { $cssCode .= "{$property->getCssName()} : '{$property->getValue($chromosome[$geneIndex])}', serif; "; } else { $cssCode .= "{$property->getCssName()} : {$property->getValue($chromosome[$geneIndex])}; "; } $geneIndex++; } $cssCode .= "}"; return $cssCode; }
/** * Radio element type * @param Element $element * @param $fieldHtml * @throws \Exception */ protected static function radio(Element &$element, &$fieldHtml) { $name = $element->getName(); $id = $element->getId(); $value = $element->getValue(); if ($element->getType() == self::TYPE_MULTICHECKBOX && !is_array($value)) { $value = array(); } $options = $element->getProperties('options'); if (!is_array($options)) { throw new \Exception("Options for field type '{$element->getType()}' must be given as array."); } //classes $class = $element->getProperties('class'); if (!is_array($class)) { $class = array(); } $attributes = $element->getProperties('attributes'); $extraAttributes = ''; if (is_array($attributes)) { foreach ($attributes as $attr => $val) { if (is_string($attr)) { $extraAttributes .= sprintf('%s="%s" ', $attr, $val); } else { $extraAttributes .= sprintf('%s ', $val); } } } foreach ($options as $option => $lbl) { $optionAttributes = $extraAttributes; //does this option have its own settings? if (is_array($lbl)) { $optionSettings = $lbl; if (!isset($optionSettings['label'])) { throw new \Exception("A label has not been set for one of the arrayed options for field - " . $element->getName(true)); } $lbl = $optionSettings['label']; if (isset($optionSettings['attributes']) && is_array($optionSettings['attributes'])) { foreach ($optionSettings['attributes'] as $attr => $val) { if (is_string($attr)) { $optionAttributes .= sprintf('%s="%s" ', $attr, $val); } else { $optionAttributes .= sprintf('%s ', $val); } } } } $labelHtml = self::$templates['label']; $labelHtml = str_replace('{{id}}', "{$id}_{$option}", $labelHtml); $labelHtml = str_replace('{{text}}', $lbl, $labelHtml); if ($element->getType() == self::TYPE_RADIO) { $field = self::$templates[self::TYPE_RADIO]; $field = str_replace('{{name}}', $name, $field); $field = str_replace('{{value}}', 'value="' . $option . '" ' . ($option == $value ? ' checked="checked"' : ''), $field); } else { if ($element->getType() == self::TYPE_MULTICHECKBOX) { $field = self::$templates[self::TYPE_CHECKBOX]; $field = str_replace('{{name}}', "{$name}[]", $field); $field = str_replace('{{value}}', 'value="' . $option . '" ' . (in_array($option, $value) ? ' checked="checked"' : ''), $field); } } $field = str_replace('{{id}}', "{$id}_{$option}", $field); $field = str_replace('{{class}}', implode(' ', $class), $field); $field = str_replace('{{attributes}}', $optionAttributes, $field); $field .= ' ' . $labelHtml; $options[$option] = "<div>{$field}</div>"; } $fieldHtml = '<div>' . implode('', $options) . '</div>'; }