Skip to content

v3.1.0 #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
41 changes: 34 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

DTO stands for **Darn Tidy Object**, a playful twist on the traditional Data Transfer Object. But this isn’t your average DTO. It’s a fully-loaded toolkit for **traversing, transforming, and tidying up structured data** in PHP with style, power, and simplicity.

_It also makes your life easier by ensuring every piece of data is returned in the correct type-helping. Whether you expect an int, string, bool, or even a callable, DTO gives you strict, reliable access to your data with minimal effort._

---

## 📦 Installation

Expand All @@ -10,13 +13,15 @@ composer require maplephp/dto
```

## 📘 Documentation
- [Why DTO?](https://maplephp.github.io/DTO/docs/intro#why-dto)
- [Traverse Collection](https://maplephp.github.io/DTO/docs/traverse)
- [Format string](https://maplephp.github.io/DTO/docs/format-string)
- [Format Number](https://maplephp.github.io/DTO/docs/format-number)
- [Format Clock](https://maplephp.github.io/DTO/docs/format-clock)
- [Format Dom](https://maplephp.github.io/DTO/docs/format-dom)

* [Why DTO?](https://maplephp.github.io/DTO/docs/intro#why-dto)
* [Traverse Collection](https://maplephp.github.io/DTO/docs/traverse)
* [Format string](https://maplephp.github.io/DTO/docs/format-string)
* [Format Number](https://maplephp.github.io/DTO/docs/format-number)
* [Format Clock](https://maplephp.github.io/DTO/docs/format-clock)
* [Format Dom](https://maplephp.github.io/DTO/docs/format-dom)

---

## How It Works

Expand Down Expand Up @@ -60,6 +65,26 @@ echo $obj->article->content->strExcerpt()->strUcFirst();

---

### Correct Type Handling (with ease)

No more clunky `is_numeric` checks or `intval` casts. DTO makes it simple to extract values in the exact type you expect:

```php
$orderId = $dto->order->id->toInt();
// Result: 1234 (int)
```

Handle flexible types cleanly with fallbacks:

```php
$callback = $dto->settings->onReady->acceptType(['callable', 'null']);
if (is_callable($callback)) {
$callback(); // Result: Runs a startup hook or closure
}
```

---

### Built-In Data Transformation

Transform values directly using built-in helpers like:
Expand Down Expand Up @@ -130,4 +155,6 @@ print_r($updated->toArray());

---

Now go forth, write cleaner code, and let DTO handle the messy parts.
Now go forth, write cleaner code, and let DTO handle the messy parts.

---
11 changes: 5 additions & 6 deletions src/DynamicDataAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@

namespace MaplePHP\DTO;

abstract class DynamicDataAbstract
/** @psalm-no-seal-properties */
abstract class DynamicDataAbstract extends \stdClass
{
private object $data;

abstract public function get();

/**
* Create a dynamic object that holds a dynamic set of items
*/
Expand Down Expand Up @@ -59,11 +58,11 @@ public function __set(string $key, mixed $value): void
* Try to get data object item
*
* @param $key
* @return null
* @return self
*/
public function __get($key)
public function __get($key): self
{
return ($this->data->{$key} ?? null);
return ($this->data->{$key} ?? $this);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Format/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ public function __construct(mixed $value)
* @param array $arguments
* @return mixed
*/
function __call(string $name, array $arguments)
public function __call(string $name, array $arguments)
{
$inst = new Traverse($this->raw);
if(!method_exists($inst, $name)) {
if (!method_exists($inst, $name)) {
throw new \BadMethodCallException("Method '$name' does not exist.");
}
return $inst->$name(...$arguments);
Expand Down
24 changes: 12 additions & 12 deletions src/Format/Clock.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@

final class Clock extends FormatAbstract implements FormatInterface
{
static protected ?string $defaultLocale = 'en';
protected static ?string $defaultLocale = 'en';

static protected string|DateTimeZone|null $defaultTimezone = null;
protected static string|DateTimeZone|null $defaultTimezone = null;

protected ?string $locale = null;
protected array $parts = [];
Expand All @@ -38,7 +38,7 @@ public function __construct(mixed $value)
throw new InvalidArgumentException("Is expecting a string or a convertable string value.", 1);
}
$date = new DateTime($value);
if(!is_null(self::$defaultTimezone)) {
if (self::$defaultTimezone !== null) {
$date->setTimezone(self::$defaultTimezone);
}
parent::__construct($date);
Expand Down Expand Up @@ -100,7 +100,7 @@ public function setLocale(string $localeCode): self
* @param string $localeCode
* @return void
*/
static public function setDefaultLocale(string $localeCode): void
public static function setDefaultLocale(string $localeCode): void
{
self::$defaultLocale = $localeCode;
}
Expand Down Expand Up @@ -130,7 +130,7 @@ public function setTimezone(DateTimeZone|string $timezone): self
* @return void
* @throws \DateInvalidTimeZoneException
*/
static public function setDefaultTimezone(string|DateTimeZone $timezone): void
public static function setDefaultTimezone(string|DateTimeZone $timezone): void
{
self::$defaultTimezone = $timezone instanceof DateTimeZone ? $timezone : new DateTimeZone($timezone);
}
Expand All @@ -154,9 +154,9 @@ public function timezone(): string
*/
public function format(string $format = 'Y-m-d H:i:s', ?string $locale = null): string
{
$locale = !is_null($locale) ? $locale : $this->getLocale();
$locale = $locale !== null ? $locale : $this->getLocale();
$translations = $this->getTranslationData($this->raw, $locale);
if($translations) {
if ($translations) {
return str_replace($translations['find'], $translations['replace'], $this->raw->format($format));
}
return $this->raw->format($format);
Expand Down Expand Up @@ -386,14 +386,14 @@ public function shortWeekday(): string
protected function getTranslationData(DateTime $date, string $locale): array
{

if($locale !== "en") {
if ($locale !== "en") {
$translations = $this->getTranslation($locale);
if($translations === false) {
if ($translations === false) {
$formatters = $this->getLocaleTranslation($locale);
}
}

if(!isset($translations) && !isset($formatters)) {
if (!isset($translations) && !isset($formatters)) {
return [];
}

Expand All @@ -417,7 +417,7 @@ protected function getTranslationData(DateTime $date, string $locale): array
protected function getTranslation(string $locale): array|false
{
$translationFile = realpath(__DIR__ . "/../lang/$locale.php");
if($translationFile !== false) {
if ($translationFile !== false) {
return require $translationFile;
}
return false;
Expand All @@ -441,7 +441,7 @@ protected function getLocaleTranslation(string $locale): array
'monthFull' => new IntlDateFormatter($locale, IntlDateFormatter::LONG, IntlDateFormatter::NONE, null, null, 'MMMM'),
'monthShort' => new IntlDateFormatter($locale, IntlDateFormatter::MEDIUM, IntlDateFormatter::NONE, null, null, 'MMM'),
'weekdayFull' => new IntlDateFormatter($locale, IntlDateFormatter::FULL, IntlDateFormatter::NONE, null, null, 'EEEE'),
'weekdayShort'=> new IntlDateFormatter($locale, IntlDateFormatter::FULL, IntlDateFormatter::NONE, null, null, 'E')
'weekdayShort' => new IntlDateFormatter($locale, IntlDateFormatter::FULL, IntlDateFormatter::NONE, null, null, 'E')
];
}
return $this->parts[$locale];
Expand Down
2 changes: 1 addition & 1 deletion src/Format/Dom.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public function element(): ElementInterface
{
$this->str = Str::value($this->raw);
$elem = $this->dom->create($this->tag, $this->str)->hideEmptyTag(true);
if($this->attr) {
if ($this->attr) {
$elem->attrArr($this->attr);
}
return $elem;
Expand Down
4 changes: 2 additions & 2 deletions src/Format/Encode.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public function urldecode(?callable $callback = null): string|array
if (is_array($this->raw)) {
$this->raw = Arr::value($this->raw)->walk(function ($value) use ($callback) {
$value = Str::value((string)$value)->rawurldecode()->get();
if (!is_null($callback)) {
if ($callback !== null) {
$value = $callback($value);
}
return $value;
Expand All @@ -110,7 +110,7 @@ public function urldecode(?callable $callback = null): string|array

} else {
$this->raw = Str::value($this->raw)->rawurldecode()->get();
if (!is_null($callback)) {
if ($callback !== null) {
$this->raw = $callback($this->raw);
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/Format/Local.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static function value(mixed $data)
{

if (is_string($data)) {
if (is_null(self::$dir)) {
if (self::$dir === null) {
throw new Exception("You need to set default lang directory.", 1);
}
if (!is_file(self::$dir . "{$data}.php")) {
Expand All @@ -44,7 +44,7 @@ public static function value(mixed $data)
}
}

if (is_null(self::$data[$data])) {
if (self::$data[$data] === null) {
throw new Exception("Could not propagate the language data object with any information.", 1);
}

Expand Down Expand Up @@ -89,10 +89,10 @@ public function getValue(string $key): ?string

public function get(string|array $key, string $fallback = "", ?array $sprint = null): ?string
{
if (is_null($this::$prefix)) {
if ($this::$prefix === null) {
throw new Exception("Lang prefix is null.", 1);
}
if (!is_null($sprint)) {
if ($sprint !== null) {
$this->sprint($sprint);
}

Expand All @@ -105,7 +105,7 @@ public function get(string|array $key, string $fallback = "", ?array $sprint = n
}

$value = ($this->value[$key][$this::$prefix] ?? $fallback);
if (is_null($sprint)) {
if ($sprint === null) {
return $value;
}

Expand Down
5 changes: 3 additions & 2 deletions src/Format/Num.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* DEPRECATED
* @Package: MaplePHP Format numbers
Expand Down Expand Up @@ -62,10 +63,10 @@ public function setLocale(string $locale, int $type = NumberFormatter::CURRENCY)
*/
public function getNumFormatter(): NumberFormatter
{
if(!is_null($this->numInst)) {
if ($this->numInst !== null) {
return $this->numInst;
}
if(is_null(self::$defNumInst)) {
if (self::$defNumInst === null) {
throw new \InvalidArgumentException("NumberFormatter instance not set.");
}
return self::$defNumInst;
Expand Down
Loading