| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace detox\core; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use detox\dataset\SetContract; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use detox\source\Text; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * Class Words | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @package detox\core | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @property Text text | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 14 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  | class Words | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |     public const MAX_SCORE        = 1; | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |     public const ASTERISKS_MIDDLE = 0.8; | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |     public const ASTERISKS_LEFT   = 0.5; | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |     public const ASTERISKS_RIGHT  = 0.6; | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |     protected $dataSet; | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |     protected $score = 0; | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |     protected $text; | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |      * Words constructor. | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |      * @param SetContract $set | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |      * @param Text $text | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     public function __construct(SetContract $set, Text $text) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $this->dataSet = $set; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 36 |  |  |         $this->text    = $text; | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |      * Finds bad words from *Set and setting score/replace | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |     public function processWords() : void | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |         // to match lower case letters in words set array | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |         $lowerSource = $this->addLowSpaces($this->text->getText()); | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         /** | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |          * @var string $points | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |          * @var array $words | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |          */ | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         foreach ($this->dataSet->getWords() as $points => $words) { | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |             foreach ($words as $word) { | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |                 if (mb_strpos($lowerSource, ' ' . $word . ' ') !== false) { | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |                     $this->score += (float)$points; | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |                 if ($this->score >= self::MAX_SCORE) { | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |                     $this->score = self::MAX_SCORE; | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |                     // we don't need to iterate more with max score | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |                     return; | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |      * Finds bad words with asterisks and setting score/replace | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |     public function processPatterns() : void | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |         $lowerSource = $this->addLowSpaces($this->text->getText()); | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |         if (preg_match('/\s(([\w]+)[\*]+([\w]+))\s/', $lowerSource) === 1) { | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |             $this->score += self::ASTERISKS_MIDDLE; | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |         } else if (preg_match('/\s(([\w]+)[\*]+)/', $lowerSource) === 1) { | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |             $this->score += self::ASTERISKS_RIGHT; | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |         } else if (preg_match('/([\*]+([\w]+))\s/', $lowerSource) === 1) { | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |             $this->score += self::ASTERISKS_LEFT; | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |      * @param SetContract $set | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |     public function setData(SetContract $set) : void | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |         $this->dataSet = $set; | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |      * @return float | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     public function getScore() : float | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 93 |  |  |         return $this->score; | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |      * @param float $score | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |     public function setScore(float $score) : void | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |         $this->score = $score; | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |      * @param string $str | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |      * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     protected function addLowSpaces(string $str) : string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 110 |  |  |         return ' ' . mb_strtolower($str) . ' '; | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |      * Setter for convenient DI with Text object and it's properties | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |      * @param Text $text | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |     public function setText(Text $text) : void | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |         $this->text = $text; | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  |      * @return Text | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     public function getText() : Text | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 127 |  |  |         return $this->text; | 
            
                                                                        
                                                                
            
                                    
            
            
                | 128 |  |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 129 |  |  | } |