hoshomoh / csvutils Goto Github PK
View Code? Open in Web Editor NEWA PHP CSV Validator and Conversion Library
Home Page: https://packagist.org/packages/oshomo/csv-utils
License: Apache License 2.0
A PHP CSV Validator and Conversion Library
Home Page: https://packagist.org/packages/oshomo/csv-utils
License: Apache License 2.0
Hey there, I hope you are well.
First of all, thanks to everyone who has contributed with this great tool.
I have a question to ask you, because maybe I am not implementing a custom rule in the correct way in the case of using parameters.
I followed the documentation, but there is no example with parameters, so I looked for a class that had them, like Between and I saw that it uses the allowedParameters() method.
Unfortunately it shows me these "Notice" messages:
Notice: Undefined offset: 0 in myproject/.../StringRule.php on line 34.
Notice: Undefined offset: 1 in myproject/.../StringRule.php on line 34.
Notice: Undefined offset: 0 in myproject/.../StringRule.php on line 34.
Notice: Undefined offset: 1 in myproject/.../StringRule.php on line 34.
And the reason is because $parameters is an empty array.
Here it is my StringRule class and when it is called from my script. I don't have a framework installed, it is an isolated script.
// String Rule
<?php
namespace Scripts\Import\Utils;
use Oshomo\CsvUtils\Contracts\ParameterizedRuleInterface;
use Oshomo\CsvUtils\Contracts\ValidationRuleInterface;
require_once __DIR__ . '/../../../vendor/autoload.php';
class StringRule implements ValidationRuleInterface, ParameterizedRuleInterface
{
/**
* Should return an array of the allowed parameters.
* See the between rule. Tha allowed parameters should be
* tokenized string e.g :min, :max, :first, :last etc.
*/
public function allowedParameters(): array
{
return [':min', ':max'];
}
/**
* 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 array $parameters
* @return bool
*/
public function passes($value, array $parameters): bool
{
$stringLength = strlen($value);
[$min, $max] = $parameters; // <------- NOTICE IS HERE
return $stringLength < (int) $min || $stringLength > (int) $max;
}
/**
* 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.
*/
public function message(): string
{
return "The :attribute value :value must be a string between :min - :max characters on line :line.";
}
}
// Script
$validator = new Validator('some/valid/file_path', ",", [
'Password' => ["string:8,20", new StringRule],
'State' => ["state", new StateRule]
]);
The StateRule class works perfectly, but this one has no parameters.
Thank you very much in advance for your help.
Hi, I'm using this library and it little bit blocking me to upgrade to PHP 8, because latest stable release has unsatisfying php version condition. But in master it seems like right condition to be able used in PHP 8 platform. So can you release patch version with change?
@hoshomoh I think it would be a good idea to create a git pre-commit hook file to check coding standard and code coverage before you push to the repo. So people who are contributing with this repository could know that they need to check/fix some stuff to pass some of the required checks before merging.
WDYT?
FormatsMessages::makeReplacements
accepts $value
as mixed
data type, but ::replaceValuePlaceholder
accepts $value
only as string
, so when value comes as NULL
, then it throw error, cause strict_types are required.
@hoshomoh Hi there, what do you think if we start supporting php7+?
My suggestions are to have the master branch using php7+ features(by default if you installed this package), since there are so many people/tools using the latest version of the language. We can keep the current code(without types) as antoher branch. So if anyone out there is still using php5.6, they could be able to use a different branch.
By doing that we can increase the php required version in composer.json.
Hi,
Great library, I was painfully doing validations in a much uglier way before I stumbled upon this.
One thing is missing, I did not find an easy way to retrieve line number in errors data. Pointing the line number when errors occur is IMHO necessary to provide usable feedback to the user.
I am getting a deprecation error when trying to construct the validator
Looking the validator has a require parameter after an optional:
CSVUtils/src/Validator/Validator.php
Line 117 in 34afb02
which is now deprecated according to:
https://php.watch/versions/8.0/deprecate-required-param-after-optional
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.