11<?php 
22
3- namespace  Tequilla \MongoDB \Util ;
3+ namespace  Tequilla \MongoDB \Write \ Model \ Traits ;
44
55use  Symfony \Component \OptionsResolver \OptionsResolver ;
66use  Tequilla \MongoDB \Exception \InvalidArgumentException ;
7+ use  Tequilla \MongoDB \Util \TypeUtils ;
78use  Symfony \Component \OptionsResolver \Exception \InvalidArgumentException  as  OptionsResolverException ;
89
9- final   class  ValidatorUtils 
10+ trait  UpdateValidationTrait 
1011{
11-     /** 
12-      * @var bool 
13-      */ 
14-     private  static  $ documentValidationEnabled  = false ;
15- 
16-     /** 
17-      * @var OptionsResolver 
18-      */ 
1912    private  static  $ updateResolver ;
2013
21-     /** 
22-      * @param array|object $document 
23-      */ 
24-     public  static  function  ensureValidDocument ($ document )
25-     {
26-         if  (!self ::$ documentValidationEnabled ) {
27-             return ;
28-         }
29- 
30-         $ document  = TypeUtils::ensureArrayRecursive ($ document );
31- 
32-         if  (empty ($ document )) {
33-             throw  new  InvalidArgumentException ('Document cannot be empty ' );
34-         }
35- 
36-         array_walk_recursive ($ document , function ($ value , $ fieldName ) {
37-             StringUtils::ensureValidDocumentFieldName ($ fieldName );
38-         });
39-     }
40- 
41-     public  static  function  ensureValidFilter ($ filter )
42-     {
43-         if  (!is_array ($ filter ) && !is_object ($ filter )) {
44-             throw  new  InvalidArgumentException (
45-                 sprintf (
46-                     '$filter must be an array or an object, %s given ' ,
47-                     TypeUtils::getType ($ filter )
48-                 )
49-             );
50-         }
51-     }
52- 
53-     /** 
54-      * @param array|object $update 
55-      */ 
5614    public  static  function  ensureValidUpdate ($ update )
5715    {
5816        if  (!is_array ($ update ) && !is_object ($ update )) {
@@ -116,20 +74,4 @@ private static function getUpdateResolver()
11674
11775        return  self ::$ updateResolver ;
11876    }
119- 
120-     /** 
121-      * Enables heavy validation, like recursive documents validation etc. 
122-      */ 
123-     public  static  function  enableDocumentValidation ()
124-     {
125-         self ::$ documentValidationEnabled  = true ;
126-     }
127- 
128-     /** 
129-      * Disables heavy validation, like recursive documents validation etc. 
130-      */ 
131-     public  static  function  disableDocumentValidation ()
132-     {
133-         self ::$ documentValidationEnabled  = false ;
134-     }
13577}
0 commit comments