Beispiel #1
0
 public function outputModel($oModel)
 {
     $flags = 0;
     //JSON_FORCE_OBJECT;
     if (phpversion() > '5.3.3') {
         $flags |= JSON_NUMERIC_CHECK;
     }
     if (phpversion() > '5.4.0') {
         $flags |= JSON_UNESCAPED_UNICODE;
         if (\vsc\isDebug()) {
             $flags |= JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES;
         }
     }
     if (ModelA::isValid($oModel)) {
         /* @var ModelA $oModel */
         $sOutput = json_encode($oModel->toArray(), $flags);
     } else {
         $sOutput = json_encode($oModel, $flags);
     }
     if (!json_last_error()) {
         return $sOutput;
     } else {
         throw new ExceptionView(json_last_error_msg());
     }
 }
Beispiel #2
0
 public function testIsValid()
 {
     $TestVar = new Base();
     $this->assertTrue(Base::isValid($TestVar));
     $this->assertTrue(Object::isValid($TestVar));
     $TestVar = new RESTProcessorA_underTest_isValid();
     $this->assertTrue(RESTProcessorA_underTest_isValid::isValid($TestVar));
     $this->assertTrue(RESTProcessorA::isValid($TestVar));
     $this->assertTrue(ProcessorA::isValid($TestVar));
     $this->assertTrue(Object::isValid($TestVar));
     $TestVar = new ProcessorA_underTest_isValid();
     $this->assertTrue(ProcessorA_underTest_isValid::isValid($TestVar));
     $this->assertTrue(ProcessorA::isValid($TestVar));
     $this->assertTrue(Object::isValid($TestVar));
     $TestVar = new RESTController();
     $this->assertTrue(RESTController::isValid($TestVar));
     $this->assertTrue(FrontControllerA::isValid($TestVar));
     $this->assertTrue(Object::isValid($TestVar));
     $TestVar = new FrontControllerA_underTest_isValid();
     $this->assertTrue(FrontControllerA_underTest_isValid::isValid($TestVar));
     $this->assertTrue(FrontControllerA::isValid($TestVar));
     $this->assertTrue(Object::isValid($TestVar));
     $TestVar = new ModelA_underTest_isValid();
     $this->assertTrue(ModelA_underTest_isValid::isValid($TestVar));
     $this->assertTrue(ModelA::isValid($TestVar));
 }
Beispiel #3
0
/* @var $this \vsc\presentation\views\JsonView  */
$aArray = $model->toArray();
$mEnd = end($aArray);
$sEndKey = key($aArray);
foreach ($aArray as $sName => $mValue) {
    if (is_scalar($mValue)) {
        StringUtils::_echo("\t", $GLOBALS['depth']);
        if (!is_int($sName)) {
            echo '"' . $sName . '": ';
        }
        echo '"' . StringUtils::stripEntities(str_replace('"', '\\"', $mValue)) . '"' . ($sName != $sEndKey ? ',' : '') . "\n";
        continue;
    } elseif (is_array($mValue)) {
        $mValue = new ArrayModel($mValue);
    }
    if (ModelA::isValid($mValue)) {
        $this->setModel($mValue);
        StringUtils::_echo("\t", $GLOBALS['depth']);
        echo '"' . $sName . '": ';
        echo '{' . "\n";
        $GLOBALS['depth']++;
        echo $this->fetch(__FILE__);
        StringUtils::_echo("\t", $GLOBALS['depth'] - 1);
        echo '}' . ($sName != $sEndKey && $mEnd != $mValue ? ',' : '') . "\n";
        $GLOBALS['depth']--;
        continue;
    }
    StringUtils::_echo("\t", $GLOBALS['depth']);
    echo '"' . $sName . '" : ';
    echo '"' . var_export($mValue, true) . '"' . ($sName != $sEndKey ? ',' : '') . "\n";
}
Beispiel #4
0
 /**
  * @throws ExceptionView
  * @returns ModelA
  */
 public function getModel()
 {
     if (ModelA::isValid($this->oModel)) {
         return $this->oModel;
     } else {
         return new Base();
     }
 }
Beispiel #5
0
 /**
  * recursively transform all properties into arrays
  */
 public function toArray()
 {
     $aRet = array();
     $aProperties = $this->getProperties();
     foreach ($aProperties as $sName => $oProperty) {
         if (ModelA::isValid($oProperty)) {
             /* @var ModelA $oProperty */
             $aRet[$sName] = $oProperty->toArray();
         } elseif (is_array($oProperty) || is_scalar($oProperty)) {
             $aRet[$sName] = $oProperty;
         } elseif (is_null($oProperty)) {
             $aRet[$sName] = $oProperty;
         } else {
             $aRet[$sName] = var_export($oProperty, true);
         }
     }
     return $aRet;
 }
Beispiel #6
0
 /**
  * @param HttpRequestA $oRequest
  * @param ProcessorA $oProcessor
  * @throws ExceptionPath
  * @throws ExceptionResponse
  * @throws ExceptionView
  * @returns HttpResponseA
  */
 public function getResponse(HttpRequestA $oRequest, $oProcessor = null)
 {
     $oResponse = vsc::getEnv()->getHttpResponse();
     $oModel = null;
     /* @var ControllerMap $oMyMap */
     $oMyMap = $this->getMap();
     if (ProcessorA::isValid($oProcessor)) {
         try {
             $oProcessor->init();
             $oModel = $oProcessor->handleRequest($oRequest);
         } catch (ExceptionResponseRedirect $e) {
             $oResponse->setStatus($e->getRedirectCode());
             $oResponse->setLocation($e->getLocation());
             return $oResponse;
         } catch (\Exception $e) {
             // we had error in the controller
             // @todo make more error processors
             return $this->getErrorResponse($e, $oRequest);
         }
     }
     if ($oResponse->getStatus() == 0) {
         $oResponse->setStatus(HttpResponseType::OK);
     }
     // we didn't set any special view
     // this means that the developer needs to provide his own views
     $oView = $this->getView();
     $oMap = null;
     if (ProcessorA::isValid($oProcessor)) {
         /* @var ProcessorMap $oMap */
         $oMap = $oProcessor->getMap();
         if (MappingA::isValid($oMap)) {
             if (MappingA::isValid($oMyMap)) {
                 $oMap->merge($oMyMap);
             }
             $oProcessorResponse = $oMap->getResponse();
             if (HttpResponseA::isValid($oProcessorResponse)) {
                 $oResponse = $oProcessorResponse;
             }
         }
         // setting the processor map
         $oView->setMap($oMap);
     }
     try {
         if ((ProcessorMap::isValid($oMap) || ClassMap::isValid($oMap)) && !$oMap->isStatic() && !$oMyMap->isStatic() && (ControllerMap::isValid($oMyMap) || ClassMap::isValid($oMyMap))) {
             $oView->setMainTemplate($oMyMap->getMainTemplatePath() . $oView->getViewFolder() . DIRECTORY_SEPARATOR . $oMyMap->getMainTemplate());
         }
     } catch (ExceptionPath $e) {
         // fallback to html5
         // @todo verify main template path and main template exist
         $oView->setMainTemplate($oMyMap->getMainTemplatePath() . DIRECTORY_SEPARATOR . 'html5' . DIRECTORY_SEPARATOR . $oMyMap->getMainTemplate());
     }
     if (!ModelA::isValid($oModel)) {
         $oModel = new EmptyModel();
         if (!ProcessorMap::isValid($oMap) || $oMap->getTitle() == '') {
             $oModel->setPageTitle('Warning');
         }
         $oModel->setPageContent('Warning: the processor didn\'t return a valid model. This is probably an error');
     }
     $oView->setModel($oModel);
     $oResponse->setView($oView);
     if (MappingA::isValid($oMap)) {
         $aHeaders = $oMap->getHeaders();
         if (count($aHeaders) > 0) {
             foreach ($aHeaders as $sName => $sHeader) {
                 $oResponse->addHeader($sName, $sHeader);
             }
         }
         $iProcessorSetStatus = $oMap->getResponseStatus();
         if (HttpResponseType::isValidStatus($iProcessorSetStatus)) {
             $oResponse->setStatus($iProcessorSetStatus);
         }
     }
     return $oResponse;
 }