Beispiel #1
0
 public function testAppliesArrayOdFilters()
 {
     $filter1 = new NativeFilter(FILTER_SANITIZE_STRING);
     $filter2 = new NativeFilter(FILTER_SANITIZE_NUMBER_INT);
     $input = 'x11';
     $filtered = $filter1->filter('test', $input);
     $filtered = $filter2->filter('test', $filtered);
     $form = new \Okneloper\Forms\Form();
     $form->add('text', 'test');
     $form->setFilters(['test' => [$filter1, $filter2]]);
     $form->submit(['test' => $input]);
     $this->assertSame($filtered, $form->val('test'));
 }
Beispiel #2
0
 /**
  * Apply filter or array of filters to a form value
  *
  * @param $filter
  * @param $key
  * @param $value
  * @return mixed|string
  * @throws \Exception
  */
 protected function applyFilter($filter, $key, $value)
 {
     if (is_array($filter)) {
         // call the function recursively to apply every filter i the array
         foreach ($filter as $singleFilter) {
             $value = $this->applyFilter($singleFilter, $key, $value);
         }
         return $value;
     } else {
         if (is_object($filter)) {
             if (!$filter instanceof FilterInterface) {
                 throw new \Exception("Filter object should implement FilterInterface.");
             }
         } else {
             // cast to array
             if (!is_array($filter)) {
                 $filter = [$filter];
             }
             // add second argument
             if (!isset($filter[1])) {
                 $filter[1] = null;
             }
             $filter = new NativeFilter($filter[0], $filter[1]);
         }
         return $filter->filter($key, $value);
     }
 }