public function __construct() { $odr = tubepress_impl_patterns_sl_ServiceLocator::getOptionDescriptorReference(); $this->_disabledParticipantsOptionDescriptor = $odr->findOneByName(tubepress_api_const_options_names_OptionsUi::DISABLED_OPTIONS_PAGE_PARTICIPANTS); parent::__construct(self::FIELD_ID, $this->_disabledParticipantsOptionDescriptor->getName(), $this->_disabledParticipantsOptionDescriptor->getDescription()); $this->setUntranslatedDisplayName($this->_disabledParticipantsOptionDescriptor->getLabel()); }
public function __construct($optionDescriptorName) { $odr = tubepress_impl_patterns_sl_ServiceLocator::getOptionDescriptorReference(); $this->_optionDescriptor = $odr->findOneByName($optionDescriptorName); if ($this->_optionDescriptor === null) { throw new InvalidArgumentException(sprintf('Could not find option with name "%s"', $optionDescriptorName)); } parent::__construct($optionDescriptorName, $this->_optionDescriptor->getLabel(), $this->_optionDescriptor->getDescription()); }
public function onBoot(tubepress_api_event_EventInterface $event) { $storageManager = tubepress_impl_patterns_sl_ServiceLocator::getOptionStorageManager(); $odr = tubepress_impl_patterns_sl_ServiceLocator::getOptionDescriptorReference(); $allOptions = $odr->findAll(); $toPersist = array(); /** * @var $optionDescriptor tubepress_spi_options_OptionDescriptor */ foreach ($allOptions as $optionDescriptor) { if ($optionDescriptor->isMeantToBePersisted()) { $toPersist[$optionDescriptor->getName()] = $optionDescriptor->getDefaultValue(); } } $storageManager->createEachIfNotExists($toPersist); }
public function onSingleVideoTemplate(tubepress_api_event_EventInterface $event) { $template = $event->getSubject(); $context = tubepress_impl_patterns_sl_ServiceLocator::getExecutionContext(); $messageService = tubepress_impl_patterns_sl_ServiceLocator::getMessageService(); $optionDescriptorReference = tubepress_impl_patterns_sl_ServiceLocator::getOptionDescriptorReference(); $metaNames = tubepress_impl_util_LangUtils::getDefinedConstants('tubepress_api_const_options_names_Meta'); $shouldShow = array(); $labels = array(); foreach ($metaNames as $metaName) { $optionDescriptor = $optionDescriptorReference->findOneByName($metaName); $shouldShow[$metaName] = $context->get($metaName); $labels[$metaName] = $messageService->_($optionDescriptor->getLabel()); } $template->setVariable(tubepress_api_const_template_Variable::META_SHOULD_SHOW, $shouldShow); $template->setVariable(tubepress_api_const_template_Variable::META_LABELS, $labels); }
/** * Queue a name-value pair for storage. * * @param string $optionName The option name. * @param mixed $optionValue The option value. * * @return string|null Null if the option was accepted for storage, otherwise a string error message. */ public final function queueForSave($optionName, $optionValue) { /** * Init the queue if need be. */ if (!isset($this->_saveQueue)) { $this->_saveQueue = array(); } /** * First filter the incoming option. */ $filteredValue = $this->_getFilteredValue($optionName, $optionValue); /** * Now validate it. */ $validationResult = $this->_validateOneForStorage($optionName, $filteredValue); if ($validationResult !== true) { return $validationResult; } $optionDescriptorReferenceService = tubepress_impl_patterns_sl_ServiceLocator::getOptionDescriptorReference(); $optionDescriptor = $optionDescriptorReferenceService->findOneByName($optionName); if (!$optionDescriptor->isMeantToBePersisted()) { /** * Not meant to be persisted. Just ignore. */ return null; } if ($this->_noChangeBetweenIncomingAndCurrent($filteredValue, $optionDescriptor)) { /** * No change. Ignore. */ return null; } /** * Option passed validation and is meant to be persisted. */ $this->_saveQueue[$optionName] = $filteredValue; return null; }
/** * Gets the failure message of a name/value pair that has failed validation. * * @param string $optionName The option name * @param mixed $candidate The candidate option value * * @return mixed Null if the option passes validation, otherwise a string failure message. */ public final function getProblemMessage($optionName, $candidate) { $optionDescriptorReferenceService = tubepress_impl_patterns_sl_ServiceLocator::getOptionDescriptorReference(); $messageService = tubepress_impl_patterns_sl_ServiceLocator::getMessageService(); $descriptor = $optionDescriptorReferenceService->findOneByName($optionName); if ($descriptor === null) { return sprintf('No option with name "%s".', $optionName); //>(translatable)< } if ($descriptor->hasValidValueRegex()) { if (preg_match_all($descriptor->getValidValueRegex(), (string) $candidate, $matches) >= 1 && $matches[0][0] === (string) $candidate) { return null; } return sprintf('Invalid value supplied for "%s".', $messageService->_($descriptor->getLabel())); //>(translatable)< } if ($descriptor->hasDiscreteAcceptableValues()) { $acceptableValues = $descriptor->getAcceptableValues(); if (tubepress_impl_util_LangUtils::isAssociativeArray($acceptableValues)) { $values = array_keys($descriptor->getAcceptableValues()); } else { $values = array_values($acceptableValues); } if (in_array($candidate, $values)) { return null; } return sprintf('"%s" must be one of "%s". You supplied "%s".', $messageService->_($descriptor->getLabel()), implode(', ', $values), $candidate); } if ($descriptor->isBoolean()) { if (is_bool($candidate)) { return null; } return sprintf('"%s" can only be "true" or "false". You supplied "%s".', $optionName, $candidate); //>(translatable)< } return null; }
public static function convertBooleans($map) { $optionDescriptorReference = tubepress_impl_patterns_sl_ServiceLocator::getOptionDescriptorReference(); foreach ($map as $key => $value) { $optionDescriptor = $optionDescriptorReference->findOneByName($key); if ($optionDescriptor === null || !$optionDescriptor->isBoolean()) { continue; } $map[$key] = $value ? true : false; } return $map; }
/** * @param string[] $names * * @return tubepress_spi_options_OptionDescriptor[] */ private function _lookupOptionDescriptors(array $names) { /** * @var $metas tubepress_spi_options_OptionDescriptor[] */ $toReturn = array(); $reference = tubepress_impl_patterns_sl_ServiceLocator::getOptionDescriptorReference(); foreach ($names as $metaName) { $toReturn[$metaName] = $reference->findOneByName($metaName); } return $toReturn; }