/**
  * Visit struct returned by controllers.
  *
  * @param \eZ\Publish\Core\REST\Common\Output\Visitor $visitor
  * @param \eZ\Publish\Core\REST\Common\Output\Generator $generator
  * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup $data
  */
 public function visit(Visitor $visitor, Generator $generator, $data)
 {
     $generator->startObjectElement('ObjectStateGroup');
     $visitor->setHeader('Content-Type', $generator->getMediaType('ObjectStateGroup'));
     $visitor->setHeader('Accept-Patch', $generator->getMediaType('ObjectStateGroupUpdate'));
     $generator->startAttribute('href', $this->router->generate('ezpublish_rest_loadObjectStateGroup', array('objectStateGroupId' => $data->id)));
     $generator->endAttribute('href');
     $generator->startValueElement('id', $data->id);
     $generator->endValueElement('id');
     $generator->startValueElement('identifier', $data->identifier);
     $generator->endValueElement('identifier');
     $generator->startValueElement('defaultLanguageCode', $data->defaultLanguageCode);
     $generator->endValueElement('defaultLanguageCode');
     $generator->startValueElement('languageCodes', implode(',', $data->languageCodes));
     $generator->endValueElement('languageCodes');
     $generator->startObjectElement('ObjectStates', 'ObjectStateList');
     $generator->startAttribute('href', $this->router->generate('ezpublish_rest_loadObjectStates', array('objectStateGroupId' => $data->id)));
     $generator->endAttribute('href');
     $generator->endObjectElement('ObjectStates');
     $this->visitNamesList($generator, $data->getNames());
     $this->visitDescriptionsList($generator, $data->getDescriptions());
     $generator->endObjectElement('ObjectStateGroup');
 }
 /**
  * Validates input for updating object state groups and builds the InputStruct object.
  *
  * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup $objectStateGroup
  * @param string $identifier
  * @param string $defaultLanguageCode
  * @param string[] $names
  * @param string[] $descriptions
  *
  * @return \eZ\Publish\SPI\Persistence\Content\ObjectState\InputStruct
  */
 protected function buildObjectStateGroupUpdateInputStruct(APIObjectStateGroup $objectStateGroup, $identifier, $defaultLanguageCode, $names, $descriptions)
 {
     $inputStruct = new InputStruct();
     if ($identifier !== null && (!is_string($identifier) || empty($identifier))) {
         throw new InvalidArgumentValue('identifier', $identifier);
     }
     $inputStruct->identifier = $identifier !== null ? $identifier : $objectStateGroup->identifier;
     if ($defaultLanguageCode !== null && (!is_string($defaultLanguageCode) || empty($defaultLanguageCode))) {
         throw new InvalidArgumentValue('defaultLanguageCode', $defaultLanguageCode);
     }
     $inputStruct->defaultLanguage = $defaultLanguageCode !== null ? $defaultLanguageCode : $objectStateGroup->defaultLanguageCode;
     if ($names !== null && (!is_array($names) || empty($names))) {
         throw new InvalidArgumentValue('names', $names);
     }
     $inputStruct->name = $names !== null ? $names : $objectStateGroup->getNames();
     if (!isset($inputStruct->name[$inputStruct->defaultLanguage])) {
         throw new InvalidArgumentValue('names', $inputStruct->name);
     }
     foreach ($inputStruct->name as $languageCode => $name) {
         try {
             $this->repository->getContentLanguageService()->loadLanguage($languageCode);
         } catch (NotFoundException $e) {
             throw new InvalidArgumentValue('names', $inputStruct->name);
         }
         if (!is_string($name) || empty($name)) {
             throw new InvalidArgumentValue('names', $inputStruct->name);
         }
     }
     if ($descriptions !== null && !is_array($descriptions)) {
         throw new InvalidArgumentValue('descriptions', $descriptions);
     }
     $descriptions = $descriptions !== null ? $descriptions : $objectStateGroup->getDescriptions();
     $descriptions = $descriptions !== null ? $descriptions : array();
     $inputStruct->description = array();
     foreach ($inputStruct->name as $languageCode => $name) {
         if (isset($descriptions[$languageCode]) && !empty($descriptions[$languageCode])) {
             $inputStruct->description[$languageCode] = $descriptions[$languageCode];
         } else {
             $inputStruct->description[$languageCode] = '';
         }
     }
     return $inputStruct;
 }
 /**
  * Tests that the resulting ObjectStateGroup contains descriptions
  *
  * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup $result
  *
  * @depends testParse
  */
 public function testResultContainsDescriptions($result)
 {
     $this->assertEquals(array('eng-GB' => 'Test group description EN', 'eng-US' => 'Test group description EN US'), $result->getDescriptions());
 }
 /**
  * updates an object state group
  *
  * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to update an object state group
  * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the object state group with provided identifier already exists
  *
  * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup $objectStateGroup
  * @param \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroupUpdateStruct $objectStateGroupUpdateStruct
  *
  * @return \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup
  */
 public function updateObjectStateGroup(ObjectStateGroup $objectStateGroup, ObjectStateGroupUpdateStruct $objectStateGroupUpdateStruct)
 {
     if (false === $this->repository->hasAccess('class', '*')) {
         throw new Exceptions\UnauthorizedExceptionStub('What error code should be used?');
     }
     if ($objectStateGroupUpdateStruct->identifier !== null) {
         foreach ($this->groups as $group) {
             if ($group->identifier == $objectStateGroupUpdateStruct->identifier && $group->id != $objectStateGroup->id) {
                 throw new Exceptions\InvalidArgumentExceptionStub('What error code should be used?');
             }
         }
     }
     $data = array('id' => $objectStateGroup->id, 'identifier' => $objectStateGroup->identifier, 'defaultLanguageCode' => $objectStateGroup->defaultLanguageCode, 'names' => $objectStateGroup->getNames(), 'descriptions' => $objectStateGroup->getDescriptions());
     foreach ($objectStateGroupUpdateStruct as $propertyName => $propertyValue) {
         if ($propertyValue !== null) {
             $data[$propertyName] = $propertyValue;
         }
     }
     $data['languageCodes'] = $this->determineLanguageCodes($data['names'], $data['descriptions']);
     $this->groups[$objectStateGroup->id] = new Values\ObjectState\ObjectStateGroupStub($data);
     return $this->groups[$objectStateGroup->id];
 }