Skip to content

Commit

Permalink
make it possible for two word registerables
Browse files Browse the repository at this point in the history
  • Loading branch information
robojuicedev committed Dec 8, 2017
1 parent 635d582 commit 0aba270
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 6 deletions.
7 changes: 4 additions & 3 deletions src/Elements/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use TypeRocket\Models\Model;
use TypeRocket\Elements\Traits\FormConnectorTrait;
use TypeRocket\Register\Registry;
use TypeRocket\Utility\Str;

class Form
{
Expand Down Expand Up @@ -45,7 +46,7 @@ public function __construct( $resource = 'auto', $action = 'update', $itemId = n
$this->itemId = $itemId;
$this->autoConfig();

$Resource = ucfirst($this->resource);
$Resource = Str::camelize($this->resource);
$model = "\\" . TR_APP_NAMESPACE . "\\Models\\{$Resource}";

if(class_exists($model)) {
Expand Down Expand Up @@ -93,7 +94,7 @@ protected function autoConfig()
$item_id = $post->ID;
$resource = Registry::getPostTypeResource($post->post_type);

$Resource = ucfirst($resource[0]);
$Resource = Str::camelize($resource[0]);
$resource = $resource[0];
$model = "\\" . TR_APP_NAMESPACE . "\\Models\\{$Resource}";
$controller = "\\" . TR_APP_NAMESPACE . "\\Controllers\\{$Resource}Controller";
Expand All @@ -111,7 +112,7 @@ protected function autoConfig()
$item_id = $tag_ID;
$resource = Registry::getTaxonomyResource($taxonomy);

$Resource = ucfirst($resource[0]);
$Resource = Str::camelize($resource[0]);
$resource = $resource[0];
$model = "\\" . TR_APP_NAMESPACE . "\\Models\\{$Resource}";
$controller = "\\" . TR_APP_NAMESPACE . "\\Controllers\\{$Resource}Controller";
Expand Down
3 changes: 2 additions & 1 deletion src/Http/Responders/PostsResponder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use \TypeRocket\Http\Request;
use \TypeRocket\Http\Response;
use \TypeRocket\Register\Registry;
use TypeRocket\Utility\Str;

class PostsResponder extends Responder
{
Expand All @@ -26,7 +27,7 @@ public function respond( $args )

$type = get_post_type( $id );
$resource = Registry::getPostTypeResource( $type );
$prefix = ucfirst( $resource[0] );
$prefix = Str::camelize( $resource[0] );
$controller = "\\" . TR_APP_NAMESPACE . "\\Controllers\\{$prefix}Controller";
$model = "\\" . TR_APP_NAMESPACE . "\\Models\\{$prefix}";
$resource = $resource[0];
Expand Down
3 changes: 2 additions & 1 deletion src/Http/Responders/TaxonomiesResponder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use \TypeRocket\Http\Request;
use \TypeRocket\Http\Response;
use \TypeRocket\Register\Registry;
use TypeRocket\Utility\Str;

class TaxonomiesResponder extends Responder
{
Expand All @@ -22,7 +23,7 @@ public function respond( $args )
{
$taxonomy = $this->taxonomy;
$resource = Registry::getTaxonomyResource( $taxonomy );
$prefix = ucfirst( $resource[0] );
$prefix = Str::camelize( $resource[0] );
$controller = "\\" . TR_APP_NAMESPACE . "\\Controllers\\{$prefix}Controller";
$model = "\\" . TR_APP_NAMESPACE . "\\Models\\{$prefix}";
$resource = $resource[0];
Expand Down
3 changes: 2 additions & 1 deletion src/Http/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use TypeRocket\Controllers\Controller;
use TypeRocket\Core\Resolver;
use TypeRocket\Models\Model;
use TypeRocket\Utility\Str;

/**
* Class Router
Expand Down Expand Up @@ -36,7 +37,7 @@ public function __construct( Request $request, Response $response, $action_metho
$this->response = $response;
$this->action = $this->getAction( $action_method );
$resource = $this->request->getResource();
$resource = ucfirst( $resource );
$resource = Str::camelize( $resource );
$controller = "\\" . TR_APP_NAMESPACE . "\\Controllers\\{$resource}Controller";

if ( class_exists( $controller ) ) {
Expand Down
1 change: 1 addition & 0 deletions src/Register/PostType.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

class PostType extends Registrable
{
use Resourceful;

private $title = null;
private $form = [];
Expand Down
31 changes: 31 additions & 0 deletions src/Register/Resourceful.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace TypeRocket\Register;

use TypeRocket\Utility\Inflect;
use TypeRocket\Utility\Sanitize;

trait Resourceful
{
/**
* Set the Registrable ID for WordPress to use. Don't use reserved names.
*
* @param string $id set the ID
* @param boolean $resource update the resource binding
*
* @return $this
*/
public function setId($id, $resource = false)
{
$this->id = Sanitize::underscore($id);
$this->dieIfReserved();

if($resource) {
$singular = Sanitize::underscore( $this->id );
$plural = Sanitize::underscore( Inflect::pluralize($this->id) );
$this->resource = [$singular, $plural];
}

return $this;
}
}
2 changes: 2 additions & 0 deletions src/Register/Taxonomy.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
class Taxonomy extends Registrable
{

use Resourceful;

private $postTypes = [];
private $form = [];
private $resource = null;
Expand Down
20 changes: 20 additions & 0 deletions src/Utility/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,24 @@ public static function starts($needle, $subject)
return mb_substr($subject, 0, mb_strlen($needle) ) === $needle;
}

/**
* Convert To Camel Case
*
* @param string $input
* @param string $separator specify - or _
* @param bool $capitalize_first_char define as false if you want camelCase over CamelCase
*
* @return mixed
*/
public static function camelize($input, $separator = '_', $capitalize_first_char = true)
{
$str = str_replace($separator, '', ucwords($input, $separator));

if (!$capitalize_first_char) {
$str = lcfirst($str);
}

return $str;
}

}

0 comments on commit 0aba270

Please sign in to comment.