Skip to content
This repository has been archived by the owner on Jan 27, 2022. It is now read-only.

Commit

Permalink
Add Link resolving to transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
iamfredric committed May 27, 2020
1 parent 828ea82 commit 008af8f
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 11 deletions.
13 changes: 3 additions & 10 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,11 @@
"Incognito\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
"autoload-dev": {},
"require": {
"iamfredric/modest": "^1.0",
"iamfredric/instantiator": "^1.0",
"duncan3dc/blade": "^4.5"
"duncan3dc/blade": "^4.8"
},
"require-dev": {
"phpunit/phpunit": "^6.2",
"mockery/mockery": "^0.9.9"
}
"require-dev": {}
}
4 changes: 3 additions & 1 deletion src/Components/Components.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ protected function resolveComponents($components)
*/
protected function resolveClassname($name)
{
$name = ucfirst($name);
$name = collect(explode('-', $name))->map(function ($name) {
return ucfirst($name);
})->implode('');

if (! $this->prefix) {
return "\\App\\Components\\{$name}Component";
Expand Down
5 changes: 5 additions & 0 deletions src/Utilities/Transformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Incognito\Utilities;

use Incognito\Utilities\Wordpress\Image;
use Incognito\Utilities\Wordpress\Link;

trait Transformer
{
Expand Down Expand Up @@ -31,6 +32,10 @@ protected function transform($data = [])
$item = new Image($item);
}

if (is_array($item) && isset($item['url']) && isset($item['title']) && isset($item['target'])) {
$item = new Link($item);
}

$key = dash_to_camel($key);

// Casts item if defined
Expand Down
60 changes: 60 additions & 0 deletions src/Utilities/Wordpress/Link.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Incognito\Utilities\Wordpress;

class Link
{
/**
* @var array
*/
protected $attributes;

/**
* Link constructor.
*
* @param array $attributes
*/
public function __construct(array $attributes)
{
$this->attributes = $attributes;
}

/**
* @return bool
*/
public function exists()
{
return $this->has('url');
}

/**
* @param string $class
*
* @return string
*/
public function render($class = '')
{
return '<a href="' . $this->get('url') . '" class="' . $class . '" target="' . $this->get('target') . '">' . $this->get('title') . '</a>';
}

/**
* @param $key
* @param null $default
*
* @return mixed|null
*/
public function get($key, $default = null)
{
return $this->has($key) ? $this->attributes[$key] : $default;
}

/**
* @param $key
*
* @return bool
*/
public function has($key)
{
return isset($this->attributes[$key]);
}
}

0 comments on commit 008af8f

Please sign in to comment.