Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Code quality improvements
update unit test for version 2
  • Loading branch information
danielRConsid committed Aug 28, 2025
commit 76818fbf4ecd42d0262a34a19c5e155528dabb5c
20 changes: 19 additions & 1 deletion src/Traits/CollectionUtilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,7 @@ public function eq(int|float|string $key): self
return new self(Helpers::traversArrFromStr($this->toArray(), $key));
}

if(is_object($this->raw)) {
if (is_object($this->raw)) {
return new self($this->raw->{$key} ?? false);
}

Expand Down Expand Up @@ -834,6 +834,11 @@ public function sprint(string $add): self
return $inst;
}

/**
* Parses the string into variables
*
* @return CollectionUtilities|Arr|Traverse
*/
public function parseStr(): self
{
$inst = clone $this;
Expand All @@ -843,6 +848,9 @@ public function parseStr(): self
return $inst;
}




/**
* Same as value validate but will method chain.
* If invalid, then the value will be set to "null" OR whatever you set the fallback
Expand All @@ -853,6 +861,16 @@ public function parseStr(): self
* @return Arr|self|Traverse
* @throws ErrorException
*/
public function expect(string $method, array $args = [], mixed $fallback = null): self
{
$inst = clone $this;
if (!$this->valid($method, $args)) {
$inst->raw = $fallback;
}
return $inst;
}

// Alias to expect
public function validOrFallback(string $method, array $args = [], mixed $fallback = null): self
{
$inst = clone $this;
Expand Down
22 changes: 14 additions & 8 deletions src/Traverse.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,12 +331,12 @@ public function add(string $key, mixed $value): self
*/
public function validator(): Validator
{
return Validator::value($this->raw);
}

// Alias to validator
public function expect(): Validator
{
if (!class_exists(Validator::class)) {
throw new BadMethodCallException(
'Missing dependency: The "Validator" class is not available. ' .
'Install it with "composer require maplephp/validate" to proceed.'
);
}
return Validator::value($this->raw);
}

Expand All @@ -348,15 +348,21 @@ public function expect(): Validator
* @return bool
* @throws ErrorException|BadMethodCallException
*/
public function valid(string $method, array $args = []): bool
public function isValid(string $method, array $args = []): bool
{
$inp = Validator::value($this->raw);
$inp = $this->validator();
if (!method_exists($inp, $method)) {
throw new BadMethodCallException("The MaplePHP validation method \"$method\" does not exist!", 1);
}
return $inp->{$method}(...$args);
}

// Alias it isValid
public function valid(string $method, array $args = []): bool
{
return $this->isValid($method, $args);
}

/**
* Returns the JSON representation of a value
*
Expand Down
13 changes: 6 additions & 7 deletions tests/unitary-dto-dom.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,31 @@
use MaplePHP\DTO\Format\Arr;
use MaplePHP\DTO\Traverse;

$unit = new MaplePHP\Unitary\Unit();

// Begin by adding a test case
$unit->case("MaplePHP DTO Dom", callback: function () {
group("MaplePHP DTO Dom", function (\MaplePHP\Unitary\TestCase $case) {
$obj = Traverse::value([
"title" => "Lorem ipsum dolor",
"content" => "lorem ipsum dolor sit amet, consectetur adipiscing elit.",
]);

$this->add($obj->title->domTag("h1")->strStrlen()->get(), [
$case->add($obj->title->domTag("h1")->strStrlen()->get(), [
"equal" => 28
], "domTag: Failed");

$this->add($obj->title->domTag("h1#title")->strStrlen()->get(), [
$case->add($obj->title->domTag("h1#title")->strStrlen()->get(), [
"equal" => 39
], "domTag id: Failed");

$this->add($obj->title->domTag("h1.title")->strStrlen()->get(), [
$case->add($obj->title->domTag("h1.title")->strStrlen()->get(), [
"equal" => 42
], "domTag add class: Failed");

$this->add($obj->title->domTag("h1.title")->domTag("header")->strClearBreaks()->strStrlen()->get(), [
$case->add($obj->title->domTag("h1.title")->domTag("header")->strClearBreaks()->strStrlen()->get(), [
"equal" => 59
], "domTag chain: Failed");

$this->add($obj->title->domBuild(function($dom) {
$case->add($obj->title->domBuild(function($dom) {
return $dom->tag("h1")->class("title");
})->strStrlen()->get(), [
"equal" => 42
Expand Down
33 changes: 15 additions & 18 deletions tests/unitary-dto-num.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
<?php

use MaplePHP\DTO\Format\Arr;
use MaplePHP\DTO\Traverse;

$unit = new MaplePHP\Unitary\Unit();

// Begin by adding a test case
$unit->case("MaplePHP DTO Number", callback: function () {
group("MaplePHP DTO Number", function (\MaplePHP\Unitary\TestCase $case) {
$obj = Traverse::value([
"price" => 1199.33,
"neg" => -9999.99,
Expand All @@ -16,56 +13,56 @@

$obj->num()::setDefaultLocale("sv_SE");

$this->add($obj->price->numToCurrency("SEK")->strEndsWith("kr")->get(), [
$case->add($obj->price->numToCurrency("SEK")->strEndsWith("kr")->get(), [
"equal" => true
], "toCurrency|setDefaultLocale|strEndsWith Failed");

$this->add($obj->price->num()->getCurrencySymbol("SEK")->get(), [
$case->add($obj->price->num()->getCurrencySymbol("SEK")->get(), [
"equal" => "kr"
], "getCurrencySymbol Failed");

$this->add($obj->price->numToCurrencyIso("SEK")->strStartsWith("SEK")->get(), [
$case->add($obj->price->numToCurrencyIso("SEK")->strStartsWith("SEK")->get(), [
"equal" => true
], "toCurrencyIso Failed");

$this->add($obj->price->num()->float()->get(), [
$case->add($obj->price->num()->float()->get(), [
"isFloat" => true
], "float Failed");

$this->add($obj->price->num()->int()->get(), [
$case->add($obj->price->num()->int()->get(), [
"isInt" => true
], "int Failed");

$this->add($obj->price->num()->floor()->int()->get(), [
$case->add($obj->price->num()->floor()->int()->get(), [
"equal" => 1199
], "floor Failed");

$this->add($obj->price->num()->ceil()->int()->get(), [
$case->add($obj->price->num()->ceil()->int()->get(), [
"equal" => 1200
], "ceil Failed");

$this->add($obj->neg->num()->abs()->int()->get(), [
$case->add($obj->neg->num()->abs()->int()->get(), [
"equal" => 9999
], "abs Failed");

$this->add($obj->price->num()->numberFormat(2)->get(), [
$case->add($obj->price->num()->numberFormat(2)->get(), [
"equal" => "1,199.33"
], "numberFormat Failed");

$this->add($obj->count->num()->leadingZero()->get(), [
$case->add($obj->count->num()->leadingZero()->get(), [
"equal" => "01"
], "leadingZero Failed");

$this->add($obj->price->num()->clamp(1, 20)->int()->get(), [
$case->add($obj->price->num()->clamp(1, 20)->int()->get(), [
"equal" => 20
], "clamp Failed");

$this->add($obj->price->num()->isEven()->get(), [
$case->add($obj->price->num()->isEven()->get(), [
"equal" => false
], "isEven Failed");

$this->add($obj->price->num()->isOdd()->get(), [
$case->add($obj->price->num()->isOdd()->get(), [
"equal" => true
], "isOdd Failed");

});
});
Loading