Ejemplo n.º 1
0
 public function testCompact()
 {
     $object = array(0, 1, false, 2, '', 3);
     $return = array(1, 2, 3);
     $result = __::compact($object);
     $this->assertEquals($return, $result);
 }
Ejemplo n.º 2
0
 public function testCompact()
 {
     // Arrange
     $a = [0, 1, false, 2, '', 3];
     // Act
     $x = __::compact($a);
     // Assert
     $this->assertEquals([1, 2, 3], $x);
 }
Ejemplo n.º 3
0
 public function testCompact()
 {
     $vals = array(0, 1, false, 2, false, 3);
     // from js
     $this->assertEquals(3, count(__::compact($vals)), 'can trim out all falsy values');
     // extra
     $this->assertEquals(array(1, 2, 3), __::compact($vals), 'can remove all falsy values');
     // docs
     $this->assertEquals(array(true, 'a', 1), __::compact(array(false, true, 'a', 0, 1, '')));
 }
Ejemplo n.º 4
0
 public function testCompact()
 {
     $vals = array(0, 1, false, 2, false, 3);
     // from js
     $this->assertEquals(3, count(__::compact($vals)), 'can trim out all falsy values');
     $func = function () {
         return count(__(func_get_args())->compact());
     };
     $result = $func(0, 1, false, 2, false, 3);
     $this->assertEquals(3, $result, 'works on arguments');
     // extra
     $this->assertEquals(array(1, 2, 3), __::compact($vals), 'can remove all falsy values');
     // docs
     $this->assertEquals(array(true, 'a', 1), __::compact(array(false, true, 'a', 0, 1, '')));
 }
Ejemplo n.º 5
0
 /**
   @param $file String filename
   @param $csv String csv data
   @param $merge Bool merge entries rather than overwrite
   @return $queries [String]
   Iff $merge is true, we shall use REPLACE INTO queries rather than DELETE followed by INSERT INTO.
 */
 public function mkQueries($file, $csv, $merge)
 {
     array_push(self::$log, 'Importer::mkQueries for ' . $file . '…');
     $desc = self::descFile($file);
     if ($desc === null) {
         array_push(self::$log, "Importer::mkQueries produced no queries for {$file}.");
         return array();
     }
     //Parsing the csv:
     $csv = self::parse($csv);
     //Finding cols to fill:
     $cols = array();
     // Index -> colName || null
     $colMap = $desc['colMapping'];
     foreach ($csv['headline'] as $h) {
         if (array_key_exists($h, $colMap)) {
             array_push($cols, $colMap[$h]);
         } else {
             array_push(self::$log, "Unknown column for file {$file}: '{$h}'");
             array_push($cols, null);
             //Placeholder column to ignore field
         }
     }
     array_push(self::$log, 'Columns to import: ' . implode($cols, ', '));
     //Tuples to insert/replace:
     $tuples = array();
     $db = Config::getConnection();
     $strMapping = self::descTable($desc['table']);
     foreach ($csv['data'] as $row) {
         $ts = array();
         for ($i = 0; $i < count($cols); $i++) {
             $c = $cols[$i];
             if ($c !== null) {
                 if (count($row) > $i) {
                     $r = $db->escape_string($row[$i]);
                     if (!array_key_exists($c, $strMapping)) {
                         array_push(self::$log, "Missing index '{$c}' in strMapping for table '" . $desc['table'] . "'");
                     } else {
                         if ($strMapping[$c]) {
                             $r = "'{$r}'";
                             //Encapsulate Strings
                         }
                     }
                     if ($r === '') {
                         $r = 'NULL';
                     }
                     //Empty Strings -> NULL
                     array_push($ts, $r);
                 } else {
                     $i = count($cols);
                     $ts = array();
                 }
             }
         }
         if (count($ts) > 0) {
             array_push($tuples, '(' . implode($ts, ',') . ')');
         }
     }
     //Queries for the given $file and $csv:
     $queries = $merge ? array() : array('DELETE FROM ' . $desc['table']);
     $q = ($merge ? 'REPLACE' : 'INSERT') . ' INTO ' . $desc['table'] . ' (' . implode(__::compact($cols), ',') . ') VALUES ' . implode($tuples, ',');
     array_push($queries, $q);
     return $queries;
 }