MessagePack\Packer::pack PHP Метод

pack() публичный Метод

public pack ( $value )
    public function pack($value)
    {
        if (\is_int($value)) {
            return $this->packInt($value);
        }
        if (\is_string($value)) {
            if (!$this->strDetectionMode) {
                return \preg_match(self::UTF8_REGEX, $value) ? $this->packStr($value) : $this->packBin($value);
            }
            if (self::FORCE_STR === $this->strDetectionMode) {
                return $this->packStr($value);
            }
            return $this->packBin($value);
        }
        if (\is_array($value)) {
            if (!$this->arrDetectionMode) {
                return \array_values($value) === $value ? $this->packArray($value) : $this->packMap($value);
            }
            if (self::FORCE_ARR === $this->arrDetectionMode) {
                return $this->packArray($value);
            }
            return $this->packMap($value);
        }
        if (null === $value) {
            return $this->packNil();
        }
        if (\is_bool($value)) {
            return $this->packBool($value);
        }
        if (\is_double($value)) {
            return $this->packFloat($value);
        }
        if ($value instanceof Ext) {
            return $this->packExt($value);
        }
        if ($this->transformers && ($transformer = $this->transformers->match($value))) {
            $ext = new Ext($transformer->getId(), $this->pack($transformer->transform($value)));
            return $this->packExt($ext);
        }
        throw new PackingFailedException($value, 'Unsupported type.');
    }

Usage Example

Пример #1
0
 public function testPackCustomType()
 {
     $obj = new \stdClass();
     $transformer = $this->getTransformerMock(5);
     $transformer->expects($this->once())->method('transform')->willReturn(1);
     $coll = $this->getTransformerCollectionMock([$transformer]);
     $coll->expects($this->once())->method('match')->with($obj);
     $this->packer->setTransformers($coll);
     $this->assertSame("�", $this->packer->pack($obj));
 }
All Usage Examples Of MessagePack\Packer::pack