public function testSizeCallback() { $field = new Int(function () { return 6; }); $this->assertEquals(6, $field->getSize()); }
public function testValueCallback() { $stream = new StringStream('123abcdefgqwertyahnytjssdadfkjhb'); $field = new Int('byte', ['formatter' => function ($value) { return $value - 2 * 8; }]); $this->assertEquals(33, $field->parse($stream)); }
/** * Read key from Stream, and return value by this key or default value. * * @param AbstractStream $stream Stream from which resolved field reads. * @return object|integer|double|string|array|boolean|callable Value by read key or default value if present. * @throws InvalidKeyException If read key is not exist and default value is not presented. */ public function read(AbstractStream $stream) { $key = parent::read($stream); $values = $this->getValues(); if (array_key_exists($key, $values)) { $value = $values[$key]; } else { $value = $this->getDefault(); } if ($value === null) { throw new InvalidKeyException("Value '{$key}' does not correspond to a valid enum key. Presented keys: '" . implode("', '", array_keys($values)) . "'"); } return $value; }