yii\base\Object::canGetProperty PHP Method

canGetProperty() public method

A property is readable if: - the class has a getter method associated with the specified name (in this case, property name is case-insensitive); - the class has a member variable with the specified name (when $checkVars is true);
See also: canSetProperty()
public canGetProperty ( string $name, boolean $checkVars = true ) : boolean
$name string the property name
$checkVars boolean whether to treat member variables as properties
return boolean whether the property can be read
    public function canGetProperty($name, $checkVars = true)
    {
        return method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name);
    }

Usage Example

 /**
  * @return string
  * @throws NotAcceptableHttpException
  */
 public function run()
 {
     if (!Yii::$app->request->isAjax) {
         throw new NotAcceptableHttpException('This action AJAX only!');
     }
     $post = Yii::$app->request->post();
     $keys = array_keys($post);
     $formName = $keys[0];
     if (!isset($post[$formName][$this->textFormatField])) {
         throw new InvalidParamException('Invalid POST data.');
     }
     $format = $post[$formName][$this->textFormatField];
     $text = $post[$formName][$this->textField];
     if ($this->context === null) {
         $this->context = Yii::$app->controller;
         if (!$this->context->canGetProperty('textFormats') || !$this->context->canGetProperty('textEditorWidgetOptions')) {
             $this->context = Yii::$app->controller->module;
         }
     }
     if (!$this->context->canGetProperty('textFormats') || !$this->context->canGetProperty('textEditorWidgetOptions')) {
         throw new InvalidParamException('Invalid context. Add TextFormatsBehavior to module.');
     }
     $formats = $this->context->textFormats;
     if (!isset($formats[$format])) {
         throw new InvalidParamException('Format not found.');
     }
     $params = ['fieldName' => $formName . '[' . $this->textField . ']', 'text' => $text, 'formatInfo' => $formats[$format], 'widgetOptions' => $this->context->textEditorWidgetOptions];
     if ($this->view) {
         return $this->controller->renderAjax($this->view, $params);
     } else {
         return $this->controller->renderAjax('@vendor/maddoger/yii2-textformats/views/changeFormat.php', $params);
     }
 }
All Usage Examples Of yii\base\Object::canGetProperty