<?php

namespace App\Media\QuickTime\Atoms;

// Movie sample data—media samples such as video frames and groups of audio samples.
// Usually this data can be interpreted only by using the movie resource.

use Illuminate\Support\Arr;

use App\Media\QuickTime\Atom;

class mdat extends Atom
{
	/**
	 * Calculate the signature of the data
	 *
	 * @param string $alg
	 * @return string
	 */
	public function signature(string $alg='sha1'): string
	{
		if (! Arr::has($this->cache,'signature')) {
			if ($this->size) {
				$this->fopen();

				$hash = hash_init($alg);

				while (!is_null($read = $this->fread(16384)))
					hash_update($hash, $read);

				$this->fclose();

				$this->cache['signature'] = hash_final($hash);

			} else {
				$this->cache['signature'] = NULL;
			}
		}

		return $this->cache['signature'];
	}
}