1. Go to this page and download the library: Download troum/mycro-core library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
troum / mycro-core example snippets
use Mycro\Core\Attributes\DefaultValue;
use Mycro\Core\Contracts\BaseDto;
final class UserRegisterDto extends BaseDto
{
public readonly string $first_name;
public readonly string $last_name;
public readonly string $email;
public readonly string $password;
public readonly string $phone;
#[DefaultValue(null)]
public readonly ?string $second_name;
}
$dto = new UserRegisterDto($request->validated());
$array = $dto->toArray();
#[DefaultValue(null)]
public readonly ?string $second_name;
#[DefaultValue('ru')]
public readonly string $locale;
use Mycro\Core\Attributes\MapProperty;
// API отдаёт user_id, в DTO — id
#[MapProperty(from: 'user_id')]
public readonly int $id;
// Несколько алиасов
#[MapProperty(from: ['phone_number', 'mobile'])]
public readonly string $phone;
// Необязательное поле: при отсутствии ключа будет null
#[MapProperty(from: 'middle_name',
use Mycro\Core\Attributes\Transform;
use Mycro\Core\Contracts\TransformerContract;
final class TrimTransformer implements TransformerContract
{
public function transform(mixed $value): mixed
{
return is_string($value) ? trim($value) : $value;
}
}
final class CreateUserDto extends BaseDto
{
#[Transform(TrimTransformer::class)]
public readonly string $email;
}
public function wasProvided(string $property): bool;
/** @return array<string, mixed> */
public function providedAttributes(): array;
final class UpdateMailingDto extends BaseDto
{
#[DefaultValue('')]
public readonly string $subject;
#[MapProperty(from: 'reply_to', e — ключ не передан
$dto->wasProvided('reply_to_email'); // true — передан явный null
$dto->toArray();
// ['subject' => '', 'reply_to_email' => null]
$dto->providedAttributes();
// ['reply_to_email' => null]
$mailing->update($dto->providedAttributes());
use Mycro\Core\Contracts\BaseDto;
use Mycro\Core\Contracts\PropertyMapperInterface;
BaseDto::setPropertyMapper(new MyCustomPropertyMapper());
public function resolve(object $dto, ReflectionProperty $property, array $data): array
{
$name = $property->getName();
if (array_key_exists($name, $data)) {
return [true, $data[$name], true]; // из входных данных
}
return [true, null, false]; // подставлено маппером
}
use Mycro\Core\Logging\FileCoreLogger;
use Mycro\Core\Messaging\RabbitMQ\RabbitMQService;
$logger = new FileCoreLogger('/var/log/my-service/rabbit.log');
$rabbit = new RabbitMQService(
host: 'rabbitmq',
port: 5672,
user: 'guest',
password: 'guest',
logger: $logger,
);
// Публикация
$rabbit->publish(
exchange: 'users',
routingKey: 'user.registered',
data: ['user_id' => 42, 'email' => '[email protected]'],
queue: 'user_events', // опционально: объявить очередь и привязать к exchange
);
// Потребление (блокирующий цикл)
$rabbit->consume('user_events', function (array $payload): void {
// обработка $payload
});
use Mycro\Core\Messaging\RabbitMQ\RabbitMQConsumer;
$consumer = new RabbitMQConsumer($rabbit);
$consumer->listen('user_events', $handler);
public function publish(string $exchange, string $routingKey, array $data, ?string $queue = null): void;
public function error(string $message): void;
public function info(string $message): void;
public function debug(string $message): void;
$logger = new FileCoreLogger(storage_path('logs/mycro-core.log'));
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.