ReplaceArrayValue.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\helpers;
  8. use yii\base\InvalidConfigException;
  9. /**
  10. * Object that represents the replacement of array value while performing [[ArrayHelper::merge()]].
  11. *
  12. * Usage example:
  13. *
  14. * ```php
  15. * $array1 = [
  16. * 'ids' => [
  17. * 1,
  18. * ],
  19. * 'validDomains' => [
  20. * 'example.com',
  21. * 'www.example.com',
  22. * ],
  23. * ];
  24. *
  25. * $array2 = [
  26. * 'ids' => [
  27. * 2,
  28. * ],
  29. * 'validDomains' => new \yii\helpers\ReplaceArrayValue([
  30. * 'yiiframework.com',
  31. * 'www.yiiframework.com',
  32. * ]),
  33. * ];
  34. *
  35. * $result = \yii\helpers\ArrayHelper::merge($array1, $array2);
  36. * ```
  37. *
  38. * The result will be
  39. *
  40. * ```php
  41. * [
  42. * 'ids' => [
  43. * 1,
  44. * 2,
  45. * ],
  46. * 'validDomains' => [
  47. * 'yiiframework.com',
  48. * 'www.yiiframework.com',
  49. * ],
  50. * ]
  51. * ```
  52. *
  53. * @author Robert Korulczyk <robert@korulczyk.pl>
  54. * @since 2.0.10
  55. */
  56. class ReplaceArrayValue
  57. {
  58. /**
  59. * @var mixed value used as replacement.
  60. */
  61. public $value;
  62. /**
  63. * Constructor.
  64. * @param mixed $value value used as replacement.
  65. */
  66. public function __construct($value)
  67. {
  68. $this->value = $value;
  69. }
  70. /**
  71. * Restores class state after using `var_export()`.
  72. *
  73. * @param array $state
  74. * @return ReplaceArrayValue
  75. * @throws InvalidConfigException when $state property does not contain `value` parameter
  76. * @see var_export()
  77. * @since 2.0.16
  78. */
  79. public static function __set_state($state)
  80. {
  81. if (!isset($state['value'])) {
  82. throw new InvalidConfigException('Failed to instantiate class "Instance". Required parameter "id" is missing');
  83. }
  84. return new self($state['value']);
  85. }
  86. }