Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Callback.php
Go to the documentation of this file.
1 <?php
9 
10 class Callback implements \Magento\Framework\Validator\Constraint\OptionInterface
11 {
15  protected $_callable;
16 
20  protected $_arguments;
21 
25  protected $_createInstance;
26 
34  public function __construct($callable, $arguments = null, $createInstance = false)
35  {
36  $this->_callable = $callable;
37  $this->setArguments($arguments);
38  $this->_createInstance = $createInstance;
39  }
40 
47  public function setArguments($arguments = null)
48  {
49  if (is_array($arguments)) {
50  $this->_arguments = $arguments;
51  } elseif (null !== $arguments) {
52  $this->_arguments = [$arguments];
53  } else {
54  $this->_arguments = null;
55  }
56  }
57 
64  public function getValue()
65  {
66  $callable = $this->_callable;
67 
68  if (is_array($callable) && isset($callable[0]) && is_string($callable[0])) {
69  if (!class_exists($callable[0])) {
70  throw new \InvalidArgumentException(sprintf('Class "%s" was not found', $callable[0]));
71  }
72  if ($this->_createInstance) {
73  $callable[0] = new $callable[0]();
74  }
75  } elseif ($this->_createInstance) {
76  throw new \InvalidArgumentException('Callable expected to be an array with class name as first element');
77  }
78 
79  if (!is_callable($callable)) {
80  throw new \InvalidArgumentException('Callback does not callable');
81  }
82 
83  if ($this->_arguments) {
84  return call_user_func_array($callable, $this->_arguments);
85  } else {
86  return call_user_func($callable);
87  }
88  }
89 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
__construct($callable, $arguments=null, $createInstance=false)
Definition: Callback.php:34
$arguments