vendor/symfony/form/ButtonBuilder.php line 746

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\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\Form\Exception\BadMethodCallException;
  13. use Symfony\Component\Form\Exception\InvalidArgumentException;
  14. /**
  15.  * A builder for {@link Button} instances.
  16.  *
  17.  * @author Bernhard Schussek <bschussek@gmail.com>
  18.  */
  19. class ButtonBuilder implements \IteratorAggregateFormBuilderInterface
  20. {
  21.     protected $locked false;
  22.     /**
  23.      * @var bool
  24.      */
  25.     private $disabled false;
  26.     /**
  27.      * @var ResolvedFormTypeInterface
  28.      */
  29.     private $type;
  30.     /**
  31.      * @var string
  32.      */
  33.     private $name;
  34.     /**
  35.      * @var array
  36.      */
  37.     private $attributes = [];
  38.     /**
  39.      * @var array
  40.      */
  41.     private $options;
  42.     /**
  43.      * @throws InvalidArgumentException if the name is empty
  44.      */
  45.     public function __construct(?string $name, array $options = [])
  46.     {
  47.         if ('' === $name || null === $name) {
  48.             throw new InvalidArgumentException('Buttons cannot have empty names.');
  49.         }
  50.         $this->name $name;
  51.         $this->options $options;
  52.         FormConfigBuilder::validateName($name);
  53.     }
  54.     /**
  55.      * Unsupported method.
  56.      *
  57.      * This method should not be invoked.
  58.      *
  59.      * @throws BadMethodCallException
  60.      */
  61.     public function add($childstring $type null, array $options = [])
  62.     {
  63.         throw new BadMethodCallException('Buttons cannot have children.');
  64.     }
  65.     /**
  66.      * Unsupported method.
  67.      *
  68.      * This method should not be invoked.
  69.      *
  70.      * @throws BadMethodCallException
  71.      */
  72.     public function create(string $namestring $type null, array $options = [])
  73.     {
  74.         throw new BadMethodCallException('Buttons cannot have children.');
  75.     }
  76.     /**
  77.      * Unsupported method.
  78.      *
  79.      * This method should not be invoked.
  80.      *
  81.      * @throws BadMethodCallException
  82.      */
  83.     public function get(string $name)
  84.     {
  85.         throw new BadMethodCallException('Buttons cannot have children.');
  86.     }
  87.     /**
  88.      * Unsupported method.
  89.      *
  90.      * This method should not be invoked.
  91.      *
  92.      * @throws BadMethodCallException
  93.      */
  94.     public function remove(string $name)
  95.     {
  96.         throw new BadMethodCallException('Buttons cannot have children.');
  97.     }
  98.     /**
  99.      * Unsupported method.
  100.      *
  101.      * @return bool Always returns false
  102.      */
  103.     public function has(string $name)
  104.     {
  105.         return false;
  106.     }
  107.     /**
  108.      * Returns the children.
  109.      *
  110.      * @return array Always returns an empty array
  111.      */
  112.     public function all()
  113.     {
  114.         return [];
  115.     }
  116.     /**
  117.      * Creates the button.
  118.      *
  119.      * @return Button The button
  120.      */
  121.     public function getForm()
  122.     {
  123.         return new Button($this->getFormConfig());
  124.     }
  125.     /**
  126.      * Unsupported method.
  127.      *
  128.      * This method should not be invoked.
  129.      *
  130.      * @throws BadMethodCallException
  131.      */
  132.     public function addEventListener(string $eventName, callable $listenerint $priority 0)
  133.     {
  134.         throw new BadMethodCallException('Buttons do not support event listeners.');
  135.     }
  136.     /**
  137.      * Unsupported method.
  138.      *
  139.      * This method should not be invoked.
  140.      *
  141.      * @throws BadMethodCallException
  142.      */
  143.     public function addEventSubscriber(EventSubscriberInterface $subscriber)
  144.     {
  145.         throw new BadMethodCallException('Buttons do not support event subscribers.');
  146.     }
  147.     /**
  148.      * Unsupported method.
  149.      *
  150.      * This method should not be invoked.
  151.      *
  152.      * @throws BadMethodCallException
  153.      */
  154.     public function addViewTransformer(DataTransformerInterface $viewTransformerbool $forcePrepend false)
  155.     {
  156.         throw new BadMethodCallException('Buttons do not support data transformers.');
  157.     }
  158.     /**
  159.      * Unsupported method.
  160.      *
  161.      * This method should not be invoked.
  162.      *
  163.      * @throws BadMethodCallException
  164.      */
  165.     public function resetViewTransformers()
  166.     {
  167.         throw new BadMethodCallException('Buttons do not support data transformers.');
  168.     }
  169.     /**
  170.      * Unsupported method.
  171.      *
  172.      * This method should not be invoked.
  173.      *
  174.      * @throws BadMethodCallException
  175.      */
  176.     public function addModelTransformer(DataTransformerInterface $modelTransformerbool $forceAppend false)
  177.     {
  178.         throw new BadMethodCallException('Buttons do not support data transformers.');
  179.     }
  180.     /**
  181.      * Unsupported method.
  182.      *
  183.      * This method should not be invoked.
  184.      *
  185.      * @throws BadMethodCallException
  186.      */
  187.     public function resetModelTransformers()
  188.     {
  189.         throw new BadMethodCallException('Buttons do not support data transformers.');
  190.     }
  191.     /**
  192.      * {@inheritdoc}
  193.      */
  194.     public function setAttribute(string $name$value)
  195.     {
  196.         $this->attributes[$name] = $value;
  197.         return $this;
  198.     }
  199.     /**
  200.      * {@inheritdoc}
  201.      */
  202.     public function setAttributes(array $attributes)
  203.     {
  204.         $this->attributes $attributes;
  205.         return $this;
  206.     }
  207.     /**
  208.      * Unsupported method.
  209.      *
  210.      * This method should not be invoked.
  211.      *
  212.      * @throws BadMethodCallException
  213.      */
  214.     public function setDataMapper(DataMapperInterface $dataMapper null)
  215.     {
  216.         throw new BadMethodCallException('Buttons do not support data mappers.');
  217.     }
  218.     /**
  219.      * Set whether the button is disabled.
  220.      *
  221.      * @return $this
  222.      */
  223.     public function setDisabled(bool $disabled)
  224.     {
  225.         $this->disabled $disabled;
  226.         return $this;
  227.     }
  228.     /**
  229.      * Unsupported method.
  230.      *
  231.      * This method should not be invoked.
  232.      *
  233.      * @param mixed $emptyData
  234.      *
  235.      * @throws BadMethodCallException
  236.      */
  237.     public function setEmptyData($emptyData)
  238.     {
  239.         throw new BadMethodCallException('Buttons do not support empty data.');
  240.     }
  241.     /**
  242.      * Unsupported method.
  243.      *
  244.      * This method should not be invoked.
  245.      *
  246.      * @throws BadMethodCallException
  247.      */
  248.     public function setErrorBubbling(bool $errorBubbling)
  249.     {
  250.         throw new BadMethodCallException('Buttons do not support error bubbling.');
  251.     }
  252.     /**
  253.      * Unsupported method.
  254.      *
  255.      * This method should not be invoked.
  256.      *
  257.      * @throws BadMethodCallException
  258.      */
  259.     public function setRequired(bool $required)
  260.     {
  261.         throw new BadMethodCallException('Buttons cannot be required.');
  262.     }
  263.     /**
  264.      * Unsupported method.
  265.      *
  266.      * This method should not be invoked.
  267.      *
  268.      * @param null $propertyPath
  269.      *
  270.      * @throws BadMethodCallException
  271.      */
  272.     public function setPropertyPath($propertyPath)
  273.     {
  274.         throw new BadMethodCallException('Buttons do not support property paths.');
  275.     }
  276.     /**
  277.      * Unsupported method.
  278.      *
  279.      * This method should not be invoked.
  280.      *
  281.      * @throws BadMethodCallException
  282.      */
  283.     public function setMapped(bool $mapped)
  284.     {
  285.         throw new BadMethodCallException('Buttons do not support data mapping.');
  286.     }
  287.     /**
  288.      * Unsupported method.
  289.      *
  290.      * This method should not be invoked.
  291.      *
  292.      * @throws BadMethodCallException
  293.      */
  294.     public function setByReference(bool $byReference)
  295.     {
  296.         throw new BadMethodCallException('Buttons do not support data mapping.');
  297.     }
  298.     /**
  299.      * Unsupported method.
  300.      *
  301.      * This method should not be invoked.
  302.      *
  303.      * @throws BadMethodCallException
  304.      */
  305.     public function setCompound(bool $compound)
  306.     {
  307.         throw new BadMethodCallException('Buttons cannot be compound.');
  308.     }
  309.     /**
  310.      * Sets the type of the button.
  311.      *
  312.      * @return $this
  313.      */
  314.     public function setType(ResolvedFormTypeInterface $type)
  315.     {
  316.         $this->type $type;
  317.         return $this;
  318.     }
  319.     /**
  320.      * Unsupported method.
  321.      *
  322.      * This method should not be invoked.
  323.      *
  324.      * @param mixed $data
  325.      *
  326.      * @throws BadMethodCallException
  327.      */
  328.     public function setData($data)
  329.     {
  330.         throw new BadMethodCallException('Buttons do not support data.');
  331.     }
  332.     /**
  333.      * Unsupported method.
  334.      *
  335.      * This method should not be invoked.
  336.      *
  337.      * @throws BadMethodCallException
  338.      */
  339.     public function setDataLocked(bool $locked)
  340.     {
  341.         throw new BadMethodCallException('Buttons do not support data locking.');
  342.     }
  343.     /**
  344.      * Unsupported method.
  345.      *
  346.      * This method should not be invoked.
  347.      *
  348.      * @throws BadMethodCallException
  349.      */
  350.     public function setFormFactory(FormFactoryInterface $formFactory)
  351.     {
  352.         throw new BadMethodCallException('Buttons do not support form factories.');
  353.     }
  354.     /**
  355.      * Unsupported method.
  356.      *
  357.      * @throws BadMethodCallException
  358.      */
  359.     public function setAction(string $action)
  360.     {
  361.         throw new BadMethodCallException('Buttons do not support actions.');
  362.     }
  363.     /**
  364.      * Unsupported method.
  365.      *
  366.      * @throws BadMethodCallException
  367.      */
  368.     public function setMethod(string $method)
  369.     {
  370.         throw new BadMethodCallException('Buttons do not support methods.');
  371.     }
  372.     /**
  373.      * Unsupported method.
  374.      *
  375.      * @throws BadMethodCallException
  376.      */
  377.     public function setRequestHandler(RequestHandlerInterface $requestHandler)
  378.     {
  379.         throw new BadMethodCallException('Buttons do not support request handlers.');
  380.     }
  381.     /**
  382.      * Unsupported method.
  383.      *
  384.      * @return $this
  385.      *
  386.      * @throws BadMethodCallException
  387.      */
  388.     public function setAutoInitialize(bool $initialize)
  389.     {
  390.         if (true === $initialize) {
  391.             throw new BadMethodCallException('Buttons do not support automatic initialization.');
  392.         }
  393.         return $this;
  394.     }
  395.     /**
  396.      * Unsupported method.
  397.      *
  398.      * @throws BadMethodCallException
  399.      */
  400.     public function setInheritData(bool $inheritData)
  401.     {
  402.         throw new BadMethodCallException('Buttons do not support data inheritance.');
  403.     }
  404.     /**
  405.      * Builds and returns the button configuration.
  406.      *
  407.      * @return FormConfigInterface
  408.      */
  409.     public function getFormConfig()
  410.     {
  411.         // This method should be idempotent, so clone the builder
  412.         $config = clone $this;
  413.         $config->locked true;
  414.         return $config;
  415.     }
  416.     /**
  417.      * Unsupported method.
  418.      */
  419.     public function getEventDispatcher()
  420.     {
  421.         return null;
  422.     }
  423.     /**
  424.      * {@inheritdoc}
  425.      */
  426.     public function getName()
  427.     {
  428.         return $this->name;
  429.     }
  430.     /**
  431.      * Unsupported method.
  432.      */
  433.     public function getPropertyPath()
  434.     {
  435.         return null;
  436.     }
  437.     /**
  438.      * Unsupported method.
  439.      *
  440.      * @return bool Always returns false
  441.      */
  442.     public function getMapped()
  443.     {
  444.         return false;
  445.     }
  446.     /**
  447.      * Unsupported method.
  448.      *
  449.      * @return bool Always returns false
  450.      */
  451.     public function getByReference()
  452.     {
  453.         return false;
  454.     }
  455.     /**
  456.      * Unsupported method.
  457.      *
  458.      * @return bool Always returns false
  459.      */
  460.     public function getCompound()
  461.     {
  462.         return false;
  463.     }
  464.     /**
  465.      * Returns the form type used to construct the button.
  466.      *
  467.      * @return ResolvedFormTypeInterface The button's type
  468.      */
  469.     public function getType()
  470.     {
  471.         return $this->type;
  472.     }
  473.     /**
  474.      * Unsupported method.
  475.      *
  476.      * @return array Always returns an empty array
  477.      */
  478.     public function getViewTransformers()
  479.     {
  480.         return [];
  481.     }
  482.     /**
  483.      * Unsupported method.
  484.      *
  485.      * @return array Always returns an empty array
  486.      */
  487.     public function getModelTransformers()
  488.     {
  489.         return [];
  490.     }
  491.     /**
  492.      * Unsupported method.
  493.      */
  494.     public function getDataMapper()
  495.     {
  496.         return null;
  497.     }
  498.     /**
  499.      * Unsupported method.
  500.      *
  501.      * @return bool Always returns false
  502.      */
  503.     public function getRequired()
  504.     {
  505.         return false;
  506.     }
  507.     /**
  508.      * Returns whether the button is disabled.
  509.      *
  510.      * @return bool Whether the button is disabled
  511.      */
  512.     public function getDisabled()
  513.     {
  514.         return $this->disabled;
  515.     }
  516.     /**
  517.      * Unsupported method.
  518.      *
  519.      * @return bool Always returns false
  520.      */
  521.     public function getErrorBubbling()
  522.     {
  523.         return false;
  524.     }
  525.     /**
  526.      * Unsupported method.
  527.      */
  528.     public function getEmptyData()
  529.     {
  530.         return null;
  531.     }
  532.     /**
  533.      * Returns additional attributes of the button.
  534.      *
  535.      * @return array An array of key-value combinations
  536.      */
  537.     public function getAttributes()
  538.     {
  539.         return $this->attributes;
  540.     }
  541.     /**
  542.      * Returns whether the attribute with the given name exists.
  543.      *
  544.      * @return bool Whether the attribute exists
  545.      */
  546.     public function hasAttribute(string $name)
  547.     {
  548.         return \array_key_exists($name$this->attributes);
  549.     }
  550.     /**
  551.      * Returns the value of the given attribute.
  552.      *
  553.      * @param mixed $default The value returned if the attribute does not exist
  554.      *
  555.      * @return mixed The attribute value
  556.      */
  557.     public function getAttribute(string $name$default null)
  558.     {
  559.         return \array_key_exists($name$this->attributes) ? $this->attributes[$name] : $default;
  560.     }
  561.     /**
  562.      * Unsupported method.
  563.      */
  564.     public function getData()
  565.     {
  566.         return null;
  567.     }
  568.     /**
  569.      * Unsupported method.
  570.      */
  571.     public function getDataClass()
  572.     {
  573.         return null;
  574.     }
  575.     /**
  576.      * Unsupported method.
  577.      *
  578.      * @return bool Always returns false
  579.      */
  580.     public function getDataLocked()
  581.     {
  582.         return false;
  583.     }
  584.     /**
  585.      * Unsupported method.
  586.      */
  587.     public function getFormFactory()
  588.     {
  589.         throw new BadMethodCallException('Buttons do not support adding children.');
  590.     }
  591.     /**
  592.      * Unsupported method.
  593.      */
  594.     public function getAction()
  595.     {
  596.         return null;
  597.     }
  598.     /**
  599.      * Unsupported method.
  600.      */
  601.     public function getMethod()
  602.     {
  603.         return null;
  604.     }
  605.     /**
  606.      * Unsupported method.
  607.      */
  608.     public function getRequestHandler()
  609.     {
  610.         return null;
  611.     }
  612.     /**
  613.      * Unsupported method.
  614.      *
  615.      * @return bool Always returns false
  616.      */
  617.     public function getAutoInitialize()
  618.     {
  619.         return false;
  620.     }
  621.     /**
  622.      * Unsupported method.
  623.      *
  624.      * @return bool Always returns false
  625.      */
  626.     public function getInheritData()
  627.     {
  628.         return false;
  629.     }
  630.     /**
  631.      * Returns all options passed during the construction of the button.
  632.      *
  633.      * @return array The passed options
  634.      */
  635.     public function getOptions()
  636.     {
  637.         return $this->options;
  638.     }
  639.     /**
  640.      * Returns whether a specific option exists.
  641.      *
  642.      * @return bool Whether the option exists
  643.      */
  644.     public function hasOption(string $name)
  645.     {
  646.         return \array_key_exists($name$this->options);
  647.     }
  648.     /**
  649.      * Returns the value of a specific option.
  650.      *
  651.      * @param mixed $default The value returned if the option does not exist
  652.      *
  653.      * @return mixed The option value
  654.      */
  655.     public function getOption(string $name$default null)
  656.     {
  657.         return \array_key_exists($name$this->options) ? $this->options[$name] : $default;
  658.     }
  659.     /**
  660.      * Unsupported method.
  661.      *
  662.      * @return int Always returns 0
  663.      */
  664.     public function count()
  665.     {
  666.         return 0;
  667.     }
  668.     /**
  669.      * Unsupported method.
  670.      *
  671.      * @return \EmptyIterator Always returns an empty iterator
  672.      */
  673.     public function getIterator()
  674.     {
  675.         return new \EmptyIterator();
  676.     }
  677. }