Esempio n. 1
0
 /**
  * @param $name
  * @param null $default
  * @return bool|float|int|null
  */
 public static function getEnv($name, $default = null)
 {
     $val = getenv($name);
     if ($val === false) {
         return $default;
     }
     return StringUtil::guessAndCastValue($val);
 }
Esempio n. 2
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     //get formatter
     $formatter = $this->getHelper('formatter');
     $file = $input->getArgument('csv-file');
     //check if file exists
     if (!file_exists($file)) {
         $output->writeln($formatter->formatBlock(['[Error]', 'File ' . $file . ' not found.'], 'error', true));
         return;
     }
     //get options
     $delimiter = $input->getOption('delimiter');
     $enclosure = $input->getOption('enclosure');
     $escape = $input->getOption('escape');
     //get content and fix encoding
     $content = file_get_contents($file);
     //remove windows line breaks
     $content = str_replace("\r", '', $content);
     //split lines
     $lines = explode("\n", $content);
     //output
     $columns = [];
     //loop over all lines and put them into columns
     foreach ($lines as $line) {
         $csv = str_getcsv($line, $delimiter, $enclosure, $escape);
         foreach ($csv as $key => $field) {
             $field = trim($field);
             $columns[$key][] = $field;
         }
     }
     $rows = [];
     //loop over columns
     foreach ($columns as $columnKey => $column) {
         //row id
         $rowId = 0;
         //get max strlen
         $max = StringUtil::maxStrlen($column) + 1;
         //make columns equal length
         foreach ($column as $fieldKey => $field) {
             $rows[$rowId][$columnKey] = ' ' . str_pad($field, $max, ' ', STR_PAD_RIGHT);
             $rowId++;
         }
     }
     $table = '';
     //loop over rows
     foreach ($rows as $row) {
         $line = '|' . implode(' | ', $row) . '|';
         if ($table == '') {
             $headers = [];
             foreach ($row as $field) {
                 $headers[] = str_repeat('-', strlen($field));
             }
             $line .= "\n|" . implode(' | ', $headers) . '|';
         }
         $table .= $line . "\n";
     }
     echo Encoding::toUTF8($table);
 }
Esempio n. 3
0
 /**
  * @param string $expected
  * @param int    $input
  *
  * @dataProvider formatBytesProvider
  */
 public function testFormatBytes($expected, $input)
 {
     $this->assertEquals($expected, StringUtil::formatBytes($input));
 }