/** * @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.'); } }