PHP code example of macfja / value-provider

1. Go to this page and download the library: Download macfja/value-provider 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/ */

    

macfja / value-provider example snippets


class Person {
    public $firstName = '';
    public $lastName = '';
}

$jdoe = new Person();
$provider = new PropertyProvider();

// ...

$provider->setValue($jdoe, 'firstName', 'John');
$provider->setValue($jdoe, 'lastName', 'Doe');

// ...

echo 'Hello ' . $provider->getValue($jdoe, 'firstName') . ' ' . $provider->getValue($jdoe, 'lastName');
//Output : "Hello John Doe"

class Person {
    private $_firstName = 'John';
    private $_lastName = 'Doe';

    function __get($name) {
        if (in_array($name, array('firstName', 'lastName')) {
            $propertyName = '_' . $name;
            return $this->$propertyName;
        }

        throw new \BadFunctionCallException;
    }

    function __set($name, $value) {
        if (in_array($name, array('firstName', 'lastName')) {
            $propertyName = '_' . $name;
            $this->$propertyName = $value;
            return;
        }

        throw new \BadFunctionCallException;
    }

    function __isset($name) {
        return in_array($name, array('firstName', 'lastName');
    }
}

$jdoe = new Person();
$provider = new PropertyProvider();

// ...

$provider->setValue($jdoe, 'firstName', 'John');
$provider->setValue($jdoe, 'lastName', 'Doe');

// ...

echo 'Hello ' . $provider->getValue($jdoe, 'firstName') . ' ' . $provider->getValue($jdoe, 'lastName');
//Output : "Hello John Doe"

class Person {
    private $_firstName = '';
    private $_lastName = '';
    private $_known = true;

    public function getFirstName() {
        return $this->_firstName;
    }
    public function setFirstName($value) {
        $this->_firstName = $value;
    }
    public function getLastName() {
        return $this->_lastName;
    }
    public function setLastName($value) {
        $this->_firstName = $value;
    }
    public function isKnown() {
        return $this->_known;
    }
    public function setKnown($flag) {
        $this->_known = $flag;
    }
}

$jdoe = new Person();
$provider = new MutatorProvider();

// ...

$provider->setValue($jdoe, 'firstName', 'John');
$provider->setValue($jdoe, 'lastName', 'Doe');
$provider->setValue($jdoe, 'known', false);

// ...

echo 'Hello ' . $provider->getValue($jdoe, 'firstName') . ' ' . $provider->getValue($jdoe, 'lastName');
echo ', you are ' . ($provider->getValue($jdoe, 'known') ? '' : 'un') . 'known';
//Output : "Hello John Doe, you are unknown"

class Person {
    private $_firstName = 'John';
    private $_lastName = 'Doe';
    private $_known = true;

    function __call($name, $arguments) {
        switch ($name) {
            case 'getFirstName':
                return $this->_firstName;
            case 'getLastName':
                return $this->_lastName;
            case 'isKnown':
                return $this->_known;
            case 'setFirstName':
                $this->_firstName = $argument[0];
                return;
            case 'setLastName':
                $this->_lastName = $argument[0];
                return;
            case 'setKnown':
                $this->_known = $argument[0];
                return;
        }

        throw new \BadFunctionCallException;
    }
}

$jdoe = new Person();
$provider = new MutatorProvider();

// ...

$provider->setValue($jdoe, 'firstName', 'John');
$provider->setValue($jdoe, 'lastName', 'Doe');
$provider->setValue($jdoe, 'known', false);

// ...

echo 'Hello ' . $provider->getValue($jdoe, 'firstName') . ' ' . $provider->getValue($jdoe, 'lastName');
echo ', you are ' . ($provider->getValue($jdoe, 'known') ? '' : 'un') . 'known';
//Output : "Hello John Doe, you are unknown"

/** @type EntityManager $entityManager */
$class = 'MyClass';
$id = 1234;

$provider = new MetadataProvider();
MetadataProvider::setEntityManager($entityManager);

jdoe = $entityManager->find($class, $id);

// ...

$provider->setValue($jdoe, 'firstName', 'John');
$provider->setValue($jdoe, 'lastName', 'Doe');
$provider->setValue($jdoe, 'known', false);

// ...

echo 'Hello ' . $provider->getValue($jdoe, 'firstName') . ' ' . $provider->getValue($jdoe, 'lastName');
echo ', you are ' . ($provider->getValue($jdoe, 'known') ? '' : 'un') . 'known';
//Output : "Hello John Doe, you are unknown"

class Person {
    public $firstName = 'John';
    protected $lastName = 'Doe';
    private $known = true;
}

$jdoe = new Person();
$provider = new ReflectorProvider();

// ...

$provider->setValue($jdoe, 'firstName', 'John');
$provider->setValue($jdoe, 'lastName', 'Doe');
$provider->setValue($jdoe, 'known', false);

// ...

echo 'Hello ' . $provider->getValue($jdoe, 'firstName') . ' ' . $provider->getValue($jdoe, 'lastName');
echo ', you are ' . ($provider->getValue($jdoe, 'known') ? '' : 'un') . 'known';
//Output : "Hello John Doe, you are unknown"