/**
  * @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());
 }
 /**
  * Fields that reference links (or arrays of links) also get a method to get just the ID.
  *
  * @param  ContentTypeField $field
  *
  * @return string
  */
 protected function generateIdMethod(ContentTypeField $field)
 {
     $parameters = $field->isLocalized() ? '$locale = null' : '';
     $lines = ['<spaces>/**'];
     if ($field->isLocalized()) {
         $lines[] = '<spaces> * @param  Locale|string|null $locale';
         $lines[] = '<spaces> *';
     }
     if ($field->getType() === 'Link') {
         $lines[] = '<spaces> * @return string';
         $lines[] = '<spaces> */';
         $lines[] = '<spaces>public function get' . ucfirst($field->getId()) . 'Id(' . $parameters . ')';
         $lines[] = '<spaces>{';
         if ($field->isLocalized()) {
             $lines[] = '<spaces><spaces>$locale = $this->getLocaleFromInput($locale);';
             $lines[] = '';
             $lines[] = '<spaces><spaces>return $this->' . $this->getPropertyName($field) . '->$locale->getId();';
         } else {
             $lines[] = '<spaces><spaces>return $this->' . $this->getPropertyName($field) . '->getId();';
         }
     }
     if (false && $field->getType() === 'Array' && $field->getItemsType() === 'Link') {
         $lines[] = '<spaces> * @return string[]';
         $lines[] = '<spaces> */';
         $lines[] = '<spaces>public function get' . ucfirst($field->getId()) . 'Id(' . $parameters . ')';
         $lines[] = '<spaces>{';
         if ($field->isLocalized()) {
             $lines[] = '<spaces><spaces>$locale = $this->getLocaleFromInput($locale);';
             $lines[] = '';
             $lines[] = '<spaces><spaces>$result = $this->' . $this->getPropertyName($field) . '->$locale->getId();';
         } else {
             $lines[] = '<spaces><spaces>$result = $this->' . $this->getPropertyName($field) . '->getId();';
         }
         $lines[] = '<spaces><spaces>return array_map(function ($value) use ($client) {';
         $lines[] = '<spaces><spaces><spaces>return $value->getId();';
         $lines[] = '<spaces><spaces>}, $result);';
     }
     $lines[] = '<spaces>}';
     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.');
     }
 }
 /**
  * @covers Contentful\Delivery\ContentType::__construct
  * @covers Contentful\Delivery\ContentType::jsonSerialize
  *
  * @uses Contentful\Delivery\SystemProperties::__construct
  * @uses Contentful\Delivery\SystemProperties::getId
  * @uses Contentful\Delivery\SystemProperties::jsonSerialize
  * @uses Contentful\Delivery\SystemProperties::formatDateForJson
  *
  * @uses Contentful\Delivery\ContentTypeField::__construct
  * @uses Contentful\Delivery\ContentTypeField::getId
  * @uses Contentful\Delivery\ContentTypeField::jsonSerialize
  *
  *
  * For some reason phpunit claims this method is executed, no idea why.
  * @uses Contentful\Delivery\SystemProperties::getCreatedAt
  */
 public function testJsonSerialize()
 {
     $space = $this->getMockBuilder(Space::class)->disableOriginalConstructor()->getMock();
     $space->method('getId')->willReturn('cfexampleapi');
     $displayField = new ContentTypeField('name', 'Name', 'Text', null, null, null, true);
     $contentType = new ContentType('Human', 'Also called h**o sapien', [$displayField, new ContentTypeField('likes', 'Likes', 'Array', null, 'Symbol', null), new ContentTypeField('image', 'Image', 'Array', null, 'Link', 'Asset', false, false, true)], $displayField->getId(), new SystemProperties('human', 'ContentType', $space, null, 3, new \DateTimeImmutable('2013-06-27T22:46:14.133Z'), new \DateTimeImmutable('2013-09-02T15:10:26.818Z')));
     $this->assertJsonStringEqualsJsonString('{"name":"Human","description":"Also called h**o sapien","displayField":"name","sys":{"id":"human","type":"ContentType","space":{"sys":{"type":"Link","linkType":"Space","id":"cfexampleapi"}},"revision":3,"createdAt":"2013-06-27T22:46:14.133Z","updatedAt":"2013-09-02T15:10:26.818Z"},"fields":[{"name":"Name","id":"name","type":"Text","required":true,"localized":false},{"name":"Likes","id":"likes","type":"Array","required":false,"localized":false,"items":{"type":"Symbol"}},{"name":"Image","id":"image","type":"Array","required":false,"localized":false,"disabled":true,"items":{"type":"Link","linkType":"Asset"}}]}', json_encode($contentType));
 }