Commits

Thomas Weinert committed 7879329

Added some array handling to the ByteArray

Comments (0)

Files changed (2)

src/Carica/Io/ByteArray.php

     }
 
     /**
+     * Read an hexadecimal encoded binary string
+     *
+     * @param string $string
+     * @param boolean $resize
+     * @throws \OutOfBoundsException
+     */
+    public function fromArray(array $bytes, $resize = FALSE) {
+      $length = count($bytes);
+      if ($resize && $length != $this->getLength()) {
+        $this->setLength($length);
+      }
+      if ($length >= $this->_length) {
+        foreach (array_values($bytes) as $index => $byte) {
+          $this->_bytes[$index] = $byte;
+        }
+      } else {
+        throw new \OutOfBoundsException(
+          sprintf(
+            'Maximum length is "%d". Got "%d".', $this->_length, $length
+          )
+        );
+      }
+    }
+
+    /**
      * Get the byte array as an hexdec string.
      *
      * @return string
       return count($this->_bytes);
     }
 
+    /**
+     * Create an ByteArray from an hexadecimal byte string
+     * @param string $hexString
+     * @return \Carica\Io\ByteArray
+     */
     public static function createFromHex($hexString) {
       $bytes = new ByteArray();
       $bytes->fromHexString($hexString, TRUE);
       return $bytes;
     }
+
+    /**
+     * Create a new ByteArray from an array of bytes
+     *
+     * @param array $bytes
+     * @return \Carica\Io\ByteArray
+     */
+    public static function createFromArray(array $array) {
+      $bytes = new ByteArray();
+      $bytes->fromArray($array, TRUE);
+      return $bytes;
+    }
   }
 }

tests/Carica/Io/ByteArrayTest.php

     }
 
     /**
+     * @covers Carica\Io\ByteArray::fromArray
+     */
+    public function testFromArray() {
+      $bytes = new ByteArray(2);
+      $bytes->fromArray([0x00, 0xFF]);
+      $this->assertEquals('00ff', $bytes->asHex());
+    }
+
+    /**
+     * @covers Carica\Io\ByteArray::fromArray
+     */
+    public function testFromArrayWithInvalidLengthExpectingException() {
+      $bytes = new ByteArray(42);
+      $this->setExpectedException('OutOfBoundsException');
+      $bytes->fromArray([0xFF, 0xF0, 0xFF], FALSE);
+    }
+
+    /**
+     * @covers Carica\Io\ByteArray::fromArray
+     */
+    public function testFromArrayWithAutomaticLengthIncrease() {
+      $bytes = new ByteArray(1);
+      $bytes->fromArray([0xFF, 0xF0, 0xFF], TRUE);
+      $this->assertSame('11111111 11110000 11111111', $bytes->asBitString());
+    }
+
+    /**
+     * @covers Carica\Io\ByteArray::fromArray
+     */
+    public function testFromArrayWithAutomaticLengthDecrease() {
+      $bytes = new ByteArray(10);
+      $bytes->fromArray([0xFF, 0xF0, 0xFF], TRUE);
+      $this->assertSame('11111111 11110000 11111111', $bytes->asBitString());
+    }
+
+    /**
      * @covers Carica\Io\ByteArray::asHex
      * @dataProvider provideHexSamples
      */
       );
     }
 
+    /**
+     * @covers Carica\Io\ByteArray::createFromArray
+     */
+    public function testCreateFromArray() {
+      $bytes = ByteArray::createFromArray([0x00, 0xF0, 0xFF]);
+      $this->assertEquals(
+        "\x00\xF0\xFF", (string)$bytes
+      );
+    }
+
     /**************************
      * Data Provider
      *************************/