Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.4] Add Caster Classes #16460

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions src/Illuminate/Database/Eloquent/Casters/AbstractCaster.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Illuminate\Database\Eloquent\Casters;

abstract class AbstractCaster
{
/**
* The caster options.
*
* @var array
*/
protected $options;

/**
* Set the caster options.
*
* @param array $options
*
* @return \Illuminate\Database\Eloquent\Casters\AbstractCaster
*/
public function options(array $options)
{
$this->options = $options;

return $this;
}

/**
* Prepare a value to be stored.
*
* @param mixed $value
*
* @return mixed
*/
abstract public function as($value);

/**
* Prepare a value to be retrieved.
*
* @param mixed $value
*
* @return mixed
*/
abstract public function from($value);
}
30 changes: 30 additions & 0 deletions src/Illuminate/Database/Eloquent/Casters/ArrayCaster.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Illuminate\Database\Eloquent\Casters;

class ArrayCaster extends AbstractCaster
{
/**
* Encode the given value as JSON.
*
* @param mixed $value
*
* @return string
*/
public function as($value)
{
return json_encode($value);
}

/**
* Decode the given JSON back into an array or object.
*
* @param string $value
*
* @return mixed
*/
public function from($value)
{
return json_decode($value, $this->options->asObject);
}
}
22 changes: 22 additions & 0 deletions src/Illuminate/Database/Eloquent/Casters/BooleanCaster.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Illuminate\Database\Eloquent\Casters;

class BooleanCaster extends AbstractCaster
{
/**
* {@inheritdoc}
*/
public function as($value)
{
return $value;
}

/**
* {@inheritdoc}
*/
public function from($value)
{
return (bool) $value;
}
}
24 changes: 24 additions & 0 deletions src/Illuminate/Database/Eloquent/Casters/CollectionCaster.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Illuminate\Database\Eloquent\Casters;

use Illuminate\Support\Collection;

class CollectionCaster extends AbstractCaster
{
/**
* {@inheritdoc}
*/
public function as($value)
{
return $value;
}

/**
* {@inheritdoc}
*/
public function from($value)
{
return new Collection(json_decode($value));
}
}
57 changes: 57 additions & 0 deletions src/Illuminate/Database/Eloquent/Casters/DateTimeCaster.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Illuminate\Database\Eloquent\Casters;

use Carbon\Carbon;

class DateTimeCaster extends AbstractCaster
{
/**
* {@inheritdoc}
*/
public function as($value)
{
// If this value is already a Carbon instance, we shall just return it as is.
// This prevents us having to re-instantiate a Carbon instance when we know
// it already is one, which wouldn't be fulfilled by the DateTime check.
if ($value instanceof Carbon) {
return $value;
}

// If the value is already a DateTime instance, we will just skip the rest of
// these checks since they will be a waste of time, and hinder performance
// when checking the field. We will just return the DateTime right away.
if ($value instanceof DateTimeInterface) {
return new Carbon(
$value->format('Y-m-d H:i:s.u'), $value->getTimeZone()
);
}

// If this value is an integer, we will assume it is a UNIX timestamp's value
// and format a Carbon object from this timestamp. This allows flexibility
// when defining your date fields as they might be UNIX timestamps here.
if (is_numeric($value)) {
return Carbon::createFromTimestamp($value);
}

// If the value is in simply year, month, day format, we will instantiate the
// Carbon instances from that format. Again, this provides for simple date
// fields on the database, while still supporting Carbonized conversion.
if (preg_match('/^(\d{4})-(\d{1,2})-(\d{1,2})$/', $value)) {
return Carbon::createFromFormat('Y-m-d', $value)->startOfDay();
}

// Finally, we will just assume this date is in the format used by default on
// the database connection and use that format to create the Carbon object
// that is returned back out to the developers after we convert it here.
return Carbon::createFromFormat($this->getDateFormat(), $value);
}

/**
* {@inheritdoc}
*/
public function from($value)
{
return $this->as($value)->format($this->options['format']);
}
}
22 changes: 22 additions & 0 deletions src/Illuminate/Database/Eloquent/Casters/EncryptedCaster.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Illuminate\Database\Eloquent\Casters;

class EncryptedCaster extends AbstractCaster
{
/**
* {@inheritdoc}
*/
public function as($value)
{
return encrypt($value);
}

/**
* {@inheritdoc}
*/
public function from($value)
{
return decrypt($value);
}
}
22 changes: 22 additions & 0 deletions src/Illuminate/Database/Eloquent/Casters/FloatCaster.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Illuminate\Database\Eloquent\Casters;

class FloatCaster extends AbstractCaster
{
/**
* {@inheritdoc}
*/
public function as($value)
{
return $value;
}

/**
* {@inheritdoc}
*/
public function from($value)
{
return (float) $value;
}
}
22 changes: 22 additions & 0 deletions src/Illuminate/Database/Eloquent/Casters/IntegerCaster.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Illuminate\Database\Eloquent\Casters;

class IntegerCaster extends AbstractCaster
{
/**
* {@inheritdoc}
*/
public function as($value)
{
return $value;
}

/**
* {@inheritdoc}
*/
public function from($value)
{
return (int) $value;
}
}
22 changes: 22 additions & 0 deletions src/Illuminate/Database/Eloquent/Casters/ObjectCaster.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Illuminate\Database\Eloquent\Casters;

class ObjectCaster extends AbstractCaster
{
/**
* {@inheritdoc}
*/
public function as($value)
{
return $value;
}

/**
* {@inheritdoc}
*/
public function from($value)
{
return json_decode($value, true);
}
}
22 changes: 22 additions & 0 deletions src/Illuminate/Database/Eloquent/Casters/StringCaster.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Illuminate\Database\Eloquent\Casters;

class StringCaster extends AbstractCaster
{
/**
* {@inheritdoc}
*/
public function as($value)
{
return $value;
}

/**
* {@inheritdoc}
*/
public function from($value)
{
return (string) $value;
}
}
24 changes: 24 additions & 0 deletions src/Illuminate/Database/Eloquent/Casters/TimestampCaster.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Illuminate\Database\Eloquent\Casters;

class TimestampCaster extends AbstractCaster
{
/**
* {@inheritdoc}
*/
public function as($value)
{
return $value;
}

/**
* {@inheritdoc}
*/
public function from($value)
{
return (new DateTimeCaster())->options($this->options)
->as($value)
->getTimestamp();
}
}
Loading