/** * @param array $post * @param array $result * @return array */ public function saveFiles($post, $result) { if (Utils::areThereDuplicatePropertyNames($post)) { return Utils::returnResultWithErrorMessage($result, "duplicatePropertyNames"); } $this->getEntityStringCreator()->consumePostData($post); $namespace = $this->getEntityStringCreator()->getClassDataStringCreator()->getNamespace(); $namespace = Utils::replaceSlashesWithSystemSeparator($namespace); $classname = $this->getEntityStringCreator()->getClassDataStringCreator()->getClassname(); $targetPhpFilePath = $this->buildTargetFilePath($namespace, $classname, $this->getSaveDir(), false); $jsonTargetFilePath = $this->buildTargetFilePath($namespace, $classname, $this->getJsonSaveDir(), true); if (!$targetPhpFilePath || !$jsonTargetFilePath) { return Utils::returnResultWithErrorMessage($result, "didntcreatefolder"); } $outputString = $this->getEntityStringCreator()->makeFinalEntityString(); $jsonRepresentation = $this->getEntityStringCreator()->createJsonRepresentation(); if (!$outputString || !$jsonRepresentation) { return Utils::returnResultWithErrorMessage($result, "stringnotcreated"); } $finalPhpString = "<?php\n\n" . $outputString; file_put_contents($targetPhpFilePath, $finalPhpString); file_put_contents($jsonTargetFilePath, $jsonRepresentation); if ($this->checkIfContentsMatchTheInput($targetPhpFilePath, $finalPhpString, $jsonTargetFilePath, $jsonRepresentation)) { $result["status"] = "success"; $result["entityString"] = $finalPhpString; $result["jsonEntityRepresentation"] = $jsonRepresentation; } else { $result["message"] = "notsaved"; } return $result; }
/** * @param array $post * @param array $result * @return array */ public function getString($post, $result) { if (Utils::areThereDuplicatePropertyNames($post)) { return Utils::returnResultWithErrorMessage($result, "duplicatePropertyNames"); } $this->getEntityStringCreator()->consumePostData($post); if (!@($finalString = $this->getEntityStringCreator()->makeFinalEntityString())) { return Utils::returnResultWithErrorMessage($result, "stringnotcreated"); } $result["entityString"] = "<pre>{$finalString}</pre>"; $result["status"] = "success"; return $result; }
public function testAreThereDuplicatePropertyNamesReturnsFalse() { $testPostData = ['entityProperties' => [0 => ['propertyName' => 'test'], 1 => ['propertyName' => 'test2']]]; $result = Utils::areThereDuplicatePropertyNames($testPostData); $this->assertEquals(false, $result); }