smartemailing / types Goto Github PK
View Code? Open in Web Editor NEWMissing data types for PHP. Highly extendable.
License: Other
Missing data types for PHP. Highly extendable.
License: Other
dopravasladek{@seznam.cz
should not be valid.BC break: posibly yes
New feature: Yes
Problem:
$data = [
'ids' => [
'a' => 1,
'b' => 2,
'c' => 'failed',
// ...
]
];
Arrays::extractIntArray($data, 'ids');
Produces error like:
Problem at key ids: Expected int, got string (failed)
so we know there is problem at key ids
but we dont know where exactly in ids
array.
It whould be nice if the massege will be formatted like: Problem at key ids[c]: Expected int, got string (failed)
.
Simular problem is with array extractable object:
$data = [
'address' => [
'street_and_number' => 'My streer 21',
'town' => 'Prague',
'zip_code' => '12552',
'country' => 'XX'
]
];
Address::extract($data, 'address');
Produces error like: Problem at key address: Problem at key country: XX [string] is not a valid value for SmartEmailing\Types\CountryCode, accepted values: ...
It would be more readable like: Problem at key address[country]: XX [string] is not a valid value for SmartEmailing\Types\CountryCode, accepted values: ...
I think that float values that can be converted to int without any rounding should not throw exceptions
IntType::from(1.0000000000);//currently throws exception, expected return value of (int) 1
IntType::from('5.00');//currently throws exception, expected return value of (int) 5
What i did:
$data = [
'quantity' => Quantity::from(5)
];
debug(json_encode($data));
Expected result:
{
"quantity": 5
}
Actual result:
{
"quantity": {}
}
Allow compare two object. Same API as Enum equal.
$email1 = Emailaddress::from('[email protected]');
$email2 = Emailaddress::from('[email protected]');
assert($email1->equal($email2));
@slischka what you think about adding jschaedl/iban-validation
dependency using in Iban type from required section in composer to suggestion?
Constructor in Iban type will be like below:
public function __construct(
string $value
)
{
if (!\class_exists(\Iban\Validation\Iban::class) || !\class_exists(\Iban\Validation\Iban::class)) {
throw new \Exception('For using ' . self::class . ' type, please require "jschaedl/iban-validation" in your project composer.')
}
$this->iban = new \Iban\Validation\Iban($value);
$validator = new Validator();
...
Extract values should be also possile from objects implementing ArrayAccess.
BC break: No
New feature: Yes
If you implement own collection or you want to extract values maybe from \Nette\Database\IRow. First you have to serialize object to array then you can extract values from array.
Example:
/** @var Nette\Database\Table\ActiveRow $databaseRow */
$databaseRow = $this->repository->fetch();
$array = iterator_to_array($databaseRow); // or $databaseRow->toArray()
$id = PrimitiveTypes::extractInt($array, 'id');
should be simplified to:
/** @var Nette\Database\Table\ActiveRow $databaseRow */
$databaseRow = $this->repository->fetch();
$id = PrimitiveTypes::extractInt($databaseRow, 'id');
So exract method should look like:
final public static function extractInt(array|ArrayAccess $data, string $key): int; // PHP 8.0 syntax
It should to possible to extract value from array with int keys.
BC break: No
New feture: Yes
PrimitiveTypes::extractIntOrNull($data, 1);
implementation:
final public static function extractIntOrNull(array $data, string|int $key): ?int; // PHP 8.0 syntax
Would it be possible to change consistence dependency to ^1.0 || ^2.0 and tag new version?
I tried to use library within new project and get dependency error...
PrimitiveTypes nevytáhne z '0'
nulu.
PrimitiveTypes::getInt("0")
Vyhodí chybu
Bug ?
$value = '0'
if (\is_int($value)) { // false '0' není int
return $value;
}
if (\is_string($value)) { // true
$value = \ltrim($value, '0'); // $value = ''
}
if ((string) (int) $value === $value) { // false '0' !== ''
return (int) $value;
}
throw InvalidTypeException::typeError('int', $value); //vyhodí vyjímku
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.