/**
  * @param string $modelClassName
  * @param array $properties
  * @param string $applicationName
  * @param array $foundRelations
  * @return string
  */
 public function getModelDeclaration($modelClassName, array $properties, $applicationName, array &$foundRelations)
 {
     $globalPropertyIgnoreConfiguration = $this->configurationManager->getConfiguration('Settings', 'Radmiraal.Emberjs.properties._exclude');
     $modelGenerationPropertyIgnoreConfiguration = $this->configurationManager->getConfiguration('Settings', 'Radmiraal.Emberjs.applications.' . $applicationName . '.model.ignoredProperties');
     $ignoredProperties = array_merge($globalPropertyIgnoreConfiguration ?: array(), $modelGenerationPropertyIgnoreConfiguration ?: array());
     $typeMapping = $this->configurationManager->getConfiguration('Settings', 'Radmiraal.Emberjs.applications.' . $applicationName . '.model.typeMapping');
     $propertyDefinition = array();
     foreach ($properties as $property => $propertyConfiguration) {
         if ($property === 'id' || in_array($property, $ignoredProperties) || $this->reflectionService->isPropertyAnnotatedWith($modelClassName, $property, 'TYPO3\\Flow\\Annotations\\Transient')) {
             continue;
         }
         $type = $propertyConfiguration['type'];
         if (isset($typeMapping[$type])) {
             $type = $typeMapping[$type];
         }
         if (strpos($propertyConfiguration['type'], '\\')) {
             if (!in_array($propertyConfiguration['type'], $foundRelations)) {
                 $foundRelations[] = $propertyConfiguration['type'];
             }
             $type = 'App.' . EmberDataUtility::uncamelizeClassName($type);
         }
         $propertyDefinition[] = sprintf('%s%s: DS.attr("%s")', chr(10) . "\t", $property, !empty($type) ? $type : 'string');
     }
     $modelName = EmberDataUtility::uncamelizeClassName($modelClassName);
     return sprintf('App.%s = DS.Model.extend(App.ModelMixin, {%s%s});', $modelName, implode(',', $propertyDefinition), chr(10)) . chr(10);
 }
 /**
  * Adds the given new task object to the task repository
  *
  * @param object $model A new task to add
  * @return void
  */
 public function createAction($model)
 {
     $this->repository->add($model);
     $this->persistenceManager->persistAll();
     $this->response->setStatus(201);
     $this->view->assign('content', array(EmberDataUtility::uncamelizeClassName($this->modelName) => $model));
 }
 protected function getPrefixedResourceTemplateName($templateName, $templateNamePrefix = NULL)
 {
     $templateName = preg_replace_callback('/\\/(.*)/', function ($matches) {
         return strtolower($matches[0]);
     }, $templateName);
     if ($templateNamePrefix !== NULL) {
         $templateName = $templateNamePrefix . $templateName;
     }
     return \Radmiraal\Emberjs\Utility\EmberDataUtility::uncamelize($templateName);
 }
 /**
  * @param string $value
  * @return boolean
  */
 protected function matchValue($value)
 {
     if ($value === NULL || $value === '') {
         return FALSE;
     }
     $className = EmberDataUtility::camelizeClassName($value);
     if ($this->objectManager->isRegistered($className)) {
         $this->setName('modelName');
         $this->value = $className;
         return TRUE;
     }
     return FALSE;
 }
 /**
  * @return void
  */
 protected function initializeAction()
 {
     if (!$this->request->hasArgument('modelName')) {
         $this->throwStatus(500, NULL, 'No modelName configuration found in request.');
     }
     $this->receivedData = json_decode($this->request->getHttpRequest()->getContent());
     $arguments = $this->request->getArguments();
     $this->modelName = $arguments['modelName'];
     $repositoryName = str_replace(array('\\Model\\'), array('\\Repository\\'), $this->modelName) . 'Repository';
     if ($this->objectManager->isRegistered($repositoryName)) {
         $this->repository = $this->objectManager->get($repositoryName);
     } else {
         if (!$this->request->getHttpRequest()->getMethod() === 'GET' || !$this->request->hasArgument('model')) {
             $this->throwStatus(500, NULL, 'No repository found for model ' . $this->modelName . '.');
         }
     }
     $lowerUnderScoredModelName = EmberDataUtility::uncamelizeClassName($this->modelName);
     if (isset($this->receivedData->{$lowerUnderScoredModelName})) {
         $this->arguments->getArgument('model')->setDataType($this->modelName);
         $arguments['model'] = (array) $this->receivedData->{$lowerUnderScoredModelName};
         if (isset($arguments['id'])) {
             $arguments['model']['__identity'] = $arguments['id'];
             unset($arguments['id']);
         }
         // HACK: for some reason ember sends <model:ember<uid>:identifier> to the server
         $arguments['model'] = array_map(function ($value) {
             if (substr($value, 0, 1) === '<' && substr($value, -1) === '>') {
                 return array('__identity' => substr($value, strrpos($value, ':') + 1, 36));
             }
             if ($value !== NULL) {
                 return $value;
             }
         }, $arguments['model']);
         // HACK: we should find another way to skip empty values
         foreach ($arguments['model'] as $key => $value) {
             if ($arguments['model'][$key] === NULL) {
                 unset($arguments['model'][$key]);
             }
         }
     }
     if ($this->request->hasArgument('model')) {
         $arguments['model'] = array('__identity' => $arguments['model']);
         $this->arguments->getArgument('model')->setDataType($this->modelName);
     }
     unset($arguments['modelName']);
     $this->request->setArguments($arguments);
 }
 /**
  * @dataProvider underscoredNames
  * @test
  *
  * @param string $underscoredName
  * @param string $expectedOutput
  * @return void
  */
 public function camelizeReplacesUnderscoreAndFollowingCharacterToAnUppercaseCharacter($underscoredName, $expectedOutput)
 {
     $this->assertEquals($expectedOutput, EmberDataUtility::camelize($underscoredName));
 }
 /**
  * Traverses the given object structure in order to transform it into an
  * array structure.
  *
  * @param object $object Object to traverse
  * @param array $configuration Configuration for transforming the given object
  * @param boolean $onlyIdentifier
  * @return array Object structure as an array
  * @todo implement sideloading
  */
 protected function transformObject($object, array $configuration, $onlyIdentifier = TRUE)
 {
     if ($object instanceof \DateTime) {
         return $object->format('Y-m-d\\TH:i:s');
     } elseif ($onlyIdentifier === TRUE) {
         $objectIdentifier = $this->persistenceManager->getIdentifierByObject($object);
         if ($objectIdentifier === NULL && method_exists($object, 'getId')) {
             $objectIdentifier = $object->getId();
         }
         return $objectIdentifier;
     } else {
         $transformedObject = array();
         if ($this->excludeIdentifier === FALSE) {
             $objectIdentifier = $this->persistenceManager->getIdentifierByObject($object);
             $transformedObject['id'] = $objectIdentifier;
         }
         $propertyNames = ObjectAccess::getGettablePropertyNames($object);
         foreach ($propertyNames as $propertyName) {
             if (substr($propertyName, 0, 1) !== '_') {
                 if (isset($configuration['_only']) && is_array($configuration['_only']) && !in_array($propertyName, $configuration['_only'])) {
                     continue;
                 }
                 if ($this->propertyIsExcluded($propertyName, $configuration)) {
                     continue;
                 }
                 $propertyValue = ObjectAccess::getProperty($object, $propertyName);
                 $objectName = $this->objectManager->getObjectNameByClassName($this->reflectionService->getClassNameByObject($object));
                 if (!$this->reflectionService->isPropertyAnnotatedWith($objectName, $propertyName, 'TYPO3\\Flow\\Annotations\\Transient')) {
                     if (is_array($propertyValue) || $propertyValue instanceof \ArrayAccess) {
                         $transformedObject[EmberDataUtility::uncamelize($propertyName)] = $this->transformValue($propertyValue, array());
                     } elseif ($propertyValue instanceof \DateTime) {
                         $transformedObject[EmberDataUtility::uncamelize($propertyName)] = $propertyValue->format('Y-m-d\\TH:i:s');
                     } elseif (is_object($propertyValue)) {
                         $objectName = $this->objectManager->getObjectNameByClassName($this->reflectionService->getClassNameByObject($object));
                         if ($this->objectManager->getScope($objectName) !== \TYPO3\Flow\Object\Configuration\Configuration::SCOPE_SINGLETON) {
                             $transformedObject[EmberDataUtility::uncamelize($propertyName) . '_id'] = $this->persistenceManager->getIdentifierByObject($propertyValue);
                             if (isset($configuration['sideloadedAssociations']) && in_array($propertyName, $configuration['sideloadedAssociations'])) {
                                 // sideload this propertyvalue
                             }
                         }
                     } else {
                         $transformedObject[EmberDataUtility::uncamelize($propertyName)] = $propertyValue;
                     }
                 }
             }
         }
         return $transformedObject;
     }
 }
 /**
  * Registers a value of given objectType to be sideLoaded
  *
  * @param string $objectType
  * @param mixed $value
  * @return void
  */
 protected function sideLoad($objectType, $value)
 {
     $sideloadKey = EmberDataUtility::uncamelizeClassName($objectType);
     if (!isset($this->objectsToSideload[$sideloadKey])) {
         $this->objectsToSideload[$sideloadKey] = array();
     }
     if (!is_array($value)) {
         $value = array($value);
     }
     foreach ($value as $uuid) {
         if (!in_array($uuid, $this->objectsToSideload[$sideloadKey])) {
             $this->objectsToSideload[$sideloadKey][] = $uuid;
         }
     }
 }