vendor/symfony/form/Button.php line 433

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\Form;
  11. use Symfony\Component\Form\Exception\AlreadySubmittedException;
  12. use Symfony\Component\Form\Exception\BadMethodCallException;
  13. /**
  14.  * A form button.
  15.  *
  16.  * @author Bernhard Schussek <bschussek@gmail.com>
  17.  */
  18. class Button implements \IteratorAggregateFormInterface
  19. {
  20.     /**
  21.      * @var FormInterface
  22.      */
  23.     private $parent;
  24.     /**
  25.      * @var FormConfigInterface
  26.      */
  27.     private $config;
  28.     /**
  29.      * @var bool
  30.      */
  31.     private $submitted false;
  32.     /**
  33.      * Creates a new button from a form configuration.
  34.      */
  35.     public function __construct(FormConfigInterface $config)
  36.     {
  37.         $this->config $config;
  38.     }
  39.     /**
  40.      * Unsupported method.
  41.      *
  42.      * @param mixed $offset
  43.      *
  44.      * @return bool Always returns false
  45.      */
  46.     public function offsetExists($offset)
  47.     {
  48.         return false;
  49.     }
  50.     /**
  51.      * Unsupported method.
  52.      *
  53.      * This method should not be invoked.
  54.      *
  55.      * @param mixed $offset
  56.      *
  57.      * @throws BadMethodCallException
  58.      */
  59.     public function offsetGet($offset)
  60.     {
  61.         throw new BadMethodCallException('Buttons cannot have children.');
  62.     }
  63.     /**
  64.      * Unsupported method.
  65.      *
  66.      * This method should not be invoked.
  67.      *
  68.      * @param mixed $offset
  69.      * @param mixed $value
  70.      *
  71.      * @throws BadMethodCallException
  72.      */
  73.     public function offsetSet($offset$value)
  74.     {
  75.         throw new BadMethodCallException('Buttons cannot have children.');
  76.     }
  77.     /**
  78.      * Unsupported method.
  79.      *
  80.      * This method should not be invoked.
  81.      *
  82.      * @param mixed $offset
  83.      *
  84.      * @throws BadMethodCallException
  85.      */
  86.     public function offsetUnset($offset)
  87.     {
  88.         throw new BadMethodCallException('Buttons cannot have children.');
  89.     }
  90.     /**
  91.      * {@inheritdoc}
  92.      */
  93.     public function setParent(FormInterface $parent null)
  94.     {
  95.         if ($this->submitted) {
  96.             throw new AlreadySubmittedException('You cannot set the parent of a submitted button.');
  97.         }
  98.         $this->parent $parent;
  99.         return $this;
  100.     }
  101.     /**
  102.      * {@inheritdoc}
  103.      */
  104.     public function getParent()
  105.     {
  106.         return $this->parent;
  107.     }
  108.     /**
  109.      * Unsupported method.
  110.      *
  111.      * This method should not be invoked.
  112.      *
  113.      * @throws BadMethodCallException
  114.      */
  115.     public function add($child$type null, array $options = [])
  116.     {
  117.         throw new BadMethodCallException('Buttons cannot have children.');
  118.     }
  119.     /**
  120.      * Unsupported method.
  121.      *
  122.      * This method should not be invoked.
  123.      *
  124.      * @param string $name
  125.      *
  126.      * @throws BadMethodCallException
  127.      */
  128.     public function get($name)
  129.     {
  130.         throw new BadMethodCallException('Buttons cannot have children.');
  131.     }
  132.     /**
  133.      * Unsupported method.
  134.      *
  135.      * @param string $name
  136.      *
  137.      * @return bool Always returns false
  138.      */
  139.     public function has($name)
  140.     {
  141.         return false;
  142.     }
  143.     /**
  144.      * Unsupported method.
  145.      *
  146.      * This method should not be invoked.
  147.      *
  148.      * @param string $name
  149.      *
  150.      * @throws BadMethodCallException
  151.      */
  152.     public function remove($name)
  153.     {
  154.         throw new BadMethodCallException('Buttons cannot have children.');
  155.     }
  156.     /**
  157.      * {@inheritdoc}
  158.      */
  159.     public function all()
  160.     {
  161.         return [];
  162.     }
  163.     /**
  164.      * {@inheritdoc}
  165.      */
  166.     public function getErrors($deep false$flatten true)
  167.     {
  168.         return new FormErrorIterator($this, []);
  169.     }
  170.     /**
  171.      * Unsupported method.
  172.      *
  173.      * This method should not be invoked.
  174.      *
  175.      * @param mixed $modelData
  176.      *
  177.      * @return $this
  178.      */
  179.     public function setData($modelData)
  180.     {
  181.         // no-op, called during initialization of the form tree
  182.         return $this;
  183.     }
  184.     /**
  185.      * Unsupported method.
  186.      */
  187.     public function getData()
  188.     {
  189.         return null;
  190.     }
  191.     /**
  192.      * Unsupported method.
  193.      */
  194.     public function getNormData()
  195.     {
  196.         return null;
  197.     }
  198.     /**
  199.      * Unsupported method.
  200.      */
  201.     public function getViewData()
  202.     {
  203.         return null;
  204.     }
  205.     /**
  206.      * Unsupported method.
  207.      *
  208.      * @return array Always returns an empty array
  209.      */
  210.     public function getExtraData()
  211.     {
  212.         return [];
  213.     }
  214.     /**
  215.      * Returns the button's configuration.
  216.      *
  217.      * @return FormConfigInterface The configuration instance
  218.      */
  219.     public function getConfig()
  220.     {
  221.         return $this->config;
  222.     }
  223.     /**
  224.      * Returns whether the button is submitted.
  225.      *
  226.      * @return bool true if the button was submitted
  227.      */
  228.     public function isSubmitted()
  229.     {
  230.         return $this->submitted;
  231.     }
  232.     /**
  233.      * Returns the name by which the button is identified in forms.
  234.      *
  235.      * @return string The name of the button
  236.      */
  237.     public function getName()
  238.     {
  239.         return $this->config->getName();
  240.     }
  241.     /**
  242.      * Unsupported method.
  243.      */
  244.     public function getPropertyPath()
  245.     {
  246.         return null;
  247.     }
  248.     /**
  249.      * Unsupported method.
  250.      *
  251.      * @throws BadMethodCallException
  252.      */
  253.     public function addError(FormError $error)
  254.     {
  255.         throw new BadMethodCallException('Buttons cannot have errors.');
  256.     }
  257.     /**
  258.      * Unsupported method.
  259.      *
  260.      * @return bool Always returns true
  261.      */
  262.     public function isValid()
  263.     {
  264.         return true;
  265.     }
  266.     /**
  267.      * Unsupported method.
  268.      *
  269.      * @return bool Always returns false
  270.      */
  271.     public function isRequired()
  272.     {
  273.         return false;
  274.     }
  275.     /**
  276.      * {@inheritdoc}
  277.      */
  278.     public function isDisabled()
  279.     {
  280.         if ($this->parent && $this->parent->isDisabled()) {
  281.             return true;
  282.         }
  283.         return $this->config->getDisabled();
  284.     }
  285.     /**
  286.      * Unsupported method.
  287.      *
  288.      * @return bool Always returns true
  289.      */
  290.     public function isEmpty()
  291.     {
  292.         return true;
  293.     }
  294.     /**
  295.      * Unsupported method.
  296.      *
  297.      * @return bool Always returns true
  298.      */
  299.     public function isSynchronized()
  300.     {
  301.         return true;
  302.     }
  303.     /**
  304.      * Unsupported method.
  305.      */
  306.     public function getTransformationFailure()
  307.     {
  308.         return null;
  309.     }
  310.     /**
  311.      * Unsupported method.
  312.      *
  313.      * @throws BadMethodCallException
  314.      */
  315.     public function initialize()
  316.     {
  317.         throw new BadMethodCallException('Buttons cannot be initialized. Call initialize() on the root form instead.');
  318.     }
  319.     /**
  320.      * Unsupported method.
  321.      *
  322.      * @param mixed $request
  323.      *
  324.      * @throws BadMethodCallException
  325.      */
  326.     public function handleRequest($request null)
  327.     {
  328.         throw new BadMethodCallException('Buttons cannot handle requests. Call handleRequest() on the root form instead.');
  329.     }
  330.     /**
  331.      * Submits data to the button.
  332.      *
  333.      * @param string|null $submittedData Not used
  334.      * @param bool        $clearMissing  Not used
  335.      *
  336.      * @return $this
  337.      *
  338.      * @throws Exception\AlreadySubmittedException if the button has already been submitted
  339.      */
  340.     public function submit($submittedData$clearMissing true)
  341.     {
  342.         if ($this->submitted) {
  343.             throw new AlreadySubmittedException('A form can only be submitted once.');
  344.         }
  345.         $this->submitted true;
  346.         return $this;
  347.     }
  348.     /**
  349.      * {@inheritdoc}
  350.      */
  351.     public function getRoot()
  352.     {
  353.         return $this->parent $this->parent->getRoot() : $this;
  354.     }
  355.     /**
  356.      * {@inheritdoc}
  357.      */
  358.     public function isRoot()
  359.     {
  360.         return null === $this->parent;
  361.     }
  362.     /**
  363.      * {@inheritdoc}
  364.      */
  365.     public function createView(FormView $parent null)
  366.     {
  367.         if (null === $parent && $this->parent) {
  368.             $parent $this->parent->createView();
  369.         }
  370.         $type $this->config->getType();
  371.         $options $this->config->getOptions();
  372.         $view $type->createView($this$parent);
  373.         $type->buildView($view$this$options);
  374.         $type->finishView($view$this$options);
  375.         return $view;
  376.     }
  377.     /**
  378.      * Unsupported method.
  379.      *
  380.      * @return int Always returns 0
  381.      */
  382.     public function count()
  383.     {
  384.         return 0;
  385.     }
  386.     /**
  387.      * Unsupported method.
  388.      *
  389.      * @return \EmptyIterator Always returns an empty iterator
  390.      */
  391.     public function getIterator()
  392.     {
  393.         return new \EmptyIterator();
  394.     }
  395. }