Skip to content
Merged
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
2 changes: 0 additions & 2 deletions src/Exceptions/HashTableFullException.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,5 @@ public function __construct(
if (empty($message)) {
$this->message = self::ERROR_UNMATCHED_CLASS;
}

parent::__construct($message, $code, $previous);
}
}
6 changes: 3 additions & 3 deletions src/Exceptions/UnmatchedClassException.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ class UnmatchedClassException extends \Exception
const ERROR_UNMATCHED_CLASS = 'Unmatched class type on deserialization';

public function __construct(
#[LanguageLevelTypeAware(['8.0' => 'string'], default: self::ERROR_UNMATCHED_CLASS)] $message = self::ERROR_UNMATCHED_CLASS,
#[LanguageLevelTypeAware(['8.0' => 'int'], default: '')] $code = 0,
#[LanguageLevelTypeAware(['8.0' => 'Throwable|null'], default: 'Throwable')] $previous = null
$message = self::ERROR_UNMATCHED_CLASS,
$code = 0,
$previous = null
) {
parent::__construct($message, $code, $previous);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Exceptions/UnmatchedVersionException.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ class UnmatchedVersionException extends \Exception
const ERROR_WRONG_VERSION = 'Unmatched version on deserialization';

public function __construct(
#[LanguageLevelTypeAware(['8.0' => 'string'], default: self::ERROR_WRONG_VERSION)] $message = self::ERROR_WRONG_VERSION,
#[LanguageLevelTypeAware(['8.0' => 'int'], default: '')] $code = 0,
#[LanguageLevelTypeAware(['8.0' => 'Throwable|null'], default: 'Throwable')] $previous = null
$message = self::ERROR_WRONG_VERSION,
$code = 0,
$previous = null
) {
parent::__construct($message, $code, $previous);
}
Expand Down
4 changes: 1 addition & 3 deletions src/Traits/CLITrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

/**
* Collection of usable CLI functions.
*
* @method bool isCli()
*/
trait CLITrait
{
Expand All @@ -14,7 +12,7 @@ trait CLITrait
*
* @return bool
*/
public function isCli()
public function isCli() : bool
{
return php_sapi_name() === 'cli';
}
Expand Down
22 changes: 14 additions & 8 deletions src/Traits/PerformanceTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,35 @@

namespace PHPAlchemist\Traits;

/**
* @deprecated Will be removed in v4.0.0
*/
trait PerformanceTrait
{
const int ONE_KILOBYTE_IN_BYTES = 1024;
const int ONE_MEGABYTE_IN_BYTES = 1048576;

/**
* get the performance data (Peak Memory Usage) for a given script or
* class.
*
* @return string
*/
public function getPerformance()
public function getPerformance() : string
{
$mem_usage = memory_get_peak_usage();
if ($mem_usage < 1024) {
$memUsage = memory_get_peak_usage();
if ($memUsage < self::ONE_KILOBYTE_IN_BYTES) {
// These two are ignored as the nature of phpUnit testing will not allow me to come in under a meg
// and run coverage
// @codeCoverageIgnoreStart
$whoa = $mem_usage.' bytes';
} elseif ($mem_usage < 1048576) {
$whoa = round($mem_usage / 1024, 2).' kilobytes';
$displayData = $memUsage.' bytes';
} elseif ($memUsage < self::ONE_MEGABYTE_IN_BYTES) {
$displayData = round($memUsage / self::ONE_KILOBYTE_IN_BYTES, 2).' kilobytes';
// @codeCoverageIgnoreEnd
} else {
$whoa = round($mem_usage / 1048576, 2).' megabytes';
$displayData = round($memUsage / self::ONE_MEGABYTE_IN_BYTES, 2).' megabytes';
}

return $whoa;
return $displayData;
}
}
7 changes: 4 additions & 3 deletions src/Utilities/CSVUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ class CSVUtil
* Nice String output replacement form fputcsv
* code taken from: http://www.php.net/manual/en/function.fputcsv.php#96937.
*
* @param $row
*
* @param array $row
* @param string $delimiter
* @param string $enclosure
* @param string $eol
*
* @return bool|string
*/
public static function sputcsv($row, $delimiter = ',', $enclosure = '"', $eol = PHP_EOL)
public static function sputcsv(array $row, string $delimiter = ',', string $enclosure = '"', string $eol = PHP_EOL) : string
{
static $fp = false;
if ($fp === false) {
Expand All @@ -26,7 +27,7 @@ public static function sputcsv($row, $delimiter = ',', $enclosure = '"', $eol =
}

if (fputcsv($fp, $row, $delimiter, $enclosure, '\\', PHP_EOL) === false) {
return false;
return '';
}

rewind($fp);
Expand Down
32 changes: 32 additions & 0 deletions src/ValueObject/Abstract/AbstractNumber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace PHPAlchemist\ValueObject\Abstract;

use PHPAlchemist\ValueObject\Contract\VONumberInterface;

abstract class AbstractNumber implements VONumberInterface
{
protected readonly int $value;

public function __construct(int $value)
{
$this->value = $value;
}

public function getValue() : int|float
{
return $this->value;
}

public function equals(VONumberInterface $number) : bool
{
if (
$number === $this
&& $number->getValue() === $this->getValue()
) {
return true;
}

return false;
}
}
95 changes: 95 additions & 0 deletions src/ValueObject/Abstract/AbstractString.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

namespace PHPAlchemist\ValueObject\Abstract;

use PHPAlchemist\Types\Twine;
use PHPAlchemist\ValueObject\Contract\VONumberInterface;
use PHPAlchemist\ValueObject\Contract\VOStringInterface;
use PHPAlchemist\ValueObject\Model\Number;

abstract class AbstractString implements VOStringInterface
{
protected readonly string $value;

public function __toString() : string
{
return ($this->getValue()) ?: '';
}

public function getValue() : ?string
{
return $this->value;
}

public function contains(mixed $needle, bool $caseInsensitive = false) : bool
{
if ($caseInsensitive) {
return str_contains(mb_strtolower($this->value), mb_strtolower($needle));
}

return str_contains($this->value, $needle);
}

/** @inheritDoc */
public function endsWith(mixed $needle) : bool
{
return str_ends_with($this->value, $needle);
}

/** @inheritDoc */
public function equals(VOStringInterface $comparitive) : bool
{
return $comparitive->getValue() === $this->getValue();
}

/**
* @inheritDoc
*/
public function hasValue() : bool
{
return !is_null($this->value);
}

/** @inheritDoc */
public function indexOf(string $needle, int $startIndex = 0) : int|false
{
return strpos($this->value, $needle, $startIndex);
}

public function lastIndexOf(string $needle, int $startIndex = 0) : int|false
{
return strrpos($this->value, $needle, $startIndex);
}

/**
* @inheritDoc
*/
public function length() : VONumberInterface
{
$length = strlen($this->value);

return new Number($length);
}

/**
* Convert string to lower case.
*
* @return string
*/
public function lower() : string
{
return mb_strtolower($this->getValue());
}

/** @inheritDoc */
public function startsWith(mixed $needle) : bool
{
return str_starts_with($this->value, $needle);
}

/** @inheritDoc */
public function substring(int $offset, ?int $length = null) : VOStringInterface
{
return new Twine(substr($this->value, $offset, $length));
}
}
10 changes: 10 additions & 0 deletions src/ValueObject/Contract/VONumberInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace PHPAlchemist\ValueObject\Contract;

interface VONumberInterface
{
public function getValue() : int|float;

public function equals(self $number) : bool;
}
73 changes: 73 additions & 0 deletions src/ValueObject/Contract/VOStringInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace PHPAlchemist\ValueObject\Contract;

interface VOStringInterface extends \Stringable
{
/** @return string */
public function __toString() : string;

/**
* @param mixed $needle
* @param bool $caseInsensitive
*
* @return bool
*/
public function contains(mixed $needle, bool $caseInsensitive = false) : bool;

public function endsWith(mixed $needle) : bool;

/**
* @param VOStringInterface $comparitive
*
* @return bool
*/
public function equals(self $comparitive) : bool;

/**
* Get value of String object.
*
* @return string
*/
public function getValue() : ?string;

/**
* Determine if string has value.
*
* @return bool
*/
public function hasValue() : bool;

/**
* @param string $needle
* @param int $startIndex
*
* @return false|int
*/
public function indexOf(string $needle, int $startIndex = 0) : int|false;

/**
* @param string $needle
* @param int $startIndex
*
* @return int|false
*/
public function lastIndexOf(string $needle, int $startIndex = 0) : int|false;

public function length() : VONumberInterface;

/**
* @param mixed $needle
*
* @return bool
*/
public function startsWith(mixed $needle) : bool;

/**
* @param int $offset
* @param int|null $length
*
* @return VOStringInterface
*/
public function substring(int $offset, ?int $length) : VOStringInterface;
}
40 changes: 40 additions & 0 deletions src/ValueObject/Model/Email.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace PHPAlchemist\ValueObject\Model;

use InvalidArgumentException;
use PHPAlchemist\Types\Twine;
use PHPAlchemist\ValueObject\Abstract\AbstractString;

final class Email extends AbstractString
{
public function __construct(string $value)
{
if (!filter_var($value, FILTER_VALIDATE_EMAIL)) {
throw new InvalidArgumentException('Invalid email address.');
}

$this->value = $value;
}

public function getUser() : Twine
{
$parts = explode('@', $this->value, 2);

return new Twine(array_shift($parts));
}

public function getDomain() : Twine
{
$parts = explode('@', $this->value, 2);

return new Twine(array_pop($parts));
}

public function getTLD() : Twine
{
$parts = explode('.', $this->value);

return new Twine(array_pop($parts));
}
}
9 changes: 9 additions & 0 deletions src/ValueObject/Model/Id.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace PHPAlchemist\ValueObject\Model;

use PHPAlchemist\ValueObjects\Abstract\AbstractNumber;

final class Id extends AbstractNumber
{
}
9 changes: 9 additions & 0 deletions src/ValueObject/Model/Number.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace PHPAlchemist\ValueObject\Model;

use PHPAlchemist\ValueObject\Abstract\AbstractNumber;

final class Number extends AbstractNumber
{
}
Loading