FormConfigBuilder.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  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\EventDispatcherInterface;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Component\EventDispatcher\ImmutableEventDispatcher;
  14. use Symfony\Component\Form\Exception\BadMethodCallException;
  15. use Symfony\Component\Form\Exception\InvalidArgumentException;
  16. use Symfony\Component\Form\Exception\UnexpectedTypeException;
  17. use Symfony\Component\PropertyAccess\PropertyPath;
  18. use Symfony\Component\PropertyAccess\PropertyPathInterface;
  19. /**
  20. * A basic form configuration.
  21. *
  22. * @author Bernhard Schussek <bschussek@gmail.com>
  23. */
  24. class FormConfigBuilder implements FormConfigBuilderInterface
  25. {
  26. /**
  27. * Caches a globally unique {@link NativeRequestHandler} instance.
  28. *
  29. * @var NativeRequestHandler
  30. */
  31. private static $nativeRequestHandler;
  32. /**
  33. * The accepted request methods.
  34. *
  35. * @var array
  36. */
  37. private static $allowedMethods = array(
  38. 'GET',
  39. 'PUT',
  40. 'POST',
  41. 'DELETE',
  42. 'PATCH',
  43. );
  44. /**
  45. * @var bool
  46. */
  47. protected $locked = false;
  48. /**
  49. * @var EventDispatcherInterface
  50. */
  51. private $dispatcher;
  52. /**
  53. * @var string
  54. */
  55. private $name;
  56. /**
  57. * @var PropertyPathInterface
  58. */
  59. private $propertyPath;
  60. /**
  61. * @var bool
  62. */
  63. private $mapped = true;
  64. /**
  65. * @var bool
  66. */
  67. private $byReference = true;
  68. /**
  69. * @var bool
  70. */
  71. private $inheritData = false;
  72. /**
  73. * @var bool
  74. */
  75. private $compound = false;
  76. /**
  77. * @var ResolvedFormTypeInterface
  78. */
  79. private $type;
  80. /**
  81. * @var array
  82. */
  83. private $viewTransformers = array();
  84. /**
  85. * @var array
  86. */
  87. private $modelTransformers = array();
  88. /**
  89. * @var DataMapperInterface
  90. */
  91. private $dataMapper;
  92. /**
  93. * @var bool
  94. */
  95. private $required = true;
  96. /**
  97. * @var bool
  98. */
  99. private $disabled = false;
  100. /**
  101. * @var bool
  102. */
  103. private $errorBubbling = false;
  104. /**
  105. * @var mixed
  106. */
  107. private $emptyData;
  108. /**
  109. * @var array
  110. */
  111. private $attributes = array();
  112. /**
  113. * @var mixed
  114. */
  115. private $data;
  116. /**
  117. * @var string|null
  118. */
  119. private $dataClass;
  120. /**
  121. * @var bool
  122. */
  123. private $dataLocked;
  124. /**
  125. * @var FormFactoryInterface
  126. */
  127. private $formFactory;
  128. /**
  129. * @var string
  130. */
  131. private $action;
  132. /**
  133. * @var string
  134. */
  135. private $method = 'POST';
  136. /**
  137. * @var RequestHandlerInterface
  138. */
  139. private $requestHandler;
  140. /**
  141. * @var bool
  142. */
  143. private $autoInitialize = false;
  144. /**
  145. * @var array
  146. */
  147. private $options;
  148. /**
  149. * Creates an empty form configuration.
  150. *
  151. * @param string|int $name The form name
  152. * @param string|null $dataClass The class of the form's data
  153. * @param EventDispatcherInterface $dispatcher The event dispatcher
  154. * @param array $options The form options
  155. *
  156. * @throws InvalidArgumentException if the data class is not a valid class or if
  157. * the name contains invalid characters
  158. */
  159. public function __construct($name, $dataClass, EventDispatcherInterface $dispatcher, array $options = array())
  160. {
  161. self::validateName($name);
  162. if (null !== $dataClass && !class_exists($dataClass) && !interface_exists($dataClass)) {
  163. throw new InvalidArgumentException(sprintf('Class "%s" not found. Is the "data_class" form option set correctly?', $dataClass));
  164. }
  165. $this->name = (string) $name;
  166. $this->dataClass = $dataClass;
  167. $this->dispatcher = $dispatcher;
  168. $this->options = $options;
  169. }
  170. /**
  171. * {@inheritdoc}
  172. */
  173. public function addEventListener($eventName, $listener, $priority = 0)
  174. {
  175. if ($this->locked) {
  176. throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
  177. }
  178. $this->dispatcher->addListener($eventName, $listener, $priority);
  179. return $this;
  180. }
  181. /**
  182. * {@inheritdoc}
  183. */
  184. public function addEventSubscriber(EventSubscriberInterface $subscriber)
  185. {
  186. if ($this->locked) {
  187. throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
  188. }
  189. $this->dispatcher->addSubscriber($subscriber);
  190. return $this;
  191. }
  192. /**
  193. * {@inheritdoc}
  194. */
  195. public function addViewTransformer(DataTransformerInterface $viewTransformer, $forcePrepend = false)
  196. {
  197. if ($this->locked) {
  198. throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
  199. }
  200. if ($forcePrepend) {
  201. array_unshift($this->viewTransformers, $viewTransformer);
  202. } else {
  203. $this->viewTransformers[] = $viewTransformer;
  204. }
  205. return $this;
  206. }
  207. /**
  208. * {@inheritdoc}
  209. */
  210. public function resetViewTransformers()
  211. {
  212. if ($this->locked) {
  213. throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
  214. }
  215. $this->viewTransformers = array();
  216. return $this;
  217. }
  218. /**
  219. * {@inheritdoc}
  220. */
  221. public function addModelTransformer(DataTransformerInterface $modelTransformer, $forceAppend = false)
  222. {
  223. if ($this->locked) {
  224. throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
  225. }
  226. if ($forceAppend) {
  227. $this->modelTransformers[] = $modelTransformer;
  228. } else {
  229. array_unshift($this->modelTransformers, $modelTransformer);
  230. }
  231. return $this;
  232. }
  233. /**
  234. * {@inheritdoc}
  235. */
  236. public function resetModelTransformers()
  237. {
  238. if ($this->locked) {
  239. throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
  240. }
  241. $this->modelTransformers = array();
  242. return $this;
  243. }
  244. /**
  245. * {@inheritdoc}
  246. */
  247. public function getEventDispatcher()
  248. {
  249. if ($this->locked && !$this->dispatcher instanceof ImmutableEventDispatcher) {
  250. $this->dispatcher = new ImmutableEventDispatcher($this->dispatcher);
  251. }
  252. return $this->dispatcher;
  253. }
  254. /**
  255. * {@inheritdoc}
  256. */
  257. public function getName()
  258. {
  259. return $this->name;
  260. }
  261. /**
  262. * {@inheritdoc}
  263. */
  264. public function getPropertyPath()
  265. {
  266. return $this->propertyPath;
  267. }
  268. /**
  269. * {@inheritdoc}
  270. */
  271. public function getMapped()
  272. {
  273. return $this->mapped;
  274. }
  275. /**
  276. * {@inheritdoc}
  277. */
  278. public function getByReference()
  279. {
  280. return $this->byReference;
  281. }
  282. /**
  283. * {@inheritdoc}
  284. */
  285. public function getInheritData()
  286. {
  287. return $this->inheritData;
  288. }
  289. /**
  290. * Alias of {@link getInheritData()}.
  291. *
  292. * @return bool
  293. *
  294. * @deprecated since version 2.3, to be removed in 3.0.
  295. * Use {@link getInheritData()} instead.
  296. */
  297. public function getVirtual()
  298. {
  299. @trigger_error('The '.__METHOD__.' method is deprecated since Symfony 2.3 and will be removed in 3.0. Use the FormConfigBuilder::getInheritData() method instead.', E_USER_DEPRECATED);
  300. return $this->getInheritData();
  301. }
  302. /**
  303. * {@inheritdoc}
  304. */
  305. public function getCompound()
  306. {
  307. return $this->compound;
  308. }
  309. /**
  310. * {@inheritdoc}
  311. */
  312. public function getType()
  313. {
  314. return $this->type;
  315. }
  316. /**
  317. * {@inheritdoc}
  318. */
  319. public function getViewTransformers()
  320. {
  321. return $this->viewTransformers;
  322. }
  323. /**
  324. * {@inheritdoc}
  325. */
  326. public function getModelTransformers()
  327. {
  328. return $this->modelTransformers;
  329. }
  330. /**
  331. * {@inheritdoc}
  332. */
  333. public function getDataMapper()
  334. {
  335. return $this->dataMapper;
  336. }
  337. /**
  338. * {@inheritdoc}
  339. */
  340. public function getRequired()
  341. {
  342. return $this->required;
  343. }
  344. /**
  345. * {@inheritdoc}
  346. */
  347. public function getDisabled()
  348. {
  349. return $this->disabled;
  350. }
  351. /**
  352. * {@inheritdoc}
  353. */
  354. public function getErrorBubbling()
  355. {
  356. return $this->errorBubbling;
  357. }
  358. /**
  359. * {@inheritdoc}
  360. */
  361. public function getEmptyData()
  362. {
  363. return $this->emptyData;
  364. }
  365. /**
  366. * {@inheritdoc}
  367. */
  368. public function getAttributes()
  369. {
  370. return $this->attributes;
  371. }
  372. /**
  373. * {@inheritdoc}
  374. */
  375. public function hasAttribute($name)
  376. {
  377. return array_key_exists($name, $this->attributes);
  378. }
  379. /**
  380. * {@inheritdoc}
  381. */
  382. public function getAttribute($name, $default = null)
  383. {
  384. return array_key_exists($name, $this->attributes) ? $this->attributes[$name] : $default;
  385. }
  386. /**
  387. * {@inheritdoc}
  388. */
  389. public function getData()
  390. {
  391. return $this->data;
  392. }
  393. /**
  394. * {@inheritdoc}
  395. */
  396. public function getDataClass()
  397. {
  398. return $this->dataClass;
  399. }
  400. /**
  401. * {@inheritdoc}
  402. */
  403. public function getDataLocked()
  404. {
  405. return $this->dataLocked;
  406. }
  407. /**
  408. * {@inheritdoc}
  409. */
  410. public function getFormFactory()
  411. {
  412. return $this->formFactory;
  413. }
  414. /**
  415. * {@inheritdoc}
  416. */
  417. public function getAction()
  418. {
  419. return $this->action;
  420. }
  421. /**
  422. * {@inheritdoc}
  423. */
  424. public function getMethod()
  425. {
  426. return $this->method;
  427. }
  428. /**
  429. * {@inheritdoc}
  430. */
  431. public function getRequestHandler()
  432. {
  433. if (null === $this->requestHandler) {
  434. if (null === self::$nativeRequestHandler) {
  435. self::$nativeRequestHandler = new NativeRequestHandler();
  436. }
  437. $this->requestHandler = self::$nativeRequestHandler;
  438. }
  439. return $this->requestHandler;
  440. }
  441. /**
  442. * {@inheritdoc}
  443. */
  444. public function getAutoInitialize()
  445. {
  446. return $this->autoInitialize;
  447. }
  448. /**
  449. * {@inheritdoc}
  450. */
  451. public function getOptions()
  452. {
  453. return $this->options;
  454. }
  455. /**
  456. * {@inheritdoc}
  457. */
  458. public function hasOption($name)
  459. {
  460. return array_key_exists($name, $this->options);
  461. }
  462. /**
  463. * {@inheritdoc}
  464. */
  465. public function getOption($name, $default = null)
  466. {
  467. return array_key_exists($name, $this->options) ? $this->options[$name] : $default;
  468. }
  469. /**
  470. * {@inheritdoc}
  471. */
  472. public function setAttribute($name, $value)
  473. {
  474. if ($this->locked) {
  475. throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
  476. }
  477. $this->attributes[$name] = $value;
  478. return $this;
  479. }
  480. /**
  481. * {@inheritdoc}
  482. */
  483. public function setAttributes(array $attributes)
  484. {
  485. if ($this->locked) {
  486. throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
  487. }
  488. $this->attributes = $attributes;
  489. return $this;
  490. }
  491. /**
  492. * {@inheritdoc}
  493. */
  494. public function setDataMapper(DataMapperInterface $dataMapper = null)
  495. {
  496. if ($this->locked) {
  497. throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
  498. }
  499. $this->dataMapper = $dataMapper;
  500. return $this;
  501. }
  502. /**
  503. * {@inheritdoc}
  504. */
  505. public function setDisabled($disabled)
  506. {
  507. if ($this->locked) {
  508. throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
  509. }
  510. $this->disabled = (bool) $disabled;
  511. return $this;
  512. }
  513. /**
  514. * {@inheritdoc}
  515. */
  516. public function setEmptyData($emptyData)
  517. {
  518. if ($this->locked) {
  519. throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
  520. }
  521. $this->emptyData = $emptyData;
  522. return $this;
  523. }
  524. /**
  525. * {@inheritdoc}
  526. */
  527. public function setErrorBubbling($errorBubbling)
  528. {
  529. if ($this->locked) {
  530. throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
  531. }
  532. $this->errorBubbling = null === $errorBubbling ? null : (bool) $errorBubbling;
  533. return $this;
  534. }
  535. /**
  536. * {@inheritdoc}
  537. */
  538. public function setRequired($required)
  539. {
  540. if ($this->locked) {
  541. throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
  542. }
  543. $this->required = (bool) $required;
  544. return $this;
  545. }
  546. /**
  547. * {@inheritdoc}
  548. */
  549. public function setPropertyPath($propertyPath)
  550. {
  551. if ($this->locked) {
  552. throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
  553. }
  554. if (null !== $propertyPath && !$propertyPath instanceof PropertyPathInterface) {
  555. $propertyPath = new PropertyPath($propertyPath);
  556. }
  557. $this->propertyPath = $propertyPath;
  558. return $this;
  559. }
  560. /**
  561. * {@inheritdoc}
  562. */
  563. public function setMapped($mapped)
  564. {
  565. if ($this->locked) {
  566. throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
  567. }
  568. $this->mapped = $mapped;
  569. return $this;
  570. }
  571. /**
  572. * {@inheritdoc}
  573. */
  574. public function setByReference($byReference)
  575. {
  576. if ($this->locked) {
  577. throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
  578. }
  579. $this->byReference = $byReference;
  580. return $this;
  581. }
  582. /**
  583. * {@inheritdoc}
  584. */
  585. public function setInheritData($inheritData)
  586. {
  587. if ($this->locked) {
  588. throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
  589. }
  590. $this->inheritData = $inheritData;
  591. return $this;
  592. }
  593. /**
  594. * Alias of {@link setInheritData()}.
  595. *
  596. * @param bool $inheritData Whether the form should inherit its parent's data
  597. *
  598. * @deprecated since version 2.3, to be removed in 3.0.
  599. * Use {@link setInheritData()} instead.
  600. */
  601. public function setVirtual($inheritData)
  602. {
  603. @trigger_error('The '.__METHOD__.' method is deprecated since Symfony 2.3 and will be removed in 3.0. Use the FormConfigBuilder::setInheritData() method instead.', E_USER_DEPRECATED);
  604. $this->setInheritData($inheritData);
  605. }
  606. /**
  607. * {@inheritdoc}
  608. */
  609. public function setCompound($compound)
  610. {
  611. if ($this->locked) {
  612. throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
  613. }
  614. $this->compound = $compound;
  615. return $this;
  616. }
  617. /**
  618. * {@inheritdoc}
  619. */
  620. public function setType(ResolvedFormTypeInterface $type)
  621. {
  622. if ($this->locked) {
  623. throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
  624. }
  625. $this->type = $type;
  626. return $this;
  627. }
  628. /**
  629. * {@inheritdoc}
  630. */
  631. public function setData($data)
  632. {
  633. if ($this->locked) {
  634. throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
  635. }
  636. $this->data = $data;
  637. return $this;
  638. }
  639. /**
  640. * {@inheritdoc}
  641. */
  642. public function setDataLocked($locked)
  643. {
  644. if ($this->locked) {
  645. throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
  646. }
  647. $this->dataLocked = $locked;
  648. return $this;
  649. }
  650. /**
  651. * {@inheritdoc}
  652. */
  653. public function setFormFactory(FormFactoryInterface $formFactory)
  654. {
  655. if ($this->locked) {
  656. throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
  657. }
  658. $this->formFactory = $formFactory;
  659. return $this;
  660. }
  661. /**
  662. * {@inheritdoc}
  663. */
  664. public function setAction($action)
  665. {
  666. if ($this->locked) {
  667. throw new BadMethodCallException('The config builder cannot be modified anymore.');
  668. }
  669. $this->action = $action;
  670. return $this;
  671. }
  672. /**
  673. * {@inheritdoc}
  674. */
  675. public function setMethod($method)
  676. {
  677. if ($this->locked) {
  678. throw new BadMethodCallException('The config builder cannot be modified anymore.');
  679. }
  680. $upperCaseMethod = strtoupper($method);
  681. if (!\in_array($upperCaseMethod, self::$allowedMethods)) {
  682. throw new InvalidArgumentException(sprintf('The form method is "%s", but should be one of "%s".', $method, implode('", "', self::$allowedMethods)));
  683. }
  684. $this->method = $upperCaseMethod;
  685. return $this;
  686. }
  687. /**
  688. * {@inheritdoc}
  689. */
  690. public function setRequestHandler(RequestHandlerInterface $requestHandler)
  691. {
  692. if ($this->locked) {
  693. throw new BadMethodCallException('The config builder cannot be modified anymore.');
  694. }
  695. $this->requestHandler = $requestHandler;
  696. return $this;
  697. }
  698. /**
  699. * {@inheritdoc}
  700. */
  701. public function setAutoInitialize($initialize)
  702. {
  703. if ($this->locked) {
  704. throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
  705. }
  706. $this->autoInitialize = (bool) $initialize;
  707. return $this;
  708. }
  709. /**
  710. * {@inheritdoc}
  711. */
  712. public function getFormConfig()
  713. {
  714. if ($this->locked) {
  715. throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
  716. }
  717. // This method should be idempotent, so clone the builder
  718. $config = clone $this;
  719. $config->locked = true;
  720. return $config;
  721. }
  722. /**
  723. * Validates whether the given variable is a valid form name.
  724. *
  725. * @param string|int $name The tested form name
  726. *
  727. * @throws UnexpectedTypeException if the name is not a string or an integer
  728. * @throws InvalidArgumentException if the name contains invalid characters
  729. */
  730. public static function validateName($name)
  731. {
  732. if (null !== $name && !\is_string($name) && !\is_int($name)) {
  733. throw new UnexpectedTypeException($name, 'string, integer or null');
  734. }
  735. if (!self::isValidName($name)) {
  736. throw new InvalidArgumentException(sprintf('The name "%s" contains illegal characters. Names should start with a letter, digit or underscore and only contain letters, digits, numbers, underscores ("_"), hyphens ("-") and colons (":").', $name));
  737. }
  738. }
  739. /**
  740. * Returns whether the given variable contains a valid form name.
  741. *
  742. * A name is accepted if it
  743. *
  744. * * is empty
  745. * * starts with a letter, digit or underscore
  746. * * contains only letters, digits, numbers, underscores ("_"),
  747. * hyphens ("-") and colons (":")
  748. *
  749. * @param string $name The tested form name
  750. *
  751. * @return bool Whether the name is valid
  752. */
  753. public static function isValidName($name)
  754. {
  755. return '' === $name || null === $name || preg_match('/^[a-zA-Z0-9_][a-zA-Z0-9_\-:]*$/D', $name);
  756. }
  757. }