Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
OzanKurt committed Mar 14, 2024
1 parent 389c6e2 commit 16994cd
Show file tree
Hide file tree
Showing 12 changed files with 2,577 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Laravel Contribution Guide

Thank you for considering contributing to the Laravel framework! The contribution guide can be found in the [Laravel documentation](http://laravel.com/docs/contributions). Please review the entire guide before sending a pull request.
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) <Adam Engebretson>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# html
Official documentation for Forms & Html for The Laravel Framework can be found at the [LaravelCollective](https://laravelcollective.com/docs) website.
53 changes: 53 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"name": "ozankurt/html",
"description": "HTML and Form Builders for the Laravel Framework",
"license": "MIT",
"homepage": "https://ozankurt.com",
"support": {
"issues": "https://github.com/OzanKurt/html/issues",
"source": "https://github.com/OzanKurt/html"
},
"authors": [
{
"name": "Ozan Kurt",
"email": "me@ozankurt.com"
}
],
"require": {
"php": "^7.2|^8.0",
"illuminate/http": "^6.0|^7.0|^8.0|^9.0|^10.0|^11.0",
"illuminate/routing": "^6.0|^7.0|^8.0|^9.0|^10.0|^11.0",
"illuminate/session": "^6.0|^7.0|^8.0|^9.0|^10.0|^11.0",
"illuminate/support": "^6.0|^7.0|^8.0|^9.0|^10.0|^11.0",
"illuminate/view": "^6.0|^7.0|^8.0|^9.0|^10.0|^11.0"
},
"require-dev": {
"illuminate/database": "^6.0|^7.0|^8.0|^9.0|^10.0|^11.0",
"mockery/mockery": "~1.0",
"phpunit/phpunit": "~8.5|^9.5.10"
},
"autoload": {
"psr-4": {
"OzanKurt\\Html\\": "src/"
},
"files": [
"src/helpers.php"
]
},
"extra": {
"branch-alias": {
"dev-master": "1.x-dev"
},
"laravel": {
"providers": [
"OzanKurt\\Html\\HtmlServiceProvider"
],
"aliases": {
"Form": "OzanKurt\\Html\\FormFacade",
"Html": "OzanKurt\\Html\\HtmlFacade"
}
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
111 changes: 111 additions & 0 deletions src/Componentable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

namespace OzanKurt\Html;

use BadMethodCallException;
use Illuminate\Support\Arr;
use Illuminate\Support\HtmlString;

trait Componentable
{

/**
* The registered components.
*
* @var array
*/
protected static $components = [];

/**
* Register a custom component.
*
* @param $name
* @param $view
* @param array $signature
*
* @return void
*/
public static function component($name, $view, array $signature)
{
static::$components[$name] = compact('view', 'signature');
}

/**
* Check if a component is registered.
*
* @param $name
*
* @return bool
*/
public static function hasComponent($name)
{
return isset(static::$components[$name]);
}

/**
* Render a custom component.
*
* @param $name
* @param array $arguments
*
* @return HtmlString
*/
protected function renderComponent($name, array $arguments)
{
$component = static::$components[$name];
$data = $this->getComponentData($component['signature'], $arguments);

return new HtmlString(
$this->view->make($component['view'], $data)->render()
);
}

/**
* Prepare the component data, while respecting provided defaults.
*
* @param array $signature
* @param array $arguments
*
* @return array
*/
protected function getComponentData(array $signature, array $arguments)
{
$data = [];

$i = 0;
foreach ($signature as $variable => $default) {
// If the "variable" value is actually a numeric key, we can assume that
// no default had been specified for the component argument and we'll
// just use null instead, so that we can treat them all the same.
if (is_numeric($variable)) {
$variable = $default;
$default = null;
}

$data[$variable] = Arr::get($arguments, $i, $default);

$i++;
}

return $data;
}

/**
* Dynamically handle calls to the class.
*
* @param string $method
* @param array $parameters
*
* @return \Illuminate\Contracts\View\View|mixed
*
* @throws \BadMethodCallException
*/
public function __call($method, $parameters)
{
if (static::hasComponent($method)) {
return $this->renderComponent($method, $parameters);
}

throw new BadMethodCallException("Method {$method} does not exist.");
}
}
115 changes: 115 additions & 0 deletions src/Eloquent/FormAccessible.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

namespace OzanKurt\Html\Eloquent;

use ReflectionClass;
use ReflectionMethod;
use Illuminate\Support\Str;

trait FormAccessible
{

/**
* A cached ReflectionClass instance for $this
*
* @var ReflectionClass
*/
protected $reflection;

/**
* @param string $key
*
* @return mixed
*/
public function getFormValue($key)
{
$value = $this->getAttributeFromArray($key);

// If the attribute is listed as a date, we will convert it to a DateTime
// instance on retrieval, which makes it quite convenient to work with
// date fields without having to create a mutator for each property.
if (in_array($key, $this->getDates())) {
if (! is_null($value)) {
$value = $this->asDateTime($value);
}
}

// If the attribute has a get mutator, we will call that then return what
// it returns as the value, which is useful for transforming values on
// retrieval from the model to a form that is more useful for usage.
if ($this->hasFormMutator($key)) {
return $this->mutateFormAttribute($key, $value);
}

$keys = explode('.', $key);

if ($this->isNestedModel($keys[0])) {
$relatedModel = $this->getRelation($keys[0]);

unset($keys[0]);
$key = implode('.', $keys);

if (method_exists($relatedModel, 'hasFormMutator') && $key !== '' && $relatedModel->hasFormMutator($key)) {
return $relatedModel->getFormValue($key);
}

return data_get($relatedModel, empty($key)? null: $key);
}

// No form mutator, let the model resolve this
return data_get($this, $key);
}

/**
* Check for a nested model.
*
* @param string $key
*
* @return bool
*/
public function isNestedModel($key)
{
return in_array($key, array_keys($this->getRelations()));
}

/**
* @param $key
*
* @return bool
*/
public function hasFormMutator($key)
{
$methods = $this->getReflection()->getMethods(ReflectionMethod::IS_PUBLIC);

$mutator = collect($methods)
->first(function (ReflectionMethod $method) use ($key) {
return $method->getName() === 'form' . Str::studly($key) . 'Attribute';
});

return (bool) $mutator;
}

/**
* @param $key
* @param $value
*
* @return mixed
*/
private function mutateFormAttribute($key, $value)
{
return $this->{'form' . Str::studly($key) . 'Attribute'}($value);
}

/**
* Get a ReflectionClass Instance
* @return ReflectionClass
*/
protected function getReflection()
{
if (! $this->reflection) {
$this->reflection = new ReflectionClass($this);
}

return $this->reflection;
}
}

0 comments on commit 16994cd

Please sign in to comment.