11<?php
2+ /**
3+ * SimpleThings FormExtraBundle
4+ *
5+ * LICENSE
6+ *
7+ * This source file is subject to the new BSD license that is bundled
8+ * with this package in the file LICENSE.txt.
9+ * If you did not receive a copy of the license and are unable to
10+ * obtain it through the world-wide-web, please send an email
11+ * to kontakt@beberlei.de so I can send you a copy immediately.
12+ */
213
314namespace SimpleThings \FormExtraBundle \Service ;
415
5- use Symfony \Component \Validator \ValidatorInterface ;
16+ use Symfony \Component \Validator \Mapping \ ClassMetadataFactoryInterface ;
617use Symfony \Component \Validator \Constraint ;
718use Symfony \Component \Translation \Translator ;
819
1122 */
1223class JsValidationConstraintsGenerator
1324{
14-
1525 /**
16- *
17- * @var ValidatorInterface
26+ * @var ClassMetadataFactoryInterface
1827 */
19- protected $ validator ;
28+ protected $ metadataFactory ;
2029
2130 /**
22- *
2331 * @var Translator
2432 */
2533 protected $ translator ;
2634
2735 /**
28- *
2936 * @var string
3037 */
3138 protected $ defaultLocale ;
3239
3340 /**
34- *
35- * @param ValidatorInterface $validator
41+ * @param metadataFactoryInterface $metadataFactory
3642 */
37- public function __construct (ValidatorInterface $ validator , Translator $ translator , $ defaultLocale )
43+ public function __construct (ClassMetadataFactoryInterface $ metadataFactory , Translator $ translator = null , $ defaultLocale = null )
3844 {
39- $ this ->validator = $ validator ;
45+ $ this ->metadataFactory = $ metadataFactory ;
4046 $ this ->translator = $ translator ;
4147 $ this ->defaultLocale = $ defaultLocale ;
4248 }
4349
4450 /**
45- *
46- * @param array $objects
51+ * @param array $classNames
4752 * @return string
4853 */
49- public function generate (array $ objects )
54+ public function generate (array $ classNames )
5055 {
51-
52- $ metadataFactory = $ this ->validator ->getMetadataFactory ();
5356 $ data = array ();
57+ foreach ($ classNames as $ className ) {
58+ $ data [$ className ] = $ this ->generateClass ($ className );
59+ }
60+
61+ return json_encode ($ data );
62+ }
5463
55- foreach ($ objects as $ object ) {
56- $ data [$ object ] = array ();
64+ /**
65+ * Generate array representation of constraints that is exported to JSON.
66+ *
67+ * @todo export class constraints
68+ * @todo filter exported contraints?
69+ * @param string $className
70+ * @return array
71+ */
72+ public function generateClass ($ className )
73+ {
74+ $ data = array ();
5775
58- $ metadata = $ metadataFactory ->getClassMetadata ($ object );
59- $ properties = $ metadata ->getConstrainedProperties ();
76+ $ metadata = $ this -> metadataFactory ->getClassMetadata ($ className );
77+ $ properties = $ metadata ->getConstrainedProperties ();
6078
61- foreach ($ properties as $ property ) {
62- $ data[ $ object ] [$ property ] = array ();
63- $ constraintsList = $ metadata ->getMemberMetadatas ($ property );
64- foreach ($ constraintsList as $ constraints ) {
65- foreach ($ constraints ->constraints as $ constraint ) {
66- $ const = clone $ constraint ;
67- $ const -> message = $ this ->translator -> trans ( $ const -> message , array (), ' validators ' , $ this -> defaultLocale );
68- $ data [ $ object ][ $ property ][ $ this ->getConstraintName ($ const)] = $ const ;
79+ foreach ($ properties as $ property ) {
80+ $ data [$ property ] = array ();
81+ $ constraintsList = $ metadata ->getMemberMetadatas ($ property );
82+ foreach ($ constraintsList as $ constraints ) {
83+ foreach ($ constraints ->constraints as $ constraint ) {
84+ $ const = clone $ constraint ;
85+ if ( $ this ->translator ) {
86+ $ const -> message = $ this ->translator -> trans ($ const-> message , array (), ' validations ' , $ this -> defaultLocale ) ;
6987 }
88+ $ data [$ property ][$ this ->getConstraintName ($ const )] = $ const ;
7089 }
7190 }
7291 }
7392
74- return json_encode ( $ data) ;
93+ return $ data ;
7594 }
7695
7796 /**
78- *
97+ * @todo Only shorten symfony ones and underscore/camlize others?
7998 * @param Constraint $constraint
8099 * @return string
81100 */
@@ -85,5 +104,5 @@ protected function getConstraintName(Constraint $constraint)
85104 $ parts = explode ('\\' , $ class );
86105 return lcfirst (array_pop ($ parts ));
87106 }
88-
89107}
108+
0 commit comments