HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/helder.komma.pro/app/Komma/Kms/Core/Attributes/CodeSnippet.php
<?php
namespace App\Komma\Kms\Core\Attributes;
use App\Komma\Kms\Core\Attributes\Traits\ExplanationTrait;
use App\Komma\Kms\Core\Attributes\Traits\LabelTrait;
use App\Komma\Kms\Core\Attributes\Traits\PlaceholderTextTrait;
use App\Komma\Kms\Core\Attributes\Traits\ReadOnlyTrait;
use Illuminate\Support\Facades\File;

/**
 * Class TextField
 * @package App\Komma\Kms\Core\Attributes
 */
class CodeSnippet extends Attribute
{
    use LabelTrait;
    use PlaceholderTextTrait;
    use ExplanationTrait;

    private $filePathFormat;

    private $filePath;

    /** @var string $pattern A javascript regex pattern that is used to define how the text field should be entered. Example [A-Za-z]{3}. Documentation @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions */
    private $pattern;

    /**
     * TextField constructor.
     * @param string $labelText
     */
    public function __construct(string $labelText)
    {
        $this->setLabelText($labelText);

        parent::__construct();
    }

    /**
     * Returns a view that visually represents this attribute
     */
    public function render()
    {
        return \View::make('kms/attributes.codeSnippet', [
            'attribute' => $this
        ]);
    }

    /**
     * @return string
     */
    public function getPattern(): ?string
    {
        return $this->pattern;
    }

    /**
     * @param string $pattern
     * @return CodeSnippet
     */
    public function setPattern(string $pattern): CodeSnippet
    {
        $this->pattern = $pattern;
        return $this;
    }

    /**
     * @param string $filePathFormat
     * @return CodeSnippet
     */
    public function setFilePathFormat(string $filePathFormat) : CodeSnippet
    {
        if( ! str_contains($filePathFormat,'{value}'))
            throw new \RuntimeException('The file path format needs to contain the string "{value}", this is wat gets replaced with the actual value');

        $this->filePathFormat = $filePathFormat;
        return $this;
    }

    /**
     * @return string The value associated with the attribute
     */
    public function getValue(): string
    {
        return $this->value;
    }

    /**
     * @return string The contents of the file associated with the attribute
     */
    public function getFileContent(): string
    {
        // Define path
        $path = base_path(str_replace('{value}',$this->value,$this->filePathFormat));

        // Todo: move to validation
        if( ! File::exists($path)) return 'file "' . $path . '" doesn\'t exist';

        // Get and prepare content
        $content = File::get($path);
        $content = trim($content);
        $content = str_replace('<','&lt;',$content);
        $content = str_replace('>','&gt;',$content);

        return $content;
    }

    /**
     * @param $value
     * @return \Illuminate\Support\Collection
     */
    public function prepareValueForViewComponent($value)
    {
        // Set value (for getFileContent)
        $this->setValue($value);

        // We need to different values
        return collect([
           'path' => $value,
           'fileContent' => $this->getFileContent(),
        ]);
    }
}