/**
  * @covers Contentful\Delivery\ContentTypeField::__construct
  * @covers Contentful\Delivery\ContentTypeField::getId
  * @covers Contentful\Delivery\ContentTypeField::getName
  * @covers Contentful\Delivery\ContentTypeField::getType
  * @covers Contentful\Delivery\ContentTypeField::getLinkType
  * @covers Contentful\Delivery\ContentTypeField::getItemsLinkType
  * @covers Contentful\Delivery\ContentTypeField::getItemsType
  * @covers Contentful\Delivery\ContentTypeField::isRequired
  * @covers Contentful\Delivery\ContentTypeField::isLocalized
  * @covers Contentful\Delivery\ContentTypeField::isDisabled
  */
 public function testGetter()
 {
     $field = new ContentTypeField('id', 'name', 'type', 'linkType', 'itemsType', 'itemsLinkType', true, false, false);
     $this->assertEquals('id', $field->getId());
     $this->assertEquals('name', $field->getName());
     $this->assertEquals('type', $field->getType());
     $this->assertEquals('linkType', $field->getLinkType());
     $this->assertEquals('itemsLinkType', $field->getItemsLinkType());
     $this->assertEquals('itemsType', $field->getItemsType());
     $this->assertEquals(true, $field->isRequired());
     $this->assertEquals(false, $field->isLocalized());
     $this->assertEquals(false, $field->isDisabled());
 }
 /**
  * Generate the getter method body for arrays.
  *
  * @param ContentTypeField $field
  *
  * @return string
  */
 protected function generateArrayMethodBody(ContentTypeField $field)
 {
     if ($field->getItemsType() !== 'Link') {
         return $this->generateSimpleMethodBody($field);
     }
     $lines = ['<spaces><spaces>$client = $this->client;'];
     if ($field->isLocalized()) {
         $lines[] = '<spaces><spaces>$locale = $this->getLocaleFromInput($locale);';
         $lines[] = '';
         $lines[] = '<spaces><spaces>$result = $this->' . $this->getPropertyName($field) . '->$locale;';
     } else {
         $lines[] = '<spaces><spaces>$result = $this->' . $this->getPropertyName($field) . ';';
     }
     $lines[] = '<spaces><spaces>return array_map(function ($value) use ($client) {';
     $lines[] = '<spaces><spaces><spaces>if ($value instanceof Link) {';
     $lines[] = '<spaces><spaces><spaces><spaces>return $client->resolveLink($value);';
     $lines[] = '<spaces><spaces><spaces>}';
     $lines[] = '';
     $lines[] = '<spaces><spaces><spaces>return $value;';
     $lines[] = '<spaces><spaces>}, $result);';
     return implode("\n", $lines);
 }
 /**
  * Transforms values from the original JSON representation to an appropriate PHP representation.
  *
  * @param  ContentTypeField|string $fieldConfig Must be a ContentTypeField if the type is Array
  * @param  mixed $value
  *
  * @return array|Asset|DynamicEntry|Link|Location|\DateTimeImmutable
  */
 private function formatValue($fieldConfig, $value)
 {
     if ($fieldConfig instanceof ContentTypeField) {
         $type = $fieldConfig->getType();
     } else {
         $type = $fieldConfig;
     }
     switch ($type) {
         case 'Symbol':
         case 'Text':
         case 'Integer':
         case 'Number':
         case 'Boolean':
         case 'Object':
             return $value;
         case 'Date':
             return new \DateTimeImmutable($value, new \DateTimeZone('UTC'));
         case 'Location':
             return new Location($value->lat, $value->lon);
         case 'Link':
             return $this->buildLink($value);
         case 'Array':
             return array_map(function ($value) use($fieldConfig) {
                 return $this->formatValue($fieldConfig->getItemsType(), $value);
             }, $value);
         default:
             throw new \InvalidArgumentException('Unexpected field type "' . $type . '" encounterted while trying to format value.');
     }
 }