Ejemplo n.º 1
0
 /**
  * {@inheritDoc}
  */
 public function create($data)
 {
     if (!is_array($data) && !$data instanceof \Traversable) {
         throw new InvalidValueException('Invalid data to create a map. Only traversable structures allowed');
     }
     $map = new StrictMap($this->keyType->getTypeCheck(), $this->valueType->getTypeCheck());
     foreach ($data as $entry) {
         if (is_object($entry)) {
             $entry = (array) $entry;
         } else {
             if (!is_array($entry)) {
                 throw new InvalidValueException('Invalid data to create a map. Each entry must be an array or object');
             }
         }
         if (!array_key_exists('key', $entry)) {
             throw new InvalidValueException('Invalid data to create a map. One of entry does not contain "key" key');
         }
         if (!array_key_exists('value', $entry)) {
             throw new InvalidValueException('Invalid data to create a map. One of entry does not contain "value" key');
         }
         try {
             $key = $this->keyType->create($entry['key']);
         } catch (\Exception $e) {
             throw new InvalidValueException('One of entry of map contains invalid "key"', 0, $e);
         }
         try {
             $value = $this->valueType->create($entry['value']);
         } catch (\Exception $e) {
             throw new InvalidValueException('One of entry of map contains invalid "value"', 0, $e);
         }
         $map->add($key, $value);
     }
     return $map;
 }
Ejemplo n.º 2
0
 public function testGetValueType()
 {
     $expected = TypeCheck::strings();
     $this->assertEquals($expected, $this->object->getValueType());
     $this->assertEquals($expected, $this->object->getValueTypeCheck());
 }