<commit_before><?php
/*
 * Copyright 2015 <NAME> <http://jacksleight.com/>
 * This source file is subject to the MIT license that is bundled with this package in the file LICENCE. 
 */

namespace Chalk\Core\Controller;

use Chalk\Chalk,
	Chalk\Core,
	Chalk\Controller\Basic,
	Coast\Request,
	Coast\Response;

class Widget extends Basic
{
	public function preDispatch(Request $req, Response $res)
	{
		$req->view->info
			= $req->info
			= Chalk::info($req->entity);
	}

	public function edit(Request $req, Response $res)
	{
		$class = $req->info->class;
		$widget = new $class();
		$req->view->widget = $wrap = $this->em->wrap($widget);
		$wrap->graphFromArray($req->bodyParams());
      
        if (!$req->post) {
            return;
        }

		if (!$wrap->isValid()) {
			return;
		}

		return $res->json([
			'entity'	=> $req->info->name,
			'params'	=> array_map(function($value) {
				return is_object($value) ? (string) $value : $value;
			}, $widget->toArray()),
		]);
	}

	public function delete(Request $req, Response $res)
	{
		return $res->json([<commit_msg>Fix widgets with objects.<commit_after><?php
/*
 * Copyright 2015 <NAME> <http://jacksleight.com/>
 * This source file is subject to the MIT license that is bundled with this package in the file LICENCE. 
 */

namespace Chalk\Core\Controller;

use Chalk\Chalk,
	Chalk\Core,
	Chalk\Controller\Basic,
	Coast\Request,
	Coast\Response;

class Widget extends Basic
{
	public function preDispatch(Request $req, Response $res)
	{
		$req->view->info
			= $req->info
			= Chalk::info($req->entity);
	}

	public function edit(Request $req, Response $res)
	{
		$class = $req->info->class;
		$widget = new $class();
		$req->view->widget = $wrap = $this->em->wrap($widget);
		$wrap->graphFromArray($req->bodyParams());
      
        if (!$req->post) {
            return;
        }

		if (!$wrap->isValid()) {
			return;
		}

		return $res->json([
			'entity'	=> $req->info->name,
			'params'	=> array_map(function($value) {
				return is_object($value) ? $value->id : $value;
			}, $widget->toArray()),
		]);
	}

	public function delete(Request $req, Response $res)
	{
		return $res->json([
{'fraction_non_alphanumeric': 0.17433751743375175, 'fraction_numerical': 0.003719200371920037, 'mean_word_length': 3.329979879275654, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 50, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2600130', 'n_tokens_mistral': 800, 'n_tokens_neox': 739, 'n_words': 173}