Requires a minimum of PHP 8.1. Works with PHP 8.4.
next()
andprevious()
onIsCollectionType
no longer return a value.- This is because of
\Iterator
requiringnext
to returnvoid
, and it makes sense fornext
andprevious
to function in the same way.
- This is because of
ConversionError
now extends from\ValueError
(previously\RuntimeException
)
-
Added default classes to be able to use some of the traits instantly without having to create an empty new class each time. The classes added are:
\FireMidge\ValueObject\Generic\AnyCollection
\FireMidge\ValueObject\Generic\AnyFloat
\FireMidge\ValueObject\Generic\AnyInteger
\FireMidge\ValueObject\Generic\AnyString
- All of the above classes implement
\JsonSerializable
, andAnyCollection
also implements the\Iterator
interface.
-
Added default
Percentage
andEmail
classes\FireMidge\ValueObject\Generic\Percentage
\FireMidge\ValueObject\Generic\Email
-
New methods on
IsCollectionType
:merge
withMerged
pop
popMultiple
split
shuffle
withReversedOrder
-
jsonSerialize
has been implemented for all types, to aid easy serialisation.- In order for automatic serialisation to happen when
json_encode
is called, the class using these traits has to implement theJsonSerializable
interface. - The new generic classes all implement it already.
- In order for automatic serialisation to happen when
- Added Psalm annotations in the collection type.
- This allows for better IDE-internal type hints for methods like
toArray()
,first()
,last()
etc. - Usage is showcased in
README.md
- This allows for better IDE-internal type hints for methods like
- More specific error messages for adding or subtracting a value from a float or integer type beyond allowed min/max values.
- Option to return
$reasons
from aConversionError
InvalidValue
renders different types better, e.g. non-strings are no longer wrapped in double quotes- Library is tested against PHP 8.4. Worked without changes to the code.
- Upgraded from PhpUnit 9 to 11.5 and upgraded Infection to 29.0.
- Changed all docblock annotations to attributes
- Infection's output is vastly improved, removing the vast majority of false positives. MSI has changed from 97% to 99%, Mutation Code Coverage from 98% to 99% and Covered Code MSI from 98% to 100%.
-
Added new type
IsClassArrayEnumType
, which is a convenience type betweenArrayEnumType
andClassCollectionType
.- It's used for collections of instances which in turn are enum types, i.e. there is a finite list of valid values. It also offers an easy way to create a new collection holding all valid variations of the underlying enum type (
withAll()
).
- It's used for collections of instances which in turn are enum types, i.e. there is a finite list of valid values. It also offers an easy way to create a new collection holding all valid variations of the underlying enum type (
-
Added pointer methods to
IsCollectionType
:current
previous
next
first
last
- This is, by extension, also available to instances using the
IsClassCollectionType
,IsArrayEnumType
,IsClassArrayEnumType
and all other related traits. - Unlike the equivalent built-in PHP methods, these new methods return
null
for non-existing elements instead offalse
.
- Improved
@covers
annotations and re-imported certain traits to improve accuracy of code coverage reports. Added tests for all new functionality as well as addressed all true positives in the Infection log.- MSI improved from 94% to 97% and CCM from 95% to 98%.
IsClassCollectionType::className()
is now astatic
abstract method.- This means all existing classes using this trait will need to change it from
protected
toprotected static
. No further changes should be necessary. - As a static, it can be used for a wider range of useful convenience methods, like the new
fromRawArray
.
- This means all existing classes using this trait will need to change it from
-
IsClassCollectionType
now has afromRawArray
method- This allows creating a new array not from instances of the required class, but from raw values which are then automatically converted into the required class.
- A custom conversion callback can be provided.
- In order to globally (for that particular class) override the conversion of raw values into target class instances, you can override the protected static method
convertFromRaw(mixed $value) : object
.
-
IsCollectionType
now hasfind
andfindIndex
methods.- This allows returning a specific element (or index, respectively) based on a custom callback.
- This means it's no longer needed to convert the class back to an array for the sake of finding a specific element.
-
isEqualTo
andisNotEqualTo
have been added to the following:IsStringEnumType
IsStringType
IsIntType
IsIntEnumType
IsFloatType
- This was done to be consistent with other traits.
ConversionError
has been introduced, which is thrown when attempting to perform a loose comparison with a value that cannot be converted to the target type.
-
The following methods for mathematical operations and comparisons have been added to
IsFloatType
andIsIntType
:add
subtract
isGreaterThan
isGreaterThanOrEqualTo
isLessThan
isLessThanOrEqualTo
-
IsFloatType
now has:fromString
fromStringOrNull
fromNumber
fromNumberOrNull
-
IsIntEnumType
now has:fromString
fromStringOrNull
-
isIntType
now hasfromStringOrNull
(besides the pre-existingfromString
) in order to make it consistent with other traits. -
IsIntEnumType
now implements the magic__toString
method, which aids with comparisons and rendering.
-
Tests to cover all of the above have been added. Some additional tests to improve pre-existing ones have been added.
-
Every single mutant in the
infection.log
has been checked - they are all false positives.- Several came back as
escaped
when in fact, the same manual mutation causes tests to fail. - The @covers annotation is correct. It may be a bug in how multi-layer trait inheritance is perceived as covered by Infection.
- MSI has fallen from 95% to 94% and Covered Code MSI has fallen from 97% to 95% - however, there is nothing that can be done to improve it.
- Several came back as
-
isEqualTo
andisNotEqualTo
onIsIntStringMapType
default to using a strict check, which means the item to compare it to must be of the same class. If you do not want a strict check to happen, you can continue to passfalse
for the$strictCheck
parameter. -
isEqualTo
andisNotEqualTo
onIsCollectionType
(which affects all array types) have a new$strictCheck
parameter, defaulting totrue
. When it is true, the item to compare to must be of the same class. If you do not want a strict check to happen, you can passfalse
for the$strictCheck
parameter.
-
Added
isEqualTo
andisNotEqualTo
toIsIntStringMapType
.- Overall Mutation Code Coverage has risen from 97% to 98%. No value has decreased.
-
Added
withValues
,withoutValues
andtryWithoutValues
toIsCollectionType
, allowing to add/remove multiple values with a single method call.- MSI has risen from 94% to 95% and Covered Code MSI from 96% to 97%.
-
It is now possible to also transform values in
IsStringEnumType
before validating. -
There is a new
fromString
method onIsIntType
.
-
Instead of using method-based @covers annotations, we now have class-based @covers annotations, which is working SO much better. Now the Infection log and coverage reports are far more accurate.
-
Added more tests, to bring unit test coverage up to 100%.
-
Throwing
LogicException
when trying to validate the length of a string but passing a higher$minNumber
than$maxNumber
.
-
Added the following methods to all the array types (
IsArrayEnumType
,IsCollectionType
,IsIntArrayEnumType
,IsStringArrayEnumType
,IsClassCollectionType
):count
: Returns the number of elements inside the array value object.isEmpty
: Whether the value object contains any elements.isNotEmpty
: The opposite ofisEmpty
.isEqualTo
: Whether the value object's elements are equal to the argument. The argument can be another class (with atoArray
method), an array, or an object with public properties.isNotEqualTo
: The opposite ofisEqualTo
.empty
: A factory method to create a new instance with no elements.
-
Added a new value type:
IsClassCollectionType
.- This adds on to
IsCollectionType
and makes it easier to create a collection holding an array of objects. It validates whether the items are of a specific class. - By default, it does not allow duplicate values, however this can be overridden.
- This adds on to
-
Added a small helper trait
CanBeConvertedToStringArray
- It can convert an array type into an array of scalar string values.
- Useful when using
IsClassCollectionType
with a class implementing the__toString
method.
-
Adding duplicate values can now be ignored
- This is achieved by overriding the
protected static function ignoreDuplicateValues() : bool
method and returningtrue
. - To be backwards-compatible, it returns
false
by default. - When this method returns true, any duplicate values will be ignored and simply not added to the collection; without throwing an exception.
- This is achieved by overriding the
- Separate exception for duplicate values:
DuplicateValue
.- It is backwards compatible as it extends from
InvalidValue
, but it does now allow developers to catch this exception separately.
- It is backwards compatible as it extends from
IsIntEnumType
was the only type left not using a staticall
method. This has been fixed now.
- Upgrade to PHP 8.1. Note that anything below PHP 8.1 is no longer supported.
Works with PHP 7.3 and above. Tested with PHP 8.1.
-
Added the following types:
IsArrayEnumType
IsFloatType
IsIntArrayEnumType
IsIntStringMapType
IsIntType
IsStringArrayEnumType
IsStringType
IsEmailType
IsCollectionType
-
Added
fromNull
methods toIsStringEnumType
andIsIntEnumType
.
- Added unit and mutation tests which can be run with Docker.
- Basic
IsIntEnumType
andIsStringEnumType