123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- <?php
- namespace App\Services\NamingRule;
- use App\Models\Asset;
- use App\Models\CustomField;
- use App\Models\Enums\CustomFieldType;
- use App\Models\Enums\FileObjectType;
- use App\Models\Enums\LibraryType;
- use App\Models\Library;
- use App\Models\NamingRule;
- use App\Models\Project;
- use Illuminate\Database\Eloquent\Collection;
- class NamingRuleCheck
- {
- protected ?Collection $customFieldsKeyBy = null;
- public function __construct(
- protected ?FileObjectType $fileObjectType = null,
- protected ?object $object = null
- )
- {
- if ($this->object!=null&&$this->object->naming_rule_id) {
- $this->loadCustomFields($this->object->naming_rule_id);
- }
- }
- public function checkByFileName(string $fileName)
- {
- if (! $this->object || ! $this->object->namingRule || !$this->fileObjectType != FileObjectType::CONTAINER) {
- return;
- }
- $fileName = pathinfo($fileName)['filename'];
- $result = $this->parse($fileName, $this->getNamingRuleCode($this->object->library), $this->object->namingRule->combination_rules);
- throw_validation_if(! $result, sprintf("'%s' does not conform to the naming rules", $fileName));
- }
- public function checkByName(string $name, $namingRule, ?string $code): array
- {
- $data = [
- 'result' => false,
- 'fields' => [],
- ];
- if (! $code) {
- return $data;
- }
- $this->loadCustomFields($namingRule->id);
- $result = $this->parse($name, $code, $namingRule->combination_rules);
- if ($result) {
- $data['result'] = true;
- $data['fields'] = $result;
- }
- return $data;
- }
- protected function parse(string $name, string $code, array $rules): bool|array
- {
- if (!str_starts_with($name, $code)) {
- return false;
- }
- $remainingString = substr($name, strlen($code));
- $items = [];
- foreach ($rules as $rule) {
- $expectedLink = $rule['link']; // 期望的连接符
- if (!str_starts_with($remainingString, $expectedLink)) {
- return false;
- }
- $remainingString = substr($remainingString, strlen($expectedLink));
- $nextLinkPos = false;
- foreach ($rules as $r) {
- $pos = strpos($remainingString, $r['link']);
- if ($pos !== false && $pos > 0) {
- $nextLinkPos = $pos;
- break;
- }
- }
- if ($nextLinkPos !== false) {
- $fieldValue = substr($remainingString, 0, $nextLinkPos);
- $remainingString = substr($remainingString, $nextLinkPos);
- } else {
- $fieldValue = $remainingString;
- $remainingString = '';
- }
- $customField = $this->customFieldsKeyBy[$rule['field']];
- if ($customField->type == CustomFieldType::SELECT->value) {
- if (! in_array($fieldValue, array_column($customField->options, "value"))) {
- return false;
- }
- }
- $items[$customField->key] = $fieldValue;
- }
- return $items;
- }
- public function getNamingRuleCode(Library $library)
- {
- $code = match (LibraryType::tryFrom($library->type)) {
- LibraryType::PROJECT => Project::query()->find($library->project_id)?->code,
- LibraryType::ASSET => Asset::query()->find($library->asset_id)?->code,
- LibraryType::CUSTOM => "custom"
- };
- throw_validation_if(! $code, "The supported type or selected item does not exist");
- return $code;
- }
- protected function loadCustomFields(int $namingRuleId): void
- {
- $this->customFieldsKeyBy = CustomField::query()
- ->where("group", $namingRuleId)
- ->get()
- ->keyBy("key");
- }
- }
|