예제 #1
0
 public function testSerialization()
 {
     $testData = [['firstname' => 'Bobby', 'surname' => 'Silver'], ['firstname' => 'Jake', 'surname' => 'Starkiller'], ['firstname' => 'Dirk', 'surname' => 'Diggler']];
     $humanCollection = new HumanCollection();
     foreach ($testData as $node) {
         $humanCollection->add(new Human($node['firstname'], $node['surname']));
     }
     $this->assertEquals($humanCollection->serialize(), json_encode($testData));
 }
예제 #2
0
 public function readFromFile(string $filename)
 {
     if (!file_exists($filename)) {
         throw new Exception('File does not EXIST!');
     }
     $fileContents = file_get_contents($filename);
     if (false === $fileContents) {
         throw new Exception('Could not READ from file!');
     }
     $serializedData = json_decode($fileContents, true);
     $humanCollection = new HumanCollection();
     foreach ($serializedData as $person) {
         $humanCollection->add(new Human($person['firstname'], $person['surname']));
     }
     return $humanCollection;
 }
예제 #3
0
 public function testSerialization()
 {
     // Test Data
     $testData = [['firstname' => 'Bobby', 'surname' => 'Silver'], ['firstname' => 'Jake', 'surname' => 'Starkiller'], ['firstname' => 'Dirk', 'surname' => 'Diggler']];
     // Load up in a collection
     $humanCollection = new HumanCollection();
     foreach ($testData as $node) {
         $humanCollection->add(new Human($node['firstname'], $node['surname']));
     }
     // Save and read from a file
     $fileHandler = new FileHandler();
     $filename = 'testfile.json';
     $fileHandler->writeToFile($humanCollection, $filename);
     $col = $fileHandler->readFromFile($filename);
     unlink($filename);
     // Test is data from the file is the same
     $this->assertEquals($humanCollection->serialize(), $col->serialize());
 }
예제 #4
0
<?php 
include_once 'init.php';
use Symfony\Component\Validator\Validation;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints as Assert;
// Define Validation rules
$validator = Validation::createValidator();
$constraint = new Assert\Collection(['firstname' => [new Assert\Length(['min' => 2, 'max' => 40]), new Assert\Regex(['pattern' => "/^[a-zA-Z0-9]+\$/i", 'message' => 'The First name should contain only letters and numbers!'])], 'surname' => [new Assert\Length(['min' => 2, 'max' => 40]), new Assert\Regex(['pattern' => "/^[a-zA-Z0-9]+\$/i", 'message' => 'The Surname name should contain only letters and numbers!'])]]);
// Go through all people and save only the ones who are valid.
$people = $_GET['people'];
$humanCollection = new HumanCollection();
foreach ($people as $person) {
    echo '<hr>';
    $violations = $validator->validate($person, $constraint);
    // Invalid person
    if (0 < count($violations)) {
        echo '<div>Problem with the peson : ' . $person['firstname'] . ' ' . $person['surname'] . '</div>';
        foreach ($violations as $violation) {
            echo '<div>' . $violation->getMessage() . '</div>';
        }
    } else {
        echo '<div>Peson : ' . $person['firstname'] . ' ' . $person['surname'] . ' saved successfully!</div>';
        $humanCollection->add(new Human($person['firstname'], $person['surname']));
    }
}
// Save valid people to the text file
try {
    $services['file_handler']->writeToFile($humanCollection, DB_FILE);
} catch (Exception $e) {
    echo $e->getMessage();
}