Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Package.php
Go to the documentation of this file.
1 <?php
8 
14 class Package
15 {
21  protected $json;
22 
28  public function __construct(\stdClass $json)
29  {
30  $this->json = $json;
31  }
32 
40  public function getJson($formatted = true, $format = null)
41  {
42  if ($formatted) {
43  if (null === $format) {
44  $format = JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES;
45  }
46  return json_encode($this->json, $format) . "\n";
47  }
48  return $this->json;
49  }
50 
66  public function get($propertyPath, $filter = null)
67  {
68  $result = $this->traverseGet($this->json, explode('->', $propertyPath));
69  if ($result && $filter) {
70  foreach ($result as $key => $value) {
71  if (!preg_match($filter, $key)) {
72  unset($result->{$key});
73  }
74  }
75  }
76  return $result;
77  }
78 
87  private function traverseGet(\StdClass $json, array $chain, $index = 0)
88  {
89  $property = $chain[$index];
90  if (!property_exists($json, $property)) {
91  return false;
92  }
93  if (isset($chain[$index + 1])) {
94  return $this->traverseGet($json->{$property}, $chain, $index + 1);
95  } else {
96  return $json->{$property};
97  }
98  }
99 }
getJson($formatted=true, $format=null)
Definition: Package.php:40
$value
Definition: gender.phtml:16
$format
Definition: list.phtml:12
$index
Definition: list.phtml:44