/** * Converts data from CSV * * @param string $data The data to parse * @param boolean $hasHeaders Whether the CSV has headers * * @return mixed */ public static function fromCSV($data, $hasHeaders = false) { $data = trim($data); // Explodes rows $data = static::explodeWith($data, array(PHP_EOL, "\r", "\n")); $data = array_map(function ($row) { return Parse::explodeWith($row, array(";", "\t", ",")); }, $data); // Get headers $headers = $hasHeaders ? $data[0] : array_keys($data[0]); if ($hasHeaders) { array_shift($data); } // Parse the columns in each row foreach ($data as $row => $columns) { foreach ($columns as $columnNumber => $column) { $array[$row][$headers[$columnNumber]] = $column; } } return $array; }
/** * Transform subject to String on toString * * @return string */ public function __toString() { return Parse::toString($this->subject); }
public function testCanParseCSVWithHeaders($value = '') { $array = Parse::fromCSV('foo;bar;bis' . PHP_EOL . "bar\tfoo\tter", true); $results = [['foo' => 'bar', 'bar' => 'foo', 'bis' => 'ter']]; $this->assertEquals($results, $array); }
/** * Execute the Request against the API * * @param boolean $parse * * @return array */ protected function execute($parse = true) { $user = $this->user; $consumer = $this->consumer; $parameters = $this->parameters; return $this->app['cache']->remember($parse . $this->hash, $this->getCacheLifetime(), function () use($parse, $user, $consumer, $parameters) { // Create OAuth request $request = new TmhOAuth(array('consumer_key' => $consumer->getKey(), 'consumer_secret' => $consumer->getSecret(), 'host' => Flickering::API_URL, 'use_ssl' => true, 'user_token' => $user->getKey(), 'user_secret' => $user->getSecret())); $request->request('GET', $request->url(''), $parameters); $response = $request->response['response']; // Return raw if requested if (!$parse) { return $response; } // Parse resulting content switch (array_get($parameters, 'format')) { case 'json': return Parse::fromJSON($response); default: return Parse::fromXML($response); } }); }