Zend\Code\Scanner\ClassScanner::getConstant PHP Method

getConstant() public method

Return a single constant by given name or index of info
public getConstant ( string | integer $constantNameOrInfoIndex ) : boolean | Zend\Code\Scanner\ConstantScanner
$constantNameOrInfoIndex string | integer
return boolean | Zend\Code\Scanner\ConstantScanner
    public function getConstant($constantNameOrInfoIndex)
    {
        $this->scan();
        if (is_int($constantNameOrInfoIndex)) {
            $info = $this->infos[$constantNameOrInfoIndex];
            if ($info['type'] != 'constant') {
                throw new Exception\InvalidArgumentException('Index of info offset is not about a constant');
            }
        } elseif (is_string($constantNameOrInfoIndex)) {
            $constantFound = false;
            foreach ($this->infos as $info) {
                if ($info['type'] === 'constant' && $info['name'] === $constantNameOrInfoIndex) {
                    $constantFound = true;
                    break;
                }
            }
            if (!$constantFound) {
                return false;
            }
        } else {
            throw new Exception\InvalidArgumentException('Invalid constant name of info index type.  Must be of type int or string');
        }
        if (!isset($info)) {
            return false;
        }
        $p = new ConstantScanner(array_slice($this->tokens, $info['tokenStart'], $info['tokenEnd'] - $info['tokenStart'] + 1), $this->nameInformation);
        $p->setClass($this->name);
        $p->setScannerClass($this);
        return $p;
    }

Usage Example

Ejemplo n.º 1
0
 /**
  * Return a single constant by given name or index of info
  *
  * @param  string|int $constantNameOrInfoIndex
  * @throws Exception\InvalidArgumentException
  * @return bool|ConstantScanner
  */
 public function getConstant($constantNameOrInfoIndex)
 {
     if ($this->classScanner->hasConstant($constantNameOrInfoIndex)) {
         return $this->classScanner->getConstant($constantNameOrInfoIndex);
     }
     foreach ($this->parentClassScanners as $pClassScanner) {
         if ($pClassScanner->hasConstant($constantNameOrInfoIndex)) {
             return $pClassScanner->getConstant($constantNameOrInfoIndex);
         }
     }
     throw new Exception\InvalidArgumentException(sprintf('Constant %s not found in %s', $constantNameOrInfoIndex, $this->classScanner->getName()));
 }