PHP code example of codewiser / casts

1. Go to this page and download the library: Download codewiser/casts 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/ */

    

codewiser / casts example snippets


use App\Collections\OptionCollection;
use Codewiser\Casts\AsObject;
use Illuminate\Database\Eloquent\Casts\AsCollection;

/**
 * Get the attributes that should be cast.
 *
 * @return array<string, string>
 */
protected function casts(): array
{
    return [
        'option'  => AsObject::of(Option::class),
        'options' => AsCollection::using(OptionCollection::class, Option::class),
    ];
}

use App\Collections\OptionCollection;
use Codewiser\Collections\EnumCollection;
use Illuminate\Database\Eloquent\Casts\AsCollection;

/**
 * Get the attributes that should be cast.
 *
 * @return array<string, string>
 */
protected function casts(): array
{
    return [
        'roles' => AsCollection::using(EnumCollection::class, RoleEnum::class),
    ];
}

class Article extends \Illuminate\Database\Eloquent\Model
{
    protected $casts = [
        'date' => 'datetime'
    ];
}

// e.g. Laravel has Europe/London (+01:00) timezone
config()->set('app.timezone', 'Europe/London');

$model = new Article();

$model->date = '2000-01-01T10:00:00+02:00';

echo $model->date->format('c');
// Expecting 2000-01-01T09:00:00+01:00
// Actual    2000-01-01T10:00:00+01:00

class Article extends \Illuminate\Database\Eloquent\Model
{
    protected $casts = [
        'date' => \Codewiser\Casts\AsDatetimeWithTZ::class
    ];
}

// e.g. Laravel has Europe/London (+01:00) timezone
config()->set('app.timezone', 'Europe/London');

$model = new Article();

$model->date = '2000-01-01T10:00:00+02:00';

echo $model->date->format('c');
// Expecting 2000-01-01T09:00:00+01:00
// Actual    2000-01-01T09:00:00+01:00

use Illuminate\Database\Eloquent\Relations\Relation;

class MyRequest extends FormRequest 
{
    public function rules(): array 
    {
        return [
            'commentable_type' => 'e_type'));
        
        return $class::find($this->integer('commentable_id'));
    }
}

use Codewiser\Requests\HasMorphs;
use Illuminate\Database\Eloquent\Relations\Relation;

class MyRequest extends FormRequest 
{
    use HasMorphs;

    public function rules(): array 
    {
        return [
            ...$this->morph('commentable')
        ];
    }
    
    public function getCommentable(): Model 
    {
        return $this->morphed('commentable');
    }
}

use Codewiser\Requests\HasMorphs;
use Illuminate\Database\Eloquent\Relations\Relation;

class MyRequest extends FormRequest 
{
    use HasMorphs;

    public function rules(): array 
    {
        return [
            ...$this->nullableMorph('commentable', [Post::class, Article::class])
        ];
    }
    
    public function hasCommentable(): bool {
        return $this->hasMorph('commentable');
    }
    
    public function getCommentable(): null|Post|Article 
    {
        return $this->morphed('commentable');
    }
}