Skip to content

Commit

Permalink
Merge pull request #1787 from damiankloip/psysh-caster-class
Browse files Browse the repository at this point in the history
Move caster callbacks into a Caster class
  • Loading branch information
damiankloip committed Nov 19, 2015
2 parents 1c7bd8e + 74f9b79 commit c133b55
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 43 deletions.
48 changes: 5 additions & 43 deletions commands/core/cli.drush.inc
Original file line number Diff line number Diff line change
Expand Up @@ -105,49 +105,11 @@ function _drush_core_cli_get_commands() {
*/
function _drush_core_cli_get_casters() {
return [
'Drupal\Core\Entity\ContentEntityInterface' => function($entity, $array, $stub, $isNested) {
if (!$isNested) {
foreach ($entity as $property => $item) {
$array[Symfony\Component\VarDumper\Caster\Caster::PREFIX_PROTECTED . $property] = $item;
}
}

return $array;
},
'Drupal\Core\Field\FieldItemListInterface' => function($list_item, $array, $stub, $isNested) {
if (!$isNested) {
foreach ($list_item as $delta => $item) {
$array[Symfony\Component\VarDumper\Caster\Caster::PREFIX_VIRTUAL . $delta] = $item;
}
}

return $array;
},
'Drupal\Core\Field\FieldItemInterface' => function($item, $array, $stub, $isNested) {
if (!$isNested) {
$array[Symfony\Component\VarDumper\Caster\Caster::PREFIX_VIRTUAL . 'value'] = $item->getValue();
}

return $array;
},
'Drupal\Core\Config\Entity\ConfigEntityInterface' => function($entity, $array, $stub, $isNested) {
if (!$isNested) {
foreach ($entity->toArray() as $property => $value) {
$array[Symfony\Component\VarDumper\Caster\Caster::PREFIX_PROTECTED . $property] = $value;
}
}

return $array;
},
'Drupal\Core\Config\ConfigBase' => function($config, $array, $stub, $isNested) {
if (!$isNested) {
foreach ($config->get() as $property => $value) {
$array[Symfony\Component\VarDumper\Caster\Caster::PREFIX_VIRTUAL . $property] = $value;
}
}

return $array;
},
'Drupal\Core\Entity\ContentEntityInterface' => 'Drush\Psysh\Caster::castContentEntity',
'Drupal\Core\Field\FieldItemListInterface' => 'Drush\Psysh\Caster::castFieldItemList',
'Drupal\Core\Field\FieldItemInterface' => 'Drush\Psysh\Caster::castFieldItem',
'Drupal\Core\Config\Entity\ConfigEntityInterface' => 'Drush\Psysh\Caster::castConfigEntity',
'Drupal\Core\Config\ConfigBase' => 'Drush\Psysh\Caster::castConfig',
];
}

Expand Down
80 changes: 80 additions & 0 deletions lib/Drush/Psysh/Caster.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

/**
* @file
* Contains \Drush\Psysh\Caster.
*/

namespace Drush\Psysh;

use Symfony\Component\VarDumper\Caster\Caster as BaseCaster;

/**
* Caster class for VarDumper casters for the shell.
*/
class Caster {

/**
* Casts \Drupal\Core\Entity\ContentEntityInterface classes.
*/
public static function castContentEntity($entity, $array, $stub, $isNested) {
if (!$isNested) {
foreach ($entity as $property => $item) {
$array[BaseCaster::PREFIX_PROTECTED . $property] = $item;
}
}

return $array;
}

/**
* Casts \Drupal\Core\Field\FieldItemListInterface classes.
*/
public static function castFieldItemList($list_item, $array, $stub, $isNested) {
if (!$isNested) {
foreach ($list_item as $delta => $item) {
$array[BaseCaster::PREFIX_VIRTUAL . $delta] = $item;
}
}

return $array;
}

/**
* Casts \Drupal\Core\Field\FieldItemInterface classes.
*/
public static function castFieldItem($item, $array, $stub, $isNested) {
if (!$isNested) {
$array[BaseCaster::PREFIX_VIRTUAL . 'value'] = $item->getValue();
}

return $array;
}

/**
* Casts \Drupal\Core\Config\Entity\ConfigEntityInterface classes.
*/
public static function castConfigEntity($entity, $array, $stub, $isNested) {
if (!$isNested) {
foreach ($entity->toArray() as $property => $value) {
$array[BaseCaster::PREFIX_PROTECTED . $property] = $value;
}
}

return $array;
}

/**
* Casts \Drupal\Core\Config\ConfigBase classes.
*/
public static function castConfig($config, $array, $stub, $isNested) {
if (!$isNested) {
foreach ($config->get() as $property => $value) {
$array[BaseCaster::PREFIX_VIRTUAL . $property] = $value;
}
}

return $array;
}

}

0 comments on commit c133b55

Please sign in to comment.