Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
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
69 changes: 69 additions & 0 deletions src/PHP.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
use function str_contains;
use function unserialize;
use const JSON_ERROR_NONE;
use const JSON_PRETTY_PRINT;


/**
Expand Down Expand Up @@ -1157,4 +1158,72 @@ static function with($obj, callable $callback): void {
$obj->___withEnd($obj);
}
}

static function int(mixed $val): int {
if (is_object($val)) {
if (static::classUsesTrait($val, MetaMagic::class)) {
return static::metaMagicSpell($val, 'int');
}
}
return (int) $val;
}

static function float(mixed $val): float {
if (is_object($val)) {
if (static::classUsesTrait($val, MetaMagic::class)) {
return static::metaMagicSpell($val, 'float');
}
}
return (float) $val;
}

static function bool(mixed $val): bool {
if (is_object($val)) {
if (static::classUsesTrait($val, MetaMagic::class)) {
return static::metaMagicSpell($val, 'bool');
}
}
return (bool) $val;
}

// todo Use Str helper inside
static function str(mixed $val): string {
return (string) $val;
}

// static function obj(mixed $val, $is_std_class = false): SimpleObject|stdClass {
// if (is_object($val)) {
// if (static::classUsesTrait($val, MetaMagic::class)) {
// return static::metaMagicSpell($val, 'obj', $val, $is_std_class);
// }
// }
// // FIX Unfinished
//// return (obj) $val;
// }

static function arr(mixed $val): array {
if (is_object($val)) {
if (static::classUsesTrait($val, MetaMagic::class)) {
return static::metaMagicSpell($val, 'array');
}
}
return (array) $val;
}

static function json(mixed $val, ?bool $pretty = null, bool $with_class = false): string {
if (is_object($val)) {
if (static::classUsesTrait($val, MetaMagic::class)) {
return static::metaMagicSpell($val, 'json', $pretty, $with_class);
}
}
return json_encode($val, PHP::flagsForJson($pretty));
}

static function flagsForJson(bool $pretty) {
$flags = 0;
if ($pretty) {
$flags |= JSON_PRETTY_PRINT;
}
return $flags;
}
}
36 changes: 36 additions & 0 deletions src/basic.php
Original file line number Diff line number Diff line change
Expand Up @@ -848,3 +848,39 @@ function ip(string|BasicIP $ip): IPv4 {
function with($obj, callable $callback): void {
PHP::with($obj, $callback);
}

#[Shortcut('PHP::str()')]
function str(mixed $val): string {
return PHP::str($val);
}

#[Shortcut('PHP::int()')]
function int(mixed $val): int {
return PHP::int($val);
}

#[Shortcut('PHP::float()')]
function float(mixed $val): float {
return PHP::float($val);
}

#[Shortcut('PHP::bool()')]
function bool(mixed $val): bool {
return PHP::bool($val);
}

//#[Shortcut('PHP::obj()')]
//function obj(mixed $val, $is_std_class = false): SimpleObject|stdClass {
// return PHP::obj($val, $is_std_class);
//}

#[Shortcut('PHP::arr()')]
function arr(mixed $val): array {
return PHP::arr($val);
}

#[Shortcut('PHP::json()')]
function json(mixed $val, ?bool $pretty = null, bool $with_class = false): string {
return PHP::json($val, $pretty, $with_class);
}

11 changes: 11 additions & 0 deletions src/exceptions/types/NonArrayObjException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace spaf\simputils\exceptions\types;

use Exception;

class NonArrayObjException extends Exception {

protected $message = 'This object does not support array conversion. Override ___array() method';

}
11 changes: 11 additions & 0 deletions src/exceptions/types/NonBoolObjException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace spaf\simputils\exceptions\types;

use Exception;

class NonBoolObjException extends Exception {

protected $message = 'This object does not support bool conversion. Override ___bool() method';

}
11 changes: 11 additions & 0 deletions src/exceptions/types/NonFloatObjException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace spaf\simputils\exceptions\types;

use Exception;

class NonFloatObjException extends Exception {

protected $message = 'This object does not support float conversion. Override ___float() method';

}
11 changes: 11 additions & 0 deletions src/exceptions/types/NonIntObjException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace spaf\simputils\exceptions\types;

use Exception;

class NonIntObjException extends Exception {

protected $message = 'This object does not support int conversion. Override ___int() method';

}
31 changes: 29 additions & 2 deletions src/models/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use DateTimeInterface;
use spaf\simputils\attributes\DebugHide;
use spaf\simputils\attributes\Extract;
use spaf\simputils\attributes\markers\Shortcut;
use spaf\simputils\attributes\Property;
use spaf\simputils\DT;
Expand Down Expand Up @@ -105,8 +106,10 @@ public function __construct(
* @var static $_orig_value
*/
#[DebugHide]
#[Extract(false)]
protected $_orig_value;

