Exemple #1
0
 /**
  * @param EnumId|string $id
  * @param string        $type
  * @param array         $values
  * @param LanguageBag   $languages
  * @param bool          $deprecated
  */
 public function __construct($id, $type, array $values, LanguageBag $languages = null, $deprecated = false)
 {
     $this->id = $id instanceof EnumId ? $id : EnumId::fromString($id);
     $this->type = $type;
     $this->values = $values;
     $this->languages = $languages;
     $this->deprecated = $deprecated;
 }
Exemple #2
0
 /**
  * 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);
 }