/** * Builds an Enum instance from a given set of data. * * @param array $data * * @return EnumDescriptor|null * * @throws \InvalidArgumentException */ private function parse(array $data) { // generate id $enumId = EnumId::fromString(sprintf('%s:%s', $data['namespace'], $data['name'])); // force default type to be "string" if (!isset($data['type'])) { $data['type'] = 'string'; } $values = []; $keys = $this->fixArray($data['option'], 'key'); foreach ($keys as $key) { $values[strtoupper($key['key'])] = $data['type'] == 'int' ? intval($key['value']) : (string) $key['value']; } if (count($values) === 0) { return; } if (array_search('unknown', array_map('strtolower', array_keys($values))) === false) { throw new \InvalidArgumentException(sprintf('Enum "%s" require an "UNKNOWN" key that will be used as default value.', $enumId->toString())); } // add enums language options $languages = $this->getLanguageOptions($data); $isDeprecated = isset($data['deprecated']) && $data['deprecated']; return new EnumDescriptor($enumId, $data['type'], $values, $languages, $isDeprecated); }
/** * @return string */ public function toString() { return $this->id->toString(); }