#[Extract(false)]
#[Property('orig_value')]
protected function getOrigValue(): static|null {
return $this->_orig_value;
Expand All @@ -123,6 +126,7 @@ public function snapshotOrigValue(bool $overwrite = true) {
* TODO Implement caching of the value
* @return \spaf\simputils\models\Date|string
*/
#[Extract(false)]
#[Property('date')]
protected function getDateExt(): Date|string {
return new Date($this);
Expand All @@ -133,36 +137,43 @@ protected function getDateExt(): Date|string {
* TODO Implement caching of the value
* @return \spaf\simputils\models\Time|string
*/
#[Extract(false)]
#[Property('time')]
protected function getTime(): Time|string {
return new Time($this);
}

#[Extract(false)]
#[Property('week')]
protected function getWeek(): int {
return (int) $this->format('W');
}

#[Extract(false)]
#[Property('dow')]
protected function getDow(): int {
return (int) $this->format('w');
}

#[Extract(false)]
#[Property('dow_iso')]
protected function getDowIso(): int {
return (int) $this->format('N');
}

#[Extract(false)]
#[Property('is_weekend')]
protected function getIsWeekend(): bool {
return $this->dow_iso > 5;
}

#[Extract(false)]
#[Property('is_weekday')]
protected function getIsWeekday(): bool {
return !$this->is_weekend;
}

#[Extract(false)]
#[Property('doy')]
protected function getDoy(): int {
return (int) $this->format('z');
Expand Down Expand Up @@ -198,6 +209,7 @@ public function setTimezone($timezone): static {
return parent::setTimezone($timezone);
}

#[Extract(false)]
#[Property('year')]
protected function getYear(): int {
return (int) $this->format('Y');
Expand All @@ -208,6 +220,7 @@ protected function setYear(int $year): void {
$this->setDate($year, $this->month, $this->day);
}

#[Extract(false)]
#[Property('month')]
protected function getMonth(): int {
return (int) $this->format('n');
Expand All @@ -218,6 +231,7 @@ protected function setMonth(int $month): void {
$this->setDate($this->year, $month, $this->day);
}

#[Extract(false)]
#[Property('day')]
protected function getDay(): int {
return (int) $this->format('j');
Expand All @@ -228,6 +242,7 @@ protected function setDay(int $day): void {
$this->setDate($this->year, $this->month, $day);
}

#[Extract(false)]
#[Property('hour')]
protected function getHour(): int {
return (int) $this->format('G');
Expand All @@ -238,6 +253,7 @@ protected function setHour(int $hour): void {
$this->setTime($hour, $this->minute, $this->second, $this->micro);
}

#[Extract(false)]
#[Property('minute')]
protected function getMinute(): int {
return (int) $this->format('i');
Expand All @@ -248,6 +264,7 @@ protected function setMinute(int $minute): void {
$this->setTime($this->hour, $minute, $this->second, $this->micro);
}

#[Extract(false)]
#[Property('second')]
protected function getSecond(): int {
return (int) $this->format('s');
Expand All @@ -258,6 +275,7 @@ protected function setSecond(int $second): void {
$this->setTime($this->hour, $this->minute, $second, $this->micro);
}

#[Extract(false)]
#[Property('micro')]
protected function getMicro(): int {
return (int) $this->format('u');
Expand All @@ -268,6 +286,7 @@ protected function setMicro(int $micro): void {
$this->setTime($this->hour, $this->minute, $this->second, $micro);
}

#[Extract(false)]
#[Property('milli')]
protected function getMilli(): int {
return (int) $this->format('v');
Expand All @@ -289,6 +308,7 @@ protected function getForSystem(): string {
return $this->getForSystemObj()->format(DT::FMT_DATETIME_FULL);
}

#[Extract(false)]
#[Property('for_user')]
protected function getForUser(): string {
return $this->format(static::$l10n_user_datetime_format);
Expand Down Expand Up @@ -410,6 +430,7 @@ function f($format) {
return $this->format($format); //@codeCoverageIgnore
}

#[Extract(false)]
#[Property('timestamp')]
#[Shortcut('static::getTimestamp()')]
protected function getTimestampProperty(): int {
Expand All @@ -422,8 +443,10 @@ public function format(string $format): string {
}


public function toJson(?bool $pretty = null, bool $with_class = false): string {
// TODO Implement optional choice of "for_*"
protected function ___json(?bool $pretty = null, bool $with_class = false): string {
if ($with_class) {
return parent::___json($pretty, $with_class);
}
return json_encode($this->for_system);
}

Expand Down Expand Up @@ -452,4 +475,8 @@ protected function ___deserialize(array|Box $data): static {
$this->setFromData($data);
return $this;
}

protected function ___int(): int {
return $this->timestamp;
}
}
Loading