Skip to content

Commit

Permalink
feat(view): add new methods includeFirst and fetchFirst
Browse files Browse the repository at this point in the history
  • Loading branch information
Awilum committed Feb 5, 2022
1 parent 2d5a0d3 commit f31e9f0
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 70 deletions.
207 changes: 137 additions & 70 deletions src/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use BadMethodCallException;
use RuntimeException as ViewException;
use LogicException as ViewLogicException;
use InvalidArgumentException as ViewInvalidArgumentException;

use function array_key_exists;
use function array_merge;
Expand Down Expand Up @@ -126,6 +127,8 @@ class View implements \ArrayAccess
*
* @param string $view Name of the view file
* @param array $data Array of view variables
*
* @throws ViewException
*/
public function __construct(string $view, array $data = [])
{
Expand Down Expand Up @@ -194,6 +197,80 @@ public function with($key, $value = null)
return $this;
}

/**
* Get the array of view data.
*
* @return array View data.
*/
public function getData(): array
{
return $this->data;
}

/**
* Set views directory.
*
* @param string $directory Views directory.
*/
public static function setDirectory(string $directory): void
{
self::$directory = $directory;
}

/**
* Set views extension.
*
* @param string $extension Views extension.
*/
public static function setExtension(string $extension): void
{
self::$extension = $extension;
}

/**
* Determining If A View Exists
*
* @param string $view View name.
*
* @return bool Returns true or false view doesnt exists.
*/
public static function exists(string $view): bool
{
return filesystem()->file(self::getFilePath($view))->exists();
}

/**
* Get view file path.
*
* @param string $view View name.
*
* @return string Retruns view file path.
*/
public static function getFilePath(string $view): string
{
return strings(self::$directory . '/' . self::denormalizeName(self::normalizeName($view)) . '.' . self::$extension)->replace('//', '/')->toString();
}

/**
* Denormalize view name.
*
* @param string $view View name.
*/
public static function denormalizeName(string $view): string
{
return strings($view)->replace('.', '/')->toString();
}

/**
* Normalize view name.
*
* @param string $view View name.
*/
public static function normalizeName(string $view): string
{
return strings($view)->replace('/', '.')->toString();
}

/**
* Render the view file and extracts the view variables before returning the generated output.
*
Expand Down Expand Up @@ -232,7 +309,7 @@ public function render(?callable $callback = null): string
$this->content = call_user_func($callback, $this->content);
}

// Return content
// Return rendered content
return $this->content;
}

Expand Down Expand Up @@ -275,90 +352,45 @@ public function display(?callable $callback = null): void
}

/**
* Get the array of view data.
* Fetch view.
*
* @return array View data.
*/
public function getData(): array
{
return $this->data;
}

/**
* Set views directory.
* @param string $view View name.
* @param array $data View data.
* @param callable|null $callback Callback function used to filter output.
*
* @param string $directory Views directory.
* @return string View content.
*/
public static function setDirectory(string $directory): void
public function fetch(string $view, array $data = [], ?callable $callback = null): string
{
self::$directory = $directory;
return view($view, $data)->render($callback);
}

/**
* Set views extension.
* Fetch first view that exists in a given array of views.
*
* @param string $extension Views extension.
*/
public static function setExtension(string $extension): void
{
self::$extension = $extension;
}

/**
* Determining If A View Exists
* @param array $views Views array.
* @param array $data View data.
* @param callable|null $callback Callback function used to filter output.
*
* @param string $view View name.
*
* @return bool Returns true or false view doesnt exists.
*/
public static function exists(string $view): bool
{
return filesystem()->file(self::getFilePath($view))->exists();
}

/**
* Get view file path.
* @throws ViewInvalidArgumentException
*
* @param string $view View name.
*
* @return string Retruns view file path.
* @return self
*/
public static function getFilePath(string $view): string
public function fetchFirst(array $views, array $data = [], ?callable $callback = null): string
{
return self::$directory . '/' . self::denormalizeName(self::normalizeName($view)) . '.' . self::$extension;
}
$view = '';

/**
* Denormalize view name.
*
* @param string $view View name.
*/
public static function denormalizeName(string $view): string
{
return strings($view)->replace('.', '/')->toString();
}
foreach ($views as $v) {
if (filesystem()->file(self::getFilePath($v))->exists()) {
$view = $v;
break;
}
}

/**
* Normalize view name.
*
* @param string $view View name.
*/
public static function normalizeName(string $view): string
{
return strings($view)->replace('/', '.')->toString();
}
if ($view === '') {
throw new ViewInvalidArgumentException('None of the views in the given array exist.');
}

/**
* Fetch view.
*
* @param string $view View name.
* @param array $data View data.
* @param callable|null $callback Callback function used to filter output.
*
* @return string View content.
*/
public function fetch(string $view, array $data = [], ?callable $callback = null): string
{
return view($view, $data)->render($callback);
}

Expand Down Expand Up @@ -406,6 +438,35 @@ public function include(string $view, array $data = [], ?callable $callback = nu
view($view, $data)->display($callback);
}

/**
* Include first view that exists in a given array of views.
*
* @param array $views Views array.
* @param array $data View data.
* @param callable|null $callback Callback function used to filter output.
*
* @throws ViewInvalidArgumentException
*
* @return self
*/
public function includeFirst(array $views, array $data = [], ?callable $callback = null): void
{
$view = '';

foreach ($views as $v) {
if (filesystem()->file(self::getFilePath($v))->exists()) {
$view = $v;
break;
}
}

if ($view === '') {
throw new ViewInvalidArgumentException('None of the views in the given array exist.');
}

$this->include($view, $data, $callback);
}

/**
* Include view and display based on a given condition.
*
Expand Down Expand Up @@ -512,6 +573,8 @@ public function appendSection(string $section): void
*
* @param string $section The name of the section.
* @param int $mode The mode of the section.
*
* @throws ViewLogicException
*/
public function section(string $section, int $mode = self::SECTION_MODE_REWRITE): void
{
Expand All @@ -527,6 +590,8 @@ public function section(string $section, int $mode = self::SECTION_MODE_REWRITE)

/**
* Stop the current section block.
*
* @throws ViewLogicException
*/
public function endSection(): void
{
Expand Down Expand Up @@ -565,6 +630,8 @@ public function endSection(): void
* @param string $method Method.
* @param array $parameters Parameters.
*
* @throws BadMethodCallException
*
* @return self
*/
public function __call(string $method, array $parameters)
Expand Down
15 changes: 15 additions & 0 deletions tests/ViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,13 @@
$view->display();
});

test('fetchFirst', function (): void {
$view = view('fetch_first');

$this->expectOutputString("Foo");
$view->display();
});

test('fetchWhen', function (): void {
$view = view('fetch_when');

Expand All @@ -196,6 +203,14 @@
$view->display();
});

test('includeFirst', function (): void {
$view = view('include_first');

$this->expectOutputString("Foo");
$view->display();
});


test('includeWhen', function (): void {
$view = view('include_when');

Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/fetch_first.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?= e($this->fetchFirst(['bar', 'foo'])) ?>
1 change: 1 addition & 0 deletions tests/fixtures/include_first.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php $this->includeFirst(['bar', 'foo']) ?>

0 comments on commit f31e9f0

Please sign in to comment.