Пример #1
0
 public function testEndsWith()
 {
     $this->assertEquals(StringUtils::endsWith('foobar', 'f'), false, 'Test 1');
     $this->assertEquals(StringUtils::endsWith('foobar', 'a'), false, 'Test 2');
     $this->assertEquals(StringUtils::endsWith('foobar', 'ar'), true, 'Test 3');
     $this->assertEquals(StringUtils::endsWith('foobar', 'foobar'), true, 'Test 4');
 }
Пример #2
0
 /**
  * @param string $selectorStr
  * @param bool $returnNull
  *
  * @return mixed
  *
  * @throws TemplateEngineException
  */
 protected function getSelectorValue($selectorStr, $returnNull = false)
 {
     $selParts = explode('.', $selectorStr);
     $firstPart = array_shift($selParts);
     $currentSel = $firstPart;
     if ($this->dataPool->offsetExists($firstPart) === false) {
         if ($returnNull === false) {
             throw new TemplateEngineException('The data with offset "' . $currentSel . '" does not exist for template file ' . $this->currentTemplateFile);
         }
         return null;
     }
     $varData = $this->dataPool->offsetGet($firstPart);
     foreach ($selParts as $part) {
         $nextSel = $currentSel . '.' . $part;
         // Try to find value in hashmap, thats faster then parse again
         /*if($this->dataTable->offsetExists($nextSel)) {
         				$varData = $this->dataTable->offsetGet($nextSel);
         
         				continue;
         			}*/
         if ($varData instanceof \ArrayObject === true) {
             /** @var \ArrayObject $varData */
             if ($varData->offsetExists($part) === false) {
                 throw new TemplateEngineException('Array key "' . $part . '" does not exist in ArrayObject "' . $currentSel . '"');
             }
             $varData = $varData->offsetGet($part);
         } elseif (is_object($varData) === true) {
             $args = array();
             if (($argPos = strpos($part, '(')) !== false) {
                 $argStr = substr($part, $argPos + 1, -1);
                 $part = substr($part, 0, $argPos);
                 foreach (preg_split('/,/x', $argStr) as $no => $arg) {
                     if (StringUtils::startsWith($argStr, '\'') === false || StringUtils::endsWith($argStr, '\'') === false) {
                         $args[$no] = $this->getSelectorValue($argStr, $returnNull);
                     } else {
                         $args[$no] = substr($arg, 1, -1);
                     }
                 }
             }
             if (property_exists($varData, $part) === true) {
                 $getProperty = new \ReflectionProperty($varData, $part);
                 if ($getProperty->isPublic() === true) {
                     $varData = $varData->{$part};
                 } else {
                     $getterMethodName = null;
                     foreach ($this->getterMethodPrefixes as $mp) {
                         $getterMethodName = $mp . ucfirst($part);
                         if (method_exists($varData, $getterMethodName) === true) {
                             break;
                         }
                         $getterMethodName = null;
                     }
                     if ($getterMethodName === null) {
                         throw new TemplateEngineException('Could not access protected/private property "' . $part . '". Please provide a getter method');
                     }
                     $varData = call_user_func(array($varData, $getterMethodName));
                 }
             } elseif (method_exists($varData, $part) === true) {
                 $varData = call_user_func_array(array($varData, $part), $args);
             } else {
                 throw new TemplateEngineException('Don\'t know how to handle selector part "' . $part . '"');
             }
         } elseif (is_array($varData) === true) {
             if (array_key_exists($part, $varData) === false) {
                 throw new TemplateEngineException('Array key "' . $part . '" does not exist in array "' . $currentSel . '"');
             }
             $varData = $varData[$part];
         } else {
             throw new TemplateEngineException('The data with offset "' . $currentSel . '" is not an object nor an array.');
         }
         $currentSel = $nextSel;
         $this->dataTable->offsetSet($currentSel, $varData);
     }
     return $varData;
 }
