/** * @param mixed $value * * @return string * * @throws \Spatie\BladeJavaScript\Exceptions\Untransformable */ public function transform($value) : string { if (method_exists($value, 'toJson')) { return $value->toJson(); } if ($value instanceof JsonSerializable || $value instanceof StdClass) { return json_encode($value); } if (!method_exists($value, '__toString')) { throw Untransformable::cannotTransformObject($value); } return "'{$value}'"; }
/** * @param mixed $value * * @return \Spatie\BladeJavaScript\Transformers\Transformer * * @throws \Spatie\BladeJavaScript\Exceptions\Untransformable */ public function getTransformer($value) : Transformer { foreach ($this->getAllTransformers() as $transformer) { if ($transformer->canTransform($value)) { return $transformer; } } throw Untransformable::noTransformerFound($value); }