public function testCreateEnumValueWithoutId() { $result = $this->repo->createEnumValue('Test Value 1', 1, true); $this->assertInstanceOf(self::ENUM_VALUE_CLASS_NAME, $result); $this->assertEquals(ExtendHelper::buildEnumValueId('Test Value 1'), $result->getId()); $this->assertEquals('Test Value 1', $result->getName()); $this->assertEquals(1, $result->getPriority()); $this->assertTrue($result->isDefault()); }
/** * Creates an entity represents an enum value * * @param string $name The enum value name * @param int $priority An number used to sort enum values on UI. * Values with less priority is rendered at the top * @param boolean $default Determines if this value is selected by default for new records * @param string|null $id The enum value identifier. If not specified it is generated * automatically based on the given name. Usually it is the same as name, * but spaces are replaced with underscore and result is converted to lower case. * As the id length is limited to 32 characters, in case if the name is longer then * some hashing function is used to generate the id. * * @return AbstractEnumValue * * @throws \InvalidArgumentException */ public function createEnumValue($name, $priority, $default, $id = null) { if (empty($name)) { throw new \InvalidArgumentException('$name must not be empty.'); } if (empty($id)) { $id = ExtendHelper::buildEnumValueId($name); } elseif (strlen($id) > ExtendHelper::MAX_ENUM_VALUE_ID_LENGTH) { throw new \InvalidArgumentException(sprintf('$id length must be less or equal %d characters. id: %s.', ExtendHelper::MAX_ENUM_VALUE_ID_LENGTH, $id)); } $enumValueClassName = $this->getClassName(); return new $enumValueClassName($id, $name, $priority, $default); }
/** * {@inheritdoc} */ public function validate($entity, Constraint $constraint) { if ($entity instanceof EnumValueEntity) { $entity = $entity->toArray(); } if (!is_array($entity)) { throw new UnexpectedTypeException($entity, 'Oro\\Bundle\\EntityExtendBundle\\Model\\EnumValue|array'); } if (!empty($entity['id']) || empty($entity['label'])) { return; } $valueId = ExtendHelper::buildEnumValueId($entity['label'], false); if (empty($valueId)) { $this->context->addViolationAt('[label]', $constraint->message, ['{{ value }}' => $entity['label']]); } }
/** * POST_SET_DATA event handler * * @param FormEvent $event */ public function postSetData(FormEvent $event) { $form = $event->getForm(); $data = $event->getData(); $constraints = [new Assert\NotBlank(), new Assert\Length(['max' => 255])]; if (empty($data['id'])) { $callback = function ($value, ExecutionContextInterface $context) { if (!empty($value)) { $id = ExtendHelper::buildEnumValueId($value, false); if (empty($id)) { $context->addViolation(self::INVALID_NAME_MESSAGE, ['{{ value }}' => $value]); } } }; $constraints[] = new Assert\Callback([$callback]); } $form->add('label', 'text', ['required' => true, 'constraints' => $constraints]); }
/** * @param QueryBag $queries * @param array $optionSets */ protected function fillEnumValues(QueryBag $queries, array &$optionSets) { foreach ($optionSets as &$optionSet) { $query = sprintf('INSERT INTO %s (id, name, priority, is_default) ' . 'VALUES (:id, :name, :priority, :is_default)', $optionSet['enum_table_name']); foreach ($optionSet['values'] as &$option) { $option['enum_value_id'] = ExtendHelper::buildEnumValueId($option['label']); $params = ['id' => $option['enum_value_id'], 'name' => $option['label'], 'priority' => $option['priority'], 'is_default' => $option['is_default']]; $types = ['id' => 'string', 'name' => 'string', 'priority' => 'integer', 'is_default' => 'boolean']; $queries->addPostQuery(new ParametrizedSqlMigrationQuery($query, $params, $types)); } } }
/** * @param string $name * @param string[] $existingIds * * @return string */ protected function generateEnumValueId($name, array $existingIds) { $id = ExtendHelper::buildEnumValueId($name); if (in_array($id, $existingIds, true)) { $prefix = substr($id, 0, ExtendHelper::MAX_ENUM_VALUE_ID_LENGTH - 6) . '_'; $counter = 1; $id = $prefix . $counter; while (in_array($id, $existingIds, true)) { $counter++; $id = $prefix . $counter; } } return $id; }
/** * @dataProvider buildEnumValueIdForInvalidEnumValueNameProvider */ public function testBuildEnumValueIdForInvalidEnumValueNameIgnoreException($enumValueName) { $this->assertSame('', ExtendHelper::buildEnumValueId($enumValueName, false)); }