Пример #3
0
 public function replaceNode(TemplateEngine $tplEngine, ElementNode $tagNode)
 {
     $compareAttr = $tagNode->getAttribute('compare')->value;
     $operatorAttr = $tagNode->getAttribute('operator')->value;
     $againstAttr = $tagNode->getAttribute('against')->value;
     $condAttr = $tagNode->getAttribute('cond')->value;
     if ($condAttr === null) {
         // Check required attrs
         $tplEngine->checkRequiredAttrs($tagNode, array('compare', 'operator', 'against'));
         if (strlen($againstAttr) === 0) {
             $againstAttr = "''";
         } elseif (is_int($againstAttr) === true) {
             $againstAttr = intval($againstAttr);
         } elseif (is_float($againstAttr) === true) {
             $againstAttr = floatval($againstAttr);
         } elseif (is_string($againstAttr) === true) {
             if (strtolower($againstAttr) === 'null') {
                 //$againstAttr = 'null';
             } elseif (strtolower($againstAttr) === 'true' || strtolower($againstAttr) === 'false') {
                 //$againstAttr = ($againstAttr === 'true')?true:false;
             } elseif (StringUtils::startsWith($againstAttr, '{') && StringUtils::endsWith($againstAttr, '}')) {
                 $arr = substr(explode(',', $againstAttr), 1, -1);
                 $againstAttr = array();
                 foreach ($arr as $a) {
                     $againstAttr[] = trim($a);
                 }
             } else {
                 $againstAttr = "'" . $againstAttr . "'";
             }
         }
         $operatorStr = '==';
         switch (strtolower($operatorAttr)) {
             case 'gt':
                 $operatorStr = '>';
                 break;
             case 'ge':
                 $operatorStr = '>=';
                 break;
             case 'lt':
                 $operatorStr = '<';
                 break;
             case 'le':
                 $operatorStr = '<=';
                 break;
             case 'eq':
                 $operatorStr = '==';
                 break;
             case 'ne':
                 $operatorStr = '!=';
                 break;
         }
         $phpCode = '<?php ';
         $phpCode .= 'if($this->getDataFromSelector(\'' . $compareAttr . '\') ' . $operatorStr . ' ' . $againstAttr . '): ?>';
         $phpCode .= $tagNode->getInnerHtml();
         if ($tplEngine->isFollowedBy($tagNode, array('else', 'elseif')) === false) {
             $phpCode .= '<?php endif; ?>';
         }
         $textNode = new TextNode($tplEngine->getDomReader());
         $textNode->content = $phpCode;
         $tagNode->parentNode->replaceNode($tagNode, $textNode);
         $tagNode->parentNode->removeNode($tagNode);
     } else {
         $phpCode = '<?php ';
         $phpCode .= 'if(' . preg_replace_callback('/\\${(.*?)}/i', function ($m) {
             if (strlen($m[1]) === 0) {
                 throw new TemplateEngineException('Empty template data reference');
             }
             return '$this->getDataFromSelector(\'' . $m[1] . '\')';
         }, $condAttr) . '): ?>';
         $phpCode .= $tagNode->getInnerHtml();
         if ($tplEngine->isFollowedBy($tagNode, array('else', 'elseif')) === false) {
             $phpCode .= '<?php endif; ?>';
         }
         $textNode = new TextNode($tplEngine->getDomReader());
         $textNode->content = $phpCode;
         $tagNode->parentNode->replaceNode($tagNode, $textNode);
         $tagNode->parentNode->removeNode($tagNode);
     }
 }
 /**
  * @param string $selector
  * @param BackendController $backendController
  *
  * @return mixed
  */
 protected function getValues($selector, BackendController $backendController)
 {
     if (is_string($selector) === true) {
         return StringUtils::endsWith($selector, '()') ? call_user_func(array($this, StringUtils::beforeLast($selector, '()')), $backendController) : $this->{$selector};
     }
     return $selector;
 }