PHP code example of guillermo-rod / convert-string-to-file

1. Go to this page and download the library: Download guillermo-rod/convert-string-to-file 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/ */

    

guillermo-rod / convert-string-to-file example snippets


    class Product extends Model {
        
        use \GuillermoRod\StringToFile\ConvertsStringToFile;

        /**
         * Return the model properties configuration array for create the files for this model.
         *
         * @return array
         */
        public function convertStringToFile(): array
        {
            return ['description', 'details', 'data_sheet'];
        }
    }

    // .. Some controller method
    $product   = Product::with('string_files')->first();
    $attribute = 'details';

    // Determine if the file was created
    $product->hasStringFile($attribute); // : bool

    // Get the model instance that belongs to the attribute,
    // Inside of model there are more methods
    $product->getStringFile($attribute); // : \GuillermoRod\StringToFile\Models\StringToFile

    // If the file extension is css or js, get the url
    $product->getStringFileUrlAsset($attribute); // : string

    // If the file extension is html, get view path
    $product->getStringFileViewPath($attribute); // : string

    // Get the file contents
    $product->getStringFileContents($attribute); // : string

    class Product extends Model {
        
        use \GuillermoRod\StringToFile\ConvertsStringToFile;

        /**
         * Return the model properties configuration array for create the files for this model.
         *
         * @return array
         */
        public function convertStringToFile(): array
        {
            return [
                'description' => [
                    'extension' => \GuillermoRod\StringToFile\Models\StringToFile::HTML_EXTENSION
                ], 
                'styles' => [                    
                    'extension' => \GuillermoRod\StringToFile\Models\StringToFile::CSS_EXTENSION
                ], 
                'details' => [                    
                    'extension' => \GuillermoRod\StringToFile\Models\StringToFile::JS_EXTENSION
                ], 
                'data_sheet' => [                    
                    'extension' => \GuillermoRod\StringToFile\Models\StringToFile::TXT_EXTENSION
                ],
            ];
        }
    }
    

    $product = Product::first();

    // create or update content
    $product->details = '<h1>New content</h1>';
    $product->save();

    // Updating model but not affect to the file content, No changes
    $product->some_another_field = 'product_1';
    $product->save();

    // Deleting file
    $product->details = null;
    $product->save();


    $newProduct = new Product;
    $newProduct->details = '<h1>Hello</h1>';
    $newProduct->save();

    // .. Some controller method
    $product = Product::with('string_files')->first();

    return view('test', compact('product')),

    //.. test.blade.php    

    /**
     * @param \Illuminate\Database\Eloquent\Model $model
     * @param string $attribute
     * @param string $richText - default value to show if file not exists
     */
    @ext - default value to show if file not exists
     * @param string $mergeElementAttributes defer, fingerprint="" etc..
     */
    @ction some() {/* ... */}
        </script>
    @endif

    // Define manually styles 
    @if ($product->hasStringFile('details'))
        <link src="{{ $product->getStringFileUrlAsset('details') }}">
    @else 
        <style>
            .some-class {/* ... */}
        </style>
    @endif

    //Define manually content 
    @if ($product->hasStringFile('details'))
        {{ $product->getStringFileContents() }}
    @else 
        {{-- ... some --}}
    @endif