vendor/symfony/form/ButtonBuilder.php line 23

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