petrknap/shorts

Set of short PHP helpers

Fund package maintenance!
Other

v3.0.0 2024-11-02 11:23 UTC

This package is auto-updated.

Last update: 2024-11-05 18:02:33 UTC


README

Exception\CouldNotProcessData template

Template for an exception that indicates that the data could not be processed.

namespace PetrKnap\Shorts;

interface ImageResizerException extends \Throwable {}

/** @extends Exception\CouldNotProcessData<string> */
final class ImageResizerCouldNotResizeImage extends Exception\CouldNotProcessData implements ImageResizerException {}

final class ImageResizer {
    public function resize(string $image) {
        throw new ImageResizerCouldNotResizeImage(__METHOD__, $image);
    }
}

Exception\NotImplemented

Simple exception for prototyping purposes.

namespace PetrKnap\Shorts;

final class StringablePrototype implements \Stringable {
    public function __toString(): string {
        Exception\NotImplemented::throw(__METHOD__);
    }
}

HasRequirements trait

Simple trait to check if requirements of your code are fulfilled.

namespace PetrKnap\Shorts;

final class ServiceWithRequirements {
    use HasRequirements;
    
    public function __construct() {
        self::checkRequirements(functions: ['required_function']);
    }

    public function do(): void {
        required_function();
    }
}

It should not replace Composers requires, but it could improve them and check suggests.

Run composer require petrknap/shorts to install it. You can support this project via donation. The project is licensed under the terms of the LGPL-3.0-or-later.