/**
  * @covers Contentful\Delivery\ContentType::__construct
  * @covers Contentful\Delivery\ContentType::getId
  * @covers Contentful\Delivery\ContentType::getName
  * @covers Contentful\Delivery\ContentType::getDescription
  * @covers Contentful\Delivery\ContentType::getSpace
  * @covers Contentful\Delivery\ContentType::getDisplayField
  * @covers Contentful\Delivery\ContentType::getCreatedAt
  * @covers Contentful\Delivery\ContentType::getUpdatedAt
  * @covers Contentful\Delivery\ContentType::getRevision
  * @covers Contentful\Delivery\ContentType::getField
  * @covers Contentful\Delivery\ContentType::getFields
  *
  * @uses Contentful\Delivery\SystemProperties::__construct
  * @uses Contentful\Delivery\SystemProperties::getId
  * @uses Contentful\Delivery\SystemProperties::getSpace
  * @uses Contentful\Delivery\SystemProperties::getCreatedAt
  * @uses Contentful\Delivery\SystemProperties::getUpdatedAt
  * @uses Contentful\Delivery\SystemProperties::getRevision
  *
  * @uses Contentful\Delivery\ContentTypeField::__construct
  * @uses Contentful\Delivery\ContentTypeField::getId
  * @uses Contentful\Delivery\ContentTypeField::getName
  */
 public function testGetter()
 {
     $space = $this->getMockBuilder(Space::class)->disableOriginalConstructor()->getMock();
     $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->assertEquals('human', $contentType->getId());
     $this->assertEquals('Human', $contentType->getName());
     $this->assertEquals('Also called h**o sapien', $contentType->getDescription());
     $this->assertEquals($space, $contentType->getSpace());
     $this->assertEquals($displayField, $contentType->getDisplayField());
     $this->assertEquals(new \DateTimeImmutable('2013-06-27T22:46:14.133Z'), $contentType->getCreatedAt());
     $this->assertEquals(new \DateTimeImmutable('2013-09-02T15:10:26.818Z'), $contentType->getUpdatedAt());
     $this->assertEquals(3, $contentType->getRevision());
     $this->assertEquals('Likes', $contentType->getField('likes')->getName());
     $fields = $contentType->getFields();
     $this->assertInternalType('array', $fields);
     $this->assertCount(3, $fields);
     $this->assertArrayHasKey('name', $fields);
     $this->assertEquals($displayField, $fields['name']);
 }
 /**
  * Generate the jsonSerialize function.
  *
  * @param  ContentType $contentType
  *
  * @return string
  */
 protected function generateJsonSerialize(ContentType $contentType)
 {
     $defaultLocale = $contentType->getSpace()->getDefaultLocale()->getCode();
     $fields = array_filter($contentType->getFields(), function ($field) {
         return !$field->isDisabled();
     });
     $fieldCount = count($fields);
     $i = 0;
     $lines = [];
     $lines[] = '<spaces>/**';
     $lines[] = '<spaces> * @return object';
     $lines[] = '<spaces> */';
     $lines[] = '<spaces>public function jsonSerialize()';
     $lines[] = '<spaces>{';
     $lines[] = '<spaces><spaces>$fields = (object) [';
     foreach ($fields as $field) {
         $i++;
         // We'll handle localized links in a second loop as they need a bit more logic
         if ($field->isLocalized()) {
             if ($this->isSimpleType($field->getType()) || $field->getType() === 'Array' && $this->isSimpleType($field->getItemsType())) {
                 $lines[] = '<spaces><spaces><spaces>\'' . $field->getId() . '\' => $this->' . $this->getPropertyName($field) . ($i !== $fieldCount ? ',' : '');
             } else {
                 $lines[] = '<spaces><spaces><spaces>\'' . $field->getId() . '\' => new \\stdClass' . ($i !== $fieldCount ? ',' : '');
             }
             continue;
         }
         $lines[] = '<spaces><spaces><spaces>\'' . $field->getId() . '\' => [';
         if ($this->isSimpleType($field->getType()) || $field->getType() === 'Array' && $this->isSimpleType($field->getItemsType())) {
             $lines[] = '<spaces><spaces><spaces><spaces>\'' . $defaultLocale . '\' => $this->' . $this->getPropertyName($field);
         } elseif ($field->getType() === 'Link') {
             $lines[] = '<spaces><spaces><spaces><spaces>\'' . $defaultLocale . '\' => (object) [';
             $lines[] = '<spaces><spaces><spaces><spaces><spaces>\'sys\' => (object) [';
             $lines[] = '<spaces><spaces><spaces><spaces><spaces><spaces>\'type\' => \'Link\',';
             $lines[] = '<spaces><spaces><spaces><spaces><spaces><spaces>\'linkType\' => \'' . $field->getLinkType() . '\',';
             $lines[] = '<spaces><spaces><spaces><spaces><spaces><spaces>\'id\' => $this->' . $this->getPropertyName($field) . '->getId()';
             $lines[] = '<spaces><spaces><spaces><spaces><spaces>]';
             $lines[] = '<spaces><spaces><spaces><spaces>]';
         } elseif ($field->getType() === 'Array' && $field->getItemsType() === 'Link') {
             $lines[] = '<spaces><spaces><spaces><spaces>\'' . $defaultLocale . '\' => array_map(function ($value) {';
             $lines[] = '<spaces><spaces><spaces><spaces><spaces>return (object) [';
             $lines[] = '<spaces><spaces><spaces><spaces><spaces><spaces>\'sys\' => (object) [';
             $lines[] = '<spaces><spaces><spaces><spaces><spaces><spaces><spaces>\'type\' => \'Link\',';
             $lines[] = '<spaces><spaces><spaces><spaces><spaces><spaces><spaces>\'linkType\' => \'' . $field->getItemsLinkType() . '\',';
             $lines[] = '<spaces><spaces><spaces><spaces><spaces><spaces><spaces>\'id\' => $this->' . $this->getPropertyName($field) . '->getId()';
             $lines[] = '<spaces><spaces><spaces><spaces><spaces><spaces>]';
             $lines[] = '<spaces><spaces><spaces><spaces><spaces>];';
             $lines[] = '<spaces><spaces><spaces><spaces>}, $this->' . $this->getPropertyName($field) . ');';
         } else {
             throw new \RuntimeException('Unexpected type "' . $field->getType() . '" while generating classes"');
         }
         $lines[] = '<spaces><spaces><spaces>]' . ($i !== $fieldCount ? ',' : '');
     }
     $lines[] = '<spaces><spaces>];';
     foreach ($fields as $field) {
         if (!$field->isLocalized() || ($field->getType() !== 'Link' || !($field->getType() === 'Array' && $field->getItemsType() === 'Link'))) {
             continue;
         }
         $lines[] = '<spaces><spaces>foreach ($this->' . $this->getPropertyName($field) . ' as $locale => $data) {';
         if ($field->getType() === 'Link') {
             $lines[] = '<spaces><spaces><spaces>$fields->' . $field->getId() . '->$locale = (object) [';
             $lines[] = '<spaces><spaces><spaces><spaces>\'sys\' => (object) [';
             $lines[] = '<spaces><spaces><spaces><spaces><spaces>\'type\' => \'Link\',';
             $lines[] = '<spaces><spaces><spaces><spaces><spaces>\'linkType\' => \'' . $field->getLinkType() . '\',';
             $lines[] = '<spaces><spaces><spaces><spaces><spaces>\'id\' => $data->getId()';
             $lines[] = '<spaces><spaces><spaces><spaces>]';
             $lines[] = '<spaces><spaces><spaces>]';
         } elseif ($field->getType() === 'Array') {
             $lines[] = '<spaces><spaces><spaces>$fields->' . $field->getId() . '->$locale = array_map(function ($value) {';
             $lines[] = '<spaces><spaces><spaces><spaces>return (object) [';
             $lines[] = '<spaces><spaces><spaces><spaces><spaces>\'sys\' => (object) [';
             $lines[] = '<spaces><spaces><spaces><spaces><spaces><spaces>\'type\' => \'Link\',';
             $lines[] = '<spaces><spaces><spaces><spaces><spaces><spaces>\'linkType\' => \'' . $field->getItemsLinkType() . '\',';
             $lines[] = '<spaces><spaces><spaces><spaces><spaces><spaces>\'id\' => $this->' . $this->getPropertyName($field) . '->getId()';
             $lines[] = '<spaces><spaces><spaces><spaces><spaces>]';
             $lines[] = '<spaces><spaces><spaces><spaces>];';
             $lines[] = '<spaces><spaces><spaces>}, $data);';
         }
         $lines[] = '<spaces><spaces>}';
     }
     $lines[] = '';
     $lines[] = '<spaces><spaces>return (object) [';
     $lines[] = '<spaces><spaces><spaces>\'sys\' => $this->sys,';
     $lines[] = '<spaces><spaces><spaces>\'fields\' => $fields';
     $lines[] = '<spaces><spaces>];';
     $lines[] = '<spaces>}';
     return implode("\n", $lines);
 }