1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
<?php
namespace OneOfZero\Json\Helpers;
use InvalidArgumentException;
use stdClass;
class Metadata
{
const TYPE = '@type';
const ID = 'id';
public static function set(&$target, $metaType, $metaValue)
{
if ($target === null)
{
throw new InvalidArgumentException("Null provided as target for metadata storage");
}
if ($target instanceof stdClass)
{
$target->{$metaType} = $metaValue;
return;
}
if (is_array($target))
{
$target[$metaType] = $metaValue;
return;
}
throw new InvalidArgumentException("Metadata can only be stored in arrays and stdClass objects");
}
public static function contains(&$target, $metaType)
{
return $target !== null
&& (is_array($target) || $target instanceof stdClass)
&& array_key_exists($metaType, $target)
;
}
public static function get(&$target, $metaType)
{
if (!self::contains($target, $metaType))
{
return null;
}
$targetAsArray = (array)$target;
return $targetAsArray[$metaType];
}
}