Skip to content

Commit

Permalink
Merge pull request #21 from meritoo/refactor/enumerations-instead-of-…
Browse files Browse the repository at this point in the history
…the-type-classes

Replace all the `*Type` classes by enumerations
  • Loading branch information
meritoo authored Nov 18, 2024
2 parents 8062c06 + 0a7197d commit dbbd592
Show file tree
Hide file tree
Showing 62 changed files with 389 additions and 1,478 deletions.
20 changes: 20 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,26 @@

Common and useful classes, methods, exceptions etc.

# 1.3.2

1. All the `*Type` classes, that extend `Meritoo\Common\Type\Base\BaseType` class, have been replaced by enumerations

| Before | After |
|-----------------------------------------|----------------------------------------|
| `Meritoo\Common\Type\DatePartType` | `Meritoo\Common\Enums\Date\DatePart` |
| `Meritoo\Common\Type\DatePeriod` | `Meritoo\Common\Enums\Date\DatePeriod` |
| `Meritoo\Common\Type\OopVisibilityType` | `Meritoo\Common\Enums\OopVisibility` |

2. Other than that:

- The `Meritoo\Common\Type\DatePartType` class has been moved to `Meritoo\Common\ValueObject\DatePeriod`
- Exceptions that extend `Meritoo\Common\Exception\Base\UnknownTypeException` has been removed as not needed anymore
- The following classes have been removed as not needed anymore:
- `Meritoo\Common\Exception\Base\UnknownTypeException`
- `Meritoo\Common\Test\Base\BaseTypeTestCase`
- `Meritoo\Common\Traits\Test\Base\BaseTypeTestCaseTrait`
- `Meritoo\Common\Type\Base\BaseType`

# 1.3.1

1. Arguments passed to the `Meritoo\Common\Utilities\Miscellaneous::concatenatePaths()` should be `string`s. The method
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.3.1
1.3.2
42 changes: 2 additions & 40 deletions docs/Exceptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,48 +11,10 @@ used to create instance of the exception. Example:

```php
use Meritoo\Common\Exception\Bundle\IncorrectBundleNameException;
throw IncorrectBundleNameException::create('RisusIpsum');
```

### Base exception for unknown type of something

##### Short description

It's a `Meritoo\Common\Exception\Base\UnknownTypeException` class. Related to `Meritoo\Common\Type\Base\BaseType` class
that represents type of something, e.g. type of button, order.

##### Usage

You can extend `Meritoo\Common\Exception\Base\UnknownTypeException` class and create your own static method,
e.g. `createException()`, which will be used create instance of the exception. Inside the `createException()` method you
can call `parent::create()` method.

##### Example
// ...

```php
<?php

namespace Your\Package\Exception\Type;

use Meritoo\Common\Exception\Base\UnknownTypeException;
use Your\Package\Type\SimpleType;

class UnknownSimpleTypeException extends UnknownTypeException
{
/**
* Creates exception
*
* @param string $unknownType Unknown and simple type
* @return UnknownSimpleTypeException
*/
public static function createException($unknownType)
{
/* @var UnknownSimpleTypeException $exception */
$exception = parent::create($unknownType, new SimpleType(), 'my simple type of something');

return $exception;
}
}
throw IncorrectBundleNameException::create('RisusIpsum');
```

# More
Expand Down
21 changes: 21 additions & 0 deletions src/Enums/Date/DatePart.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/**
* (c) Meritoo.pl, http://www.meritoo.pl
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Meritoo\Common\Enums\Date;

enum DatePart: string
{
case Day = 'day';
case Hour = 'hour';
case Minute = 'minute';
case Month = 'month';
case Second = 'second';
case Year = 'year';
}
24 changes: 24 additions & 0 deletions src/Enums/Date/DatePeriod.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/**
* (c) Meritoo.pl, http://www.meritoo.pl
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Meritoo\Common\Enums\Date;

enum DatePeriod: int
{
case LastMonth = 4;
case LastWeek = 1;
case LastYear = 7;
case NextMonth = 6;
case NextWeek = 3;
case NextYear = 9;
case ThisMonth = 5;
case ThisWeek = 2;
case ThisYear = 8;
}
18 changes: 18 additions & 0 deletions src/Enums/OopVisibility.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/**
* (c) Meritoo.pl, http://www.meritoo.pl
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Meritoo\Common\Enums;

enum OopVisibility: int
{
case Private = 3;
case Protected = 2;
case Public = 1;
}
41 changes: 0 additions & 41 deletions src/Exception/Base/UnknownTypeException.php

This file was deleted.

27 changes: 27 additions & 0 deletions src/Exception/Date/InvalidDatePartException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/**
* (c) Meritoo.pl, http://www.meritoo.pl
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Meritoo\Common\Exception\Date;

use Meritoo\Common\Enums\Date\DatePart;

final class InvalidDatePartException extends \Exception
{
public function __construct(DatePart $datePart, string $value)
{
$message = \sprintf(
'Value of the \'%s\' date part is invalid: %s',
$datePart->value,
$value,
);

parent::__construct($message);
}
}
12 changes: 6 additions & 6 deletions src/Exception/Reflection/TooManyChildClassesException.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ class TooManyChildClassesException extends Exception
/**
* Creates exception
*
* @param array|object|string $parentClass Class that has more than one child class, but it shouldn't. An array
* of objects, strings, object or string.
* @param array $childClasses Child classes
* @param object|string $parentClass Class that has more than one child class, but it shouldn't
* @param array $childClasses Child classes
*
* @return TooManyChildClassesException
*/
public static function create($parentClass, array $childClasses): TooManyChildClassesException
public static function create(object|string $parentClass, array $childClasses): TooManyChildClassesException
{
$template = "The '%s' class requires one child class at most who will extend her, but more than one child"
." class was found:\n- %s\n\nWhy did you create more than one classes that extend '%s' class?";
$template = "The %s class requires one child class at most who will extend her, but more than one child"
." class was found:\n- %s\n\nWhy did you create more than one classes that extend %s class?";

$parentClassName = Reflection::getClassName($parentClass) ?? '[unknown class]';
$message = sprintf($template, $parentClassName, implode("\n- ", $childClasses), $parentClassName);
Expand Down
39 changes: 0 additions & 39 deletions src/Exception/Type/UnknownDatePartTypeException.php

This file was deleted.

34 changes: 0 additions & 34 deletions src/Exception/Type/UnknownOopVisibilityTypeException.php

This file was deleted.

22 changes: 0 additions & 22 deletions src/Test/Base/BaseTypeTestCase.php

This file was deleted.

Loading

0 comments on commit dbbd592

Please sign in to comment.