1. Go to this page and download the library: Download oshomo/csv-utils library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
oshomo / csv-utils example snippets
use Oshomo\CsvUtils\Validator\Validator;
$validator = new Validator(
"some/valid/file_path",
[
"name" => ["ascii_only"],
"uri" => ["url"],
"stars" => ["between:0,5"]
]
);
use Oshomo\CsvUtils\Validator\Validator;
$validator = new Validator(
"some/valid/file_path",
[
'title' => ["ascii_only", "url"]
]
);
if ($validator->fails()) {
// Do something when validation fails
$errors = $validator->errors();
}
use Oshomo\CsvUtils\Validator\Validator;
$validator = new Validator(
"some/valid/file_path",
['title' => ["ascii_only", "url"]],
[
'ascii_only' => 'The :value supplied for :attribute attribute is invalid on line :line of the CSV.',
// This specifies a custom message for a given attribute.
'hotel_link:url' => 'The :attribute must be a valid link. This error occured on line :line of the CSV.',
]
);
use Oshomo\CsvUtils\Validator\Validator;
use Oshomo\CsvUtils\Converter\JsonConverter;
use Oshomo\CsvUtils\Converter\XmlConverter;
$validator = new Validator(
'some/valid/file_path',
[
"stars" => ["between:0,5"],
"name" => ["ascii_only"],
"uri" => ["url"],
]
);
if(!$validator->fails()) {
$validator->write(new JsonConverter());
$validator->write(new XmlConverter("hotel"));
} else {
print_r($validator->errors());
}
use Oshomo\CsvUtils\Validator\Validator;
$validator = new Validator(
'some/valid/file_path',
["name" => ["ascii_only", new UppercaseRule]]
);
use Oshomo\CsvUtils\Contracts\ValidationRuleInterface;
class UppercaseRule implements ValidationRuleInterface
{
/**
* Determines if the validation rule passes. This is where we do the
* actual validation. If the validation passes return true else false
*
* @param mixed $value
* @param $parameters
* @return bool
*/
public function passes($value, array $parameters): bool
{
return strtoupper($value) === $value;
}
/**
* Get the validation error message. Specify the message that should
* be returned if the validation fails. You can make use of the
* :attribute and :value placeholders in the message string
*
* @return string
*/
public function message(): string
{
return "The :attribute value :value must be uppercase on line :line.";
}
}
use Oshomo\CsvUtils\Validator\Validator;
$validator = new Validator(
"some/valid/file_path",
[
"uri" => ["url", function($value, $fail) {
if (strpos($value, "https://") !== 0) {
return $fail('The URL passed must be https i.e it must start with https://');
}
}]
]);
use Oshomo\CsvUtils\Contracts\ConverterHandlerInterface;
class JsonConverter implements ConverterHandlerInterface
{
const FILE_EXTENSION = "json";
/**
* The converted data
*
* @var string
*/
protected $data;
/**
* @return string
*/
public function getExtension(): string
{
return JsonConverter::FILE_EXTENSION;
}
/**
* @param array $data
* @return $this|mixed
*/
public function convert(array $data): ConverterHandlerInterface
{
$this->data = json_encode($data,
JSON_PRETTY_PRINT |
JSON_NUMERIC_CHECK |
JSON_UNESCAPED_SLASHES |
JSON_UNESCAPED_UNICODE
);
return $this;
}
/**
* @param string $filename
* @return bool
*/
public function write(string $filename): bool
{
return (file_put_contents($filename, $this->data)) ? true : false;
}
}
//////////////////////////////////////////////////////
// To use the converter above.
//////////////////////////////////////////////////////
$validator->write(new JsonConverter());
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.