예제 #1
0
파일: Controller.php 프로젝트: nbeletsky/nx
 /**
  *  Sanitizes data according to the sanitizers defined in $this->_sanitizers.
  *  If data is an object, the object's sanitize() method will be called.
  *
  *  @param array $data          The data to be sanitized.
  *  @access public
  *  @return array
  */
 public function sanitize($data)
 {
     $sanitized = array();
     foreach ($data as $key => $val) {
         if (!is_array($val)) {
             if (isset($this->_sanitizers[$key])) {
                 $sanitized[$key] = Data::sanitize($val, $this->_sanitizers[$key]);
             }
         } else {
             foreach ($val as $id => $obj) {
                 $sanitized[$key][$id] = $obj->sanitize();
             }
         }
     }
     return $sanitized;
 }
예제 #2
0
파일: DataTest.php 프로젝트: nbeletsky/nx
 public function test_Sanitize_ReturnsTypecastedData()
 {
     // Bool to bool
     $dirty = true;
     $type = 'b';
     $check = true;
     $clean = Data::sanitize($dirty, $type);
     $this->assertEquals($clean, $check, 'Boolean `true` was not sanitized to
         boolean `true`.');
     // Bool to float
     $dirty = true;
     $type = 'f';
     $check = 1;
     $clean = Data::sanitize($dirty, $type);
     $this->assertEquals($clean, $check, 'Boolean `true` was not sanitized to
         float `1`.');
     // Bool to int
     $dirty = false;
     $type = 'i';
     $check = 0;
     $clean = Data::sanitize($dirty, $type);
     $this->assertEquals($clean, $check, 'Boolean `false` was not sanitized
         to integer `0`.');
     // Bool to string
     $dirty = true;
     $type = 's';
     $check = '1';
     $clean = Data::sanitize($dirty, $type);
     $this->assertEquals($clean, $check, 'Boolean `true` was not sanitized to
         string `1`.');
     // Bool to string
     $dirty = false;
     $type = 's';
     $check = '';
     $clean = Data::sanitize($dirty, $type);
     $this->assertEquals($clean, $check, 'Boolean `false` was not sanitized
         to empty string ``.');
     // Float to bool
     $dirty = 1.234;
     $type = 'b';
     $check = true;
     $clean = Data::sanitize($dirty, $type);
     $this->assertEquals($clean, $check, 'Float `1.234` was not sanitized to
         boolean `true`.');
     // Float to float
     $dirty = 1.234;
     $type = 'f';
     $check = 1.234;
     $clean = Data::sanitize($dirty, $type);
     $this->assertEquals($clean, $check, 'Float `1.234` was not sanitized to
         float `1.234`.');
     // Float to int
     $dirty = 1.928;
     $type = 'i';
     $check = 1928;
     $clean = Data::sanitize($dirty, $type);
     $this->assertEquals($clean, $check, 'Float `1.928` was not sanitized to
         integer `1928`.');
     // Float to string
     $dirty = 1.928;
     $type = 's';
     $check = '1.928';
     $clean = Data::sanitize($dirty, $type);
     $this->assertEquals($clean, $check, 'Float `1.928` was not sanitized to
         string `1.928`.');
     // Int to bool
     $dirty = 0;
     $type = 'b';
     $check = false;
     $clean = Data::sanitize($dirty, $type);
     $this->assertEquals($clean, $check, 'Integer `0` was not sanitized to
         boolean `false`.');
     // Int to bool
     $dirty = 1;
     $type = 'b';
     $check = true;
     $clean = Data::sanitize($dirty, $type);
     $this->assertEquals($clean, $check, 'Integer `1` was not sanitized to
         boolean `true`.');
     // Int to float
     $dirty = 2;
     $type = 'f';
     $check = 2;
     $clean = Data::sanitize($dirty, $type);
     $this->assertEquals($clean, $check, 'Integer `2` was not sanitized to
         float `2`.');
     // Int to int
     $dirty = 3;
     $type = 'i';
     $check = 3;
     $clean = Data::sanitize($dirty, $type);
     $this->assertEquals($clean, $check, 'Integer `3` was not sanitized to
         integer `3`.');
     // Int to string
     $dirty = 3;
     $type = 's';
     $check = '3';
     $clean = Data::sanitize($dirty, $type);
     $this->assertEquals($clean, $check, 'Integer `3` was not sanitized to
         string `3`.');
     // String to bool
     $dirty = 'true';
     $type = 'b';
     $check = false;
     $clean = Data::sanitize($dirty, $type);
     $this->assertEquals($clean, $check, 'String `true` was not sanitized to
         boolean `false`.');
     // String to bool
     $dirty = '1';
     $type = 'b';
     $check = true;
     $clean = Data::sanitize($dirty, $type);
     $this->assertEquals($clean, $check, 'String `1` was not sanitized to
         boolean `true`.');
     // String to float
     $dirty = "1.928";
     $type = 'f';
     $check = 1.928;
     $clean = Data::sanitize($dirty, $type);
     $this->assertEquals($clean, $check, 'String `1.928` was not sanitized to
         float `1.928`.');
     // String to int
     $dirty = "1";
     $type = 'i';
     $check = 1;
     $clean = Data::sanitize($dirty, $type);
     $this->assertEquals($clean, $check, 'String `1` was not sanitized to
         integer `1`.');
     // String to int
     $dirty = "1.928";
     $type = 'i';
     $check = 1.928;
     $clean = Data::sanitize($dirty, $type);
     $this->assertNotEquals($clean, $check, 'String `1.928` sanitized as an
         integer should not return `1.928`.');
     // String to string
     $dirty = 'test';
     $type = 's';
     $check = 'test';
     $clean = Data::sanitize($dirty, $type);
     $this->assertEquals($clean, $check, 'String `test` was not sanitized to
         string `test`.');
 }
예제 #3
0
파일: Model.php 프로젝트: nbeletsky/nx
 /**
  *  Sanitizes an object's properties in accordance with the sanitizers
  *  defined in $this->_sanitzers.
  *
  *  @access public
  *  @return object
  */
 public function sanitize()
 {
     foreach ($this->_sanitizers as $property => $type) {
         $this->{$property} = Data::sanitize($this->{$property}, $type);
     }
     return $this;
 }