/** * Gets the failure message of a name/value pair that has failed validation. * * @param string $optionName The option name * @param unknown_type $candidate The candidate option value * * @return unknown Null if the option passes validation, otherwise a string failure message. */ function getProblemMessage($optionName, $candidate) { $ioc = org_tubepress_impl_ioc_IocContainer::getInstance(); $odr = $ioc->get(org_tubepress_api_options_OptionDescriptorReference::_); $descriptor = $odr->findOneByName($optionName); if ($descriptor === null) { return sprintf('No option with name %s', $optionName); } if ($descriptor->hasValidValueRegex()) { if (preg_match_all($descriptor->getValidValueRegex(), (string) $candidate, $matches) >= 1 && $matches[0][0] === (string) $candidate) { return null; } return sprintf('"%s" must match the regular expression %s. You supplied "%s".', $optionName, $descriptor->getValidValueRegex(), $candidate); } if ($descriptor->hasDiscreteAcceptableValues()) { $acceptableValues = $descriptor->getAcceptableValues(); if (org_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".', $optionName, implode(', ', $values), $candidate); } if ($descriptor->isBoolean()) { if (is_bool($candidate)) { return null; } return sprintf('"%s" can only accept true/false values. You supplied "%s".', $optionName, $candidate); } return null; }
function testAssocArray() { $this->assertFalse(org_tubepress_impl_util_LangUtils::isAssociativeArray(array(1, 2))); $this->assertFalse(org_tubepress_impl_util_LangUtils::isAssociativeArray(array())); $this->assertFalse(org_tubepress_impl_util_LangUtils::isAssociativeArray(array('foo' => 'bar', 3))); $this->assertTrue(org_tubepress_impl_util_LangUtils::isAssociativeArray(array('foo' => 'bar', 'smack' => 'crack'))); }
protected function populateTemplate($template, $currentValue) { $values = array(); $map = $this->getOptionDescriptor()->getAcceptableValues(); if (!org_tubepress_impl_util_LangUtils::isAssociativeArray($map)) { throw new Exception(sprintf('"%s" has a non-associative array set for its value map', $this->getOptionDescriptor()->getName())); } foreach ($map as $key => $value) { $values[$key] = $this->getMessageService()->_($value); } $template->setVariable(self::TEMPLATE_VAR_ACCEPTABLE_VALUES, $values); }