Skip to content
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

Replace all the *Type classes by enumerations #21

Merged
merged 1 commit into from
Nov 18, 2024
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
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