PHP code example of treehousetim / document

1. Go to this page and download the library: Download treehousetim/document library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

treehousetim / document example snippets


public function setFromMappedArray( array $map, array $data )

abstract public function jsonSerialize();

abstract protected function validate()

// See class definitions at the bottom of this README

// create a sub document and pass it to a function
$name = (new personName())
	->first( 'Easter' )
	->last( 'Bunny' )
	->full( 'The Easter Bunny' );

$customer = new (customer() )
	->name( $name );

// or create sub documents inline

$customer = new (customer() )
	->name( (new personName())
		->first( 'Easter' )
		->last( 'Bunny' )
		->full( 'The Easter Bunny' )
	)
	->address_line_1( '123 Main Street' )
	->address_line_2( '' )
	->city( 'Dubuque' )
	->state_province( 'IA' )
	->postal_code( '12345' );

class aDocument extends \treehousetim\document\document
{
	const adtHTML = 'html';
	const adtTEXT = 'text';
	const adtJSON = 'json';

	protected $allowedTypes = [
		self::adtHTML,
		self::adtTEXT,
		self::adtJSON
	];

	protected $type;

	public function type( string $type ) : self
	{
		$this->validateValueInList( 'type', $type, $this->allowedTypes );
		return parent::type( $type );
	}
	//------------------------------------------------------------------------
	public function jsonSerialize()
	{
		$this->validate();
		return $this->getFieldArray( 'type' );
	}
	//------------------------------------------------------------------------
	protected function validate()
	{
		$this->validateRequired( 'type' );
	}
}

class arrayDocument extends \treehousetim\document\document
{
	protected $name = array();

	public function jsonSerialize ()
	{
		$this->validate();
		return $this->getFieldArray( 'name' );
	}
	//------------------------------------------------------------------------
	public function name( nameDocument $nameDoc ) : self
	{
		$this->name[] = $nameDoc;
		$this->markValueSet( 'name' );
		return $this;
	}
	//------------------------------------------------------------------------
	protected function validate()
	{
		$this->validateSubDocument( 'name' );
	}
}

// choose one way to do this
public function name( personName $name ) : self
{
	$this->name = $name;
	$this->markValueSet( 'name' );
	return $this;
}

// or 
public function name( personName $name ) : self
{
	return parent::name( $name );
}

class Exception extends \LogicException
{
	const undefined = -1;
	const missingData = 1;
	const wrongType = 2;
	const callOneVar = 3;
	const noSuchProperty = 4;
	const disallowedValue = 5;
	const noValue = 6;
}

class Exception extends \LogicException
{
	const undefined = -1;
	const missingData = 1;
	const wrongType = 2;
	const callOneVar = 3;
	const noSuchProperty = 4;
	const disallowedValue = 5;
	const noValue = 6;
}

<?PHP namespace application\libraries\documents;

class customer extends \treehousetim\document\document
{
	protected $name;
	protected $address_line_1;
	protected $address_line_2;
	protected $city;
	protected $state_province;
	protected $postal_code;
	protected $email;

	public function jsonSerialize ()
	{
		$this->validate();
		return $this->getFieldArray(
			'name',
			'address_line_1',
			'address_line_2',
			'city',
			'state_province',
			'postal_code',
			'email'
		);
	}
	//------------------------------------------------------------------------
	protected function validate()
	{
		// email is optional, so not included

		$this->validateHasValue(
			'address_line_1',
			'city',
			'state_province',
			'postal_code'
		);

		$this->validateRequired( 'address_line_2' );
		$this->validateSubDocument( 'name' );
	}
	//------------------------------------------------------------------------
	public function name( personName $name ) : self
	{
		return parent::name( $name );
	}
}

class personName extends \treehousetim\document\document
{
	protected $first;
	protected $last;
	protected $full;

	public function jsonSerialize()
	{
		return $this->getFieldArray(
			'first',
			'last',
			'full'
		);
	}
	//------------------------------------------------------------------------
	protected function validate()
	{
		// all are 

<?PHP

$map = ['fullName' => 'full_name', 'firstName' => 'first_name', 'lastName' => 'last_name'];

// could be from POST or API incoming parsed JSON
$data = ['fullName' => 'Robot Droid', 'firstName' => 'Robot', 'lastName' => 'Droid'];

// nameDocument from this project's unit tests
$doc = new \treehousetim\document\test\nameDocument();
$doc->setFromMappedArray( $map, $data );
// $doc now has the proper values set from the data array


 namespace App\Models;

class Names
{
	public $full_name;
	public $first_name;
	public $last_name;
	public $suffix;
}

<?PHP
use App\Models\Names as NameModel;
use App\Documents\Name as NameDocument;

$nameDoc = (new NameDocument())
	->first_name( 'Robby' )
	->last_name( 'Robot' )
	->full_name( 'Robby the Robot' );

$model = $nameDoc->asClassWithProps( NameModel::class );

echo $model->first_name; // outputs Robby

 namespace App\Models;

class Names
{
	protected $full_name;
	protected $first_name;
	protected $last_name;
	protected $suffix;

	public function __call( $name, $args )
	{
		$this->${name} = $args[0];
	}
	//------------------------------------------------------------------------
	public function get_first_name() : string
	{
		return $this->first_name;
	}
}

<?PHP
use App\Models\Names as NameModel;
use App\Documents\Name as NameDocument;

$nameDoc = (new NameDocument())
	->first_name( 'Robby' )
	->last_name( 'Robot' )
	->full_name( 'Robby the Robot' );

$model = $nameDoc->asClassWithFuncs( NameModel::class );

 $model->get_first_name(); // outputs Robby

protected function optionalFieldOut( $fieldName, array &$out )
protected function fieldOutIfSet( $name, array &$out )
protected function getFieldArray( string ...$fields ) : array
public function doValidate()
public function dataArray() : array
public function dataObject() : \stdClass
public function fieldExists( $name ) : bool
public function fieldSet( $name ) : bool
public function fieldExistsAndIsSet( $name ) : bool
public function asClassWithProps( string $className, $map = array() ) : object
public function asClassWithFuncs( string $className, $map = array() ) : object