{"record":{"id":"b2e2fc60ba311341","repo":"PHPOffice/PHPWord","slug":"invalid-style-value-value","errorCode":null,"errorMessage":"Invalid style value: {$value}","messagePattern":"Invalid style value: (.+?)","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/PhpWord/Element/AbstractElement.php","lineNumber":537,"sourceCode":"    {\n        $this->trackChange = new TrackChange($type, $author, $date);\n    }\n\n    /**\n     * Set enum value.\n     *\n     * @param null|string $value\n     * @param string[] $enum\n     * @param null|string $default\n     *\n     * @return null|string\n     *\n     * @todo Merge with the same method in AbstractStyle\n     */\n    protected function setEnumVal($value = null, $enum = [], $default = null)\n    {\n        if ($value !== null && trim($value) != '' && !empty($enum) && !in_array($value, $enum)) {\n            throw new InvalidArgumentException(\"Invalid style value: {$value}\");\n        } elseif ($value === null || trim($value) == '') {\n            $value = $default;\n        }\n\n        return $value;\n    }\n}\n","sourceCodeStart":519,"sourceCodeEnd":545,"githubUrl":"https://github.com/PHPOffice/PHPWord/blob/aef95c04151b5633cc505f672ddd6a71da900ee1/src/PhpWord/Element/AbstractElement.php#L519-L545","documentation":"AbstractElement::setEnumVal() is the shared enum validator for element style setters (setType, setVAlign, setTextDirection, setVMerge, setUnit, setLegendPosition). If a non-null, non-empty value is given that is not in the allowed enum array, it throws an InvalidArgumentException with 'Invalid style value: {value}'. Null/empty values fall back to the provided default instead of throwing.","triggerScenarios":"Calling a style setter like $table->setStyleByArgument / $image->setImageStyle or direct calls such as $cell->setVAlign('middle') when only 'center'/'top'/'bottom' etc. are allowed; setting setType('x') on ListRun/Table styles with wrong values; passing style arrays with invalid keys' values via constructor.","commonSituations":"Using CSS-like values ('middle' vs 'center') in vAlign; passing integers for Unit where string constants are expected (or vice versa); values ported from raw OOXML attributes with different casing; typo in a style array.","solutions":["Use the class constants for the style (e.g. Cell::VALIGN_CENTER, Image::UNIT_PX) instead of free-form strings","Check the exact enum values accepted by the specific setter in the element class","Match casing exactly — the in_array check is case-sensitive","Let the value be null/empty to get the setter's default instead of guessing"],"exampleFix":"// before\n$cell->setVAlign('middle');\n// after\nuse PhpOffice\\PhpWord\\SimpleType\\VerticalJc;\n$cell->setVAlign(VerticalJc::CENTER);","handlingStrategy":"validation","validationCode":"use PhpOffice\\PhpWord\\SimpleType\\VerticalJc;\n$allowed = [VerticalJc::TOP, VerticalJc::CENTER, VerticalJc::BOTTOM];\nif ($value !== null && trim($value) !== '' && !in_array($value, $allowed, true)) {\n    throw new UnexpectedValueException(\"vAlign must be one of: \" . implode(', ', $allowed));\n}","typeGuard":"function isEnumValue(?string $value, array $enum): bool {\n    return $value === null || trim($value) === '' || in_array($value, $enum, true);\n}","tryCatchPattern":"try {\n    $cell->setVAlign($value);\n} catch (\\InvalidArgumentException $e) {\n    // leave unset so the library default applies\n}","preventionTips":["Use the SimpleType constant classes (VerticalJc, TextDirection, etc.) instead of raw strings","Remember the check is case-sensitive — match constants exactly","Pass null/empty to accept the setter default rather than guessing a value","Keep a mapping layer between user-facing config keys and PhpWord constants"],"tags":["php","phpword","style","enum"],"backgroundTag":"invalid-enum-value","analyzedSha":"aef95c04151b5633cc505f672ddd6a71da900ee1","analyzedAt":"2026-09-14T10:53:58.933Z","contentChangedAt":"2026-09-14T10:53:58.933Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}