Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
TypeCaster.php
Go to the documentation of this file.
1 <?php
7 
9 
14 {
18  private $serializer;
19 
23  public function __construct(Json $serializer)
24  {
25  $this->serializer = $serializer;
26  }
27 
37  public function castValueToType($value, $type)
38  {
39  if ($value === null) {
40  return null;
41  }
42 
49  if (is_array($value) && !interface_exists($type) && !class_exists($type)) {
50  return $this->serializer->serialize($value);
51  }
52 
53  if ($type === "int" || $type === "integer") {
54  return (int)$value;
55  }
56 
57  if ($type === "string") {
58  return (string)$value;
59  }
60 
61  if ($type === "bool" || $type === "boolean" || $type === "true" || $type == "false") {
62  return (bool)$value;
63  }
64 
65  if ($type === "float") {
66  return (float)$value;
67  }
68 
69  if ($type === "double") {
70  return (double)$value;
71  }
72 
73  return $value;
74  }
75 }
$type
Definition: item.phtml:13
$value
Definition: gender.phtml:16