|
| 1 | +<?php |
| 2 | + |
| 3 | +// Copyright 2025 DeepL SE (https://www.deepl.com) |
| 4 | +// Use of this source code is governed by an MIT |
| 5 | +// license that can be found in the LICENSE file. |
| 6 | + |
| 7 | +namespace DeepL; |
| 8 | + |
| 9 | +use DateTime; |
| 10 | +use JsonException; |
| 11 | + |
| 12 | +/** |
| 13 | + * Information about a style rule list. |
| 14 | + */ |
| 15 | +class StyleRuleInfo |
| 16 | +{ |
| 17 | + /** @var string Unique ID assigned to the style rule list. */ |
| 18 | + public $styleId; |
| 19 | + |
| 20 | + /** @var string User-defined name assigned to the style rule list. */ |
| 21 | + public $name; |
| 22 | + |
| 23 | + /** @var DateTime Timestamp when the style rule list was created. */ |
| 24 | + public $creationTime; |
| 25 | + |
| 26 | + /** @var DateTime Timestamp when the style rule list was last updated. */ |
| 27 | + public $updatedTime; |
| 28 | + |
| 29 | + /** @var string Language code for the style rule list. */ |
| 30 | + public $language; |
| 31 | + |
| 32 | + /** @var int Version number of the style rule list. */ |
| 33 | + public $version; |
| 34 | + |
| 35 | + /** @var ConfiguredRules|null The predefined rules that have been enabled. */ |
| 36 | + public $configuredRules; |
| 37 | + |
| 38 | + /** @var CustomInstruction[]|null Optional list of custom instructions. */ |
| 39 | + public $customInstructions; |
| 40 | + |
| 41 | + public function __construct( |
| 42 | + string $styleId, |
| 43 | + string $name, |
| 44 | + DateTime $creationTime, |
| 45 | + DateTime $updatedTime, |
| 46 | + string $language, |
| 47 | + int $version, |
| 48 | + ?ConfiguredRules $configuredRules = null, |
| 49 | + ?array $customInstructions = null |
| 50 | + ) { |
| 51 | + $this->styleId = $styleId; |
| 52 | + $this->name = $name; |
| 53 | + $this->creationTime = $creationTime; |
| 54 | + $this->updatedTime = $updatedTime; |
| 55 | + $this->language = $language; |
| 56 | + $this->version = $version; |
| 57 | + $this->configuredRules = $configuredRules; |
| 58 | + $this->customInstructions = $customInstructions; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @param string|StyleRuleInfo $styleRule Style rule ID or StyleRuleInfo of style rule. |
| 63 | + */ |
| 64 | + public static function getStyleId($styleRule): string |
| 65 | + { |
| 66 | + return is_string($styleRule) ? $styleRule : $styleRule->styleId; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * @throws InvalidContentException |
| 71 | + */ |
| 72 | + public static function fromJson(array $json): StyleRuleInfo |
| 73 | + { |
| 74 | + $configuredRules = null; |
| 75 | + if (isset($json['configured_rules'])) { |
| 76 | + $configuredRules = ConfiguredRules::fromJson($json['configured_rules']); |
| 77 | + } |
| 78 | + |
| 79 | + $customInstructions = null; |
| 80 | + if (isset($json['custom_instructions']) && is_array($json['custom_instructions'])) { |
| 81 | + $customInstructions = []; |
| 82 | + foreach ($json['custom_instructions'] as $instruction) { |
| 83 | + $customInstructions[] = CustomInstruction::fromJson($instruction); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + return new StyleRuleInfo( |
| 88 | + $json['style_id'], |
| 89 | + $json['name'], |
| 90 | + new DateTime($json['creation_time']), |
| 91 | + new DateTime($json['updated_time']), |
| 92 | + $json['language'], |
| 93 | + $json['version'], |
| 94 | + $configuredRules, |
| 95 | + $customInstructions |
| 96 | + ); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * @throws InvalidContentException |
| 101 | + */ |
| 102 | + public static function parseList(string $content): array |
| 103 | + { |
| 104 | + try { |
| 105 | + $decoded = json_decode($content, true, 512, \JSON_THROW_ON_ERROR); |
| 106 | + } catch (JsonException $exception) { |
| 107 | + throw new InvalidContentException($exception); |
| 108 | + } |
| 109 | + |
| 110 | + $result = []; |
| 111 | + $styleRules = $decoded['style_rules'] ?? []; |
| 112 | + foreach ($styleRules as $object) { |
| 113 | + $result[] = self::fromJson($object); |
| 114 | + } |
| 115 | + return $result; |
| 116 | + } |
| 117 | + |
| 118 | + public function __toString(): string |
| 119 | + { |
| 120 | + return "StyleRule \"{$this->name}\" ({$this->styleId})"; |
| 121 | + } |
| 122 | +} |
0 commit